Run a shell script for each file in the directory

I have a bunch of files in a directory called YYYY_MM_DD

-rw-r--r-- 1 root root 480K Apr 21 13:17 2012_04_05
-rw-r--r-- 1 root root 483K Apr 21 13:17 2012_04_06
-rw-r--r-- 1 root root 484K Apr 21 13:17 2012_04_07
-rw-r--r-- 1 root root 480K Apr 21 13:17 2012_04_08
-rw-r--r-- 1 root root 344K Apr 21 13:17 2012_04_09
-rw-r--r-- 1 root root  66K Apr 21 13:17 2012_04_10
-rw-r--r-- 1 root root 461K Apr 21 13:17 2012_04_11
-rw-r--r-- 1 root root 475K Apr 21 15:09 2012_04_17
-rw-r--r-- 1 root root 480K Apr 21 15:10 2012_04_18
-rw-r--r-- 1 root root 474K Apr 21 15:10 2012_04_19
-rw-r--r-- 1 root root 474K Apr 21 15:10 2012_04_20

I have a shell script that takes a file as a parameter and calculates numbers based on the data in the file, I call a script like this

sh Calculate.sh MyFile

I want to run this shell script for every file in this directory.

How would I do this, xargs ??

+5
source share
4 answers

Have you tried the findexecution team ?

My sample will echo the files, but you can call the shell script with the file name as a parameter

find . -maxdepth 1 -type f -exec echo {} \;
+5
source

A simple loop in a shell:

for file in *; do sh Calculate.sh "$file"; done
+3
source

find . -maxdepth 1 -type f | xargs -n 1 -I % Calculate.sh %

+1
source
./Calculate.sh 2012_04_{05..20}
+1
source

All Articles