Intersection of all files in a given directory

Here is what I am trying to do:

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

For example: ./ doProcess / media / repo / user1 / dir5

and all jpg, bmp, tif files in this directory will run a specific task.

Now I have:

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

I need help with a for loop to limit file types, and also have some way to run in the specified directory instead of the current directory.

+7
source share
3 answers

Use shell extension, not ls

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

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 
+6
source
 for i in `ls $1/*.jpg $1/*.bmp $1/*.tif`; do imagejob "$i"; done 

This assumes you are using a bashlike shell, where $1 is the first argument.

You can also do:

 find "$1" -iname "*.jpg" -or -iname "*.bmp" -or -iname "*.tif" \ -exec imagejob \{\} \; 
+1
source

You can use the construct with backticks and ls (or any other commando, of course):

 for f in `ls *.jpg *.bmp *.tif`; do ...; done 
+1
source

All Articles