list files commands

hi all scripting gurus,

need some guide and advise from you.

i'm trying to list all the files in the year 2004 and the file format is something like this: 11176MZ00004JV900004JVB00004JVCcDBU20041206.txt try to use the symbol ^ but somehow it does not help.

i try this as well: ls -ltr | grep 200401 | more and it does shows the files listed in 2004 but also in 2005 and 2006. any way i can refine this search? :o

can anyone help? many thanks. :slight_smile:

wee

If there are files from 2005 and 2006 which coincidentally contain 200401 in their file name then you need to make the regular expression more exact. If the date is always just before a final .txt extension then grep 2004....\.txt$ might work better. (Just a dot means "any character" in a regular expression.)

hi era,

thanks for the guide. tried the command but getting this result:

spids111% ls -ltr |grep *2004....\.txt$* | more
No match
spids111%

somewhere gone wrong in my command line? :confused:

thanks again! :slight_smile:

wee

it works!! sorry i put in the *...oops...many thanks!! :b:

wee

apologies...one more question.

if i want to remove the files in 2004 what is the next command i need to put in? using rm command? like this:

spids111% ls -ltr | grep 2004....\.txt$ | rm * ?

rm doesn't read standard input, it expects the files to remove on its command line. Use xargs or backticks.

rm `ls -ltr | grep 2004....\.txt$`

or

ls -ltr | grep 2004....\.txt$ | xargs rm

Try it with "echo" instead of "rm" to make sure that you're doing the right thing.

(Weird, double post, sorry.)

era,

thanks a million :b: it works!

wee

ooops....it does not work...got the following:

spids111% ls -ltr | grep 2004....\.txt$ | xargs rm
rm: illegal option -- w
rm: illegal option -- -
rm: illegal option -- w
rm: illegal option -- -
rm: illegal option -- -
rm: illegal option -- -
usage: rm [-fiRr] file ...

somewhere went wrong? apologies...:o

wee

Yes, take out the options from ls, you just want the file names -- it's trying to remove the permissions, the owners, and the date stamps, too. Oops.

hi era,

sorry for the trouble...:o

i try the following:

spids111% grep 2004....\.txt$ | xargs echo

but nothing returns...some part went wrong? thanks.

wee

You took out too much. You still need ls to list the files, only not in "long format".

ls | grep 2004....\.txt$ | xargs echo rm

I put in an "echo" so you see what it's doing. Take out the echo if the result looks correct.

hi era...wonderful!! it works perfectly fine now! thanks so much!!:b: