Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through all files in a given directory [duplicate]

Tags:

shell

Here is what I'm trying to do:

Give a parameter to a shell script that will run a task on all files of jpg, bmp, tif extension.

Eg: ./doProcess /media/repo/user1/dir5

and all jpg, bmp, tif files in that directory will have a certain task run on them.

What I have now is:

for f in *
do
  imagejob "$f"  "output/${f%.output}" ;
done

I need help with the for loop to restrict the file types and also have some way of starting under a specified directory instead of current directory.

like image 228
ash Avatar asked Feb 28 '11 04:02

ash


1 Answers

Use shell expansion rather than ls

for file in *.{jpg,bmp,tif}
do
  imagejob "$file" "output/${file%.output}"
done

also if you have bash 4.0+, you can use globstar

shopt -s globstar
shopt -s nullglob
shopt -s nocaseglob
for file in **/*.{jpg,bmp,tif}
do
  # do something with $file
done
like image 79
kurumi Avatar answered Dec 30 '22 08:12

kurumi