Excluding files with timestamp from ls

Hi,

I have a list of files all starting with aa but some of them also have a timestamp suffixed which I want to remove from my search. For e.g.

aa1
aa2
aa.15-05-25_20:41:05.20150611
aa.15-05-26_20:29:40.20150611
aa.15-05-27_20:28:32.20150611

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

With your sample filenames, you can use something like:

ls -l aa* | grep -v '[.]'
ls aa[^.]*

Thank you Don Cragun and Aia for your reply. That's exactly what I was looking for.

@Don Cragun - My actual file list looks like this:

 sample-test~1.lg.15-05-25_20:41:05.20150611
 sample-test~1.lg.*
 sample-test~1.lg.15-05-27_20:28:32.20150611
 sample-test~1.lg.15-05-26_20:29:40.20150611
 sample-test~1.lg.20160431.lg
 sample-test~1.lg.20150301.lg
 sample-file~df_gp~1.lg.20150616
 sample-test~1.lg.20150616
 sample-test~1.lg.15-05-28_20:28:27.20150616
 sample-test~1.lg.15-05-27_20:28:32.20150616
 sample-test~1.lg.15-05-26_20:29:40.20150616
 sample-test~1.lg.15-05-25_20:41:05.20150616
 sample-bat-test~1.lg.20150616

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:

sample-bat-test~1.lg.20150616
sample-test~1.lg.*
sample-test~1.lg.20150301.lg
sample-test~1.lg.20150616
sample-test~1.lg.20160431.lg
sample-file~df_gp~1.lg.20150616

I want to create empty files of all the ones without a timestamp so tried doing this but doesn't seem to work:

for fn in `ls -1 sample* | grep -v '[:]'`
do
touch fn
done

Could you please tell me where I've gone wrong?

Without the `$' what you are doing is creating an empty file named fn and successively updating the metadata each time it loops.

With a `$' you are just updating the metadata of the file that the for loop iterates over.

What you need is a different location where that file can live without conflicting with others, or change to a different name.

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).

Thank you both. Is there a way I can exclude the files with both the date and timestamp? I mean only the below files need to be retained:

 sample-test~1.lg.
 sample-test~1.lg.
 sample-file~df_gp~1.lg.
 sample-bat-test~1.lg.

from the list below:

 sample-test~1.lg.15-05-25_20:41:05.20150611
 sample-test~1.lg.15-05-27_20:28:32.20150611
 sample-test~1.lg.15-05-26_20:29:40.20150611
 sample-test~1.lg.
 sample-test~1.lg.
 sample-file~df_gp~1.lg.
 sample-test~1.lg.20150616
 sample-test~1.lg.15-05-28_20:28:27.20150616
 sample-test~1.lg.15-05-27_20:28:32.20150616
 sample-test~1.lg.15-05-26_20:29:40.20150616
 sample-test~1.lg.15-05-25_20:41:05.20150616
 sample-bat-test~1.lg.

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