Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to list only the files in a directory into a file

Status
Not open for further replies.

madurai1968

IS-IT--Management
Sep 24, 2004
2
US
I am using command ls -l | grep ^- and this list only the files in the directory

But actually, I wanted to have only the list all the files present in the directory to a file and then use it in for loop in shell script.

If i use,
ls -l|grep ^- >pickfile

then i would have something like below in the pickfile.
-rw-r--r-- 1 ediusrd edi 20 Sep 24 15:49 cos00878_97878
-rw-r--r-- 1 ediusrd edi 20 Sep 24 15:21 cos00008676_45630


I wanted to have only the file name in the pickfile so that i can use in the for loop ny checking the file, say
cos00878_97878
cos00008676_45630.

something like ls |grep ^- >pickfile. But this does not seem to work.

Do you have some information on this OR do you have any awk/unix pipe commands throught which I can acheive this.

Thank for your time.
 


ls -l | egrep -v '^l|^p|^d' | awk '{print $NF}'


The Egrep -v eliminates

links
directories
named pipes

If you wanted to inlcude those as well you can remove that section. If you need to eliminate more kinds add more cases.

The $NF says print the Last field of the line.



 
Also
Code:
ls -1 > pickfile
will work, but links or directories will be included.
Note - syntax is -1 (number one), not -l (letter 'l'). ls -1 (one) lists directory with only the names of the files - one file per line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top