1850818 Members
3434 Online
104056 Solutions
New Discussion

Re: listing files

 
himacs
Super Advisor

listing files

Hi admin,

My directory contains lots of files including files appended with date.I want to list files with no date appended.For example i want to list only test.txt not text.txt.11112009

Please suggest

regards
himacs
11 REPLIES 11
R.K. #
Honored Contributor

Re: listing files

Hi Himacs,

If all files in that directory contains 2009 then you can try this:

# ll | grep -v 2009*

Let me know if you have any exceptions.

Regds..
Don't fix what ain't broke
chendan_1
Regular Advisor

Re: listing files

the "awk" command may help you.
Wouter Jagers
Honored Contributor

Re: listing files

This would remove any filenames from your listing which end on 6 or more digits

ls | perl -ne 'print if (!/\d{6}\d*$/)'

cheers
an engineer's aim in a discussion is not to persuade, but to clarify.
Wouter Jagers
Honored Contributor

Re: listing files

Sorry, seems I misread the question.

ls | sed 's/\(.*\)[0-9][0-9][0-9][0-9][0-9][0-9][0-9]*$/\1/'

would chop off the digits at the end, if 6 or more.
an engineer's aim in a discussion is not to persuade, but to clarify.
Wouter Jagers
Honored Contributor

Re: listing files

and this should do the same

perl -ne 's/(.*)[^0-9][0-9]{6}[0-9]*$/\1/; print;'
an engineer's aim in a discussion is not to persuade, but to clarify.
Suraj K Sankari
Honored Contributor

Re: listing files

Hi,

If its a single directiory then do "ls -lrt" and you will get the files with sorted date recent modify at last.

Suraj
Patrick Wallek
Honored Contributor

Re: listing files

What happens if you just do a

# ll *.txt

James R. Ferguson
Acclaimed Contributor

Re: listing files

Hi HImacs:

Depending on your exact objectives, this might help:

# ls | grep -Ev "[0-9]{6}"

This will filter out files whose size is 6-digits in length if you do a long 'ls' listing, though (i.e. 'ls -l') so, this would correctly report files from that, too:

# ls -l | awk '$NF!~/[0-9]{6}/'

The assumption here is that a date that is part of the filename is exactly 6-digits long. If you always use the format of "MMDDYYYY" as you suggest in your example, a better expression might be:

ls -l | awk '$NF!~/[0-1][0-9][0-3][0-9][20][0-9][0-9]/'

In this case, the YYYY portion is assumed to be the year 2000 or later.

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: listing files

>I want to list only test.txt not text.txt.11112009

Then exclude that pattern:
ls !(*.[01][0-9][0-3][0-9][12][0-9][0-9][0-9])

>JRF: You probably didn't want [20]:
ll | awk '$NF !~ /[0-1][0-9][0-3][0-9]20[0-9][0-9]/'
Arturo Galbiati
Esteemed Contributor

Re: listing files

Hi himacs,
this should run (I'm not able to test it now):
ls -l !(*.*2009*)

this will excluse all file with 2009 in the suffix

HTH,
Art
R.K. #
Honored Contributor

Re: listing files

@Arturo

# ls -l !(*.*2009*) <<== Works good
Don't fix what ain't broke