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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to loop through files in a directory

Status
Not open for further replies.

jongbsio

Programmer
Aug 16, 2002
5
US
Hi and thanks.

Does anyone have a simple sample script that will loop through a directory and echo out the name of the file to the screen. Something like "ls" does, but what I want to do with this is be able to run a command against each file name.

I tried this, but I get nothing....

###############
for file
in $*
do
echo $file
done
###############

what am I doing wrong?
 
for file
in $*

Remove the '$' to make it:
for file in *
do
echo $file
done

or:
for file in `ls /usr`
do
echo $file
done

or get more complicated and set arrays and pass over directories or go into directories.
 
This on will also work

for i in `ls -l | awk '{print $9}'`
do
echo $i
done
 
Or this:

ls | xargs -n 1 "command"

Where "command" is your command, without the quotes of course. For example,

ls|xargs -n 1 file IBM Certified -- AIX 4.3 Obfuscation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top