If I do ls -1 aa* , it will list everything but I want the timestamped ones to be excluded from ls . How do I make use of grep -v , if at all I can?Any help would be appreciated
So, inline with the example given by you, I managed to filter out the ones with timestamp by using ls -1 sample* | grep -v '[:]' and obtained the below:
As Aia said, in this case, you're just creating or updating the timestamps of a file named fn with your current loop. The touch command creates files named as operands if they didn't already exist and updates the timestamps of files that already existed; it doesn't remove the contents of a file.
If what you're trying to do is turn your selected files into empty files, there is no need for ls or grep , you can just use something like:
#!/bin/ksh
for fn in sample*
do [ "$fn" == "${fn%*:*}" ] && > "$fn" && printf '%s truncated\n' "$fn"
done
which (since it just uses shell built-ins and doesn't need to invoke ls , grep , and touch ) will run faster, and it will work even if some of your filenames contain whitespace characters (i.e., space, tab, or newline).
meaning any file that has any kind of a date or time extension should get filtered out. ls -1 sample* | grep -v '[:,-]' is removing all of the files because of the hyphen between sample and test.
First, you can't have two files in a directory with the same name. Do some of your filenames contain backspace characters that disappeared when you copied your filenames into the lists above?
You can make up expressions that might work for any particular list you have to work with, but we're just wasting our time doing so. You need to clearly define the format of names you want to keep and the format of names of files you want to "filter out". Then you can look for an expression (or set of expressions) that can reliably determine which files to keep and which files to "filter out".
And what does "filter out" mean? Before, you said you wanted to truncate the contents of files. Now, you're talking about retaining some files and other files get filtered out???
#!/bin/ksh
for fn in sample*
do [ "$fn" != "${fn%*[0-9][0-9]*}" ] && printf 'filter out %s\n' "$fn" || printf 'retain %s\n' "$fn"
done