Reputation: 802
i've the following the folder stucrtre
/var/data/2017/01/$day/files
i want to excute shell script to for loop in the all months / day and run shell script against all files
DIR_PROC = /var/data/2017/01/$day/$file
for day in {1..30} do
echo "processing file $file
sh /opt/myscript.sh /var/data/2017/1/$day/$file
done
i'm not sure about the logic here i missed the file listing how i can get it
any advise here
Upvotes: 0
Views: 91
Reputation: 6527
If you want to run the script for all files in the directory structure, just use find
:
find /var/data/2017/01 -type f -exec sh /opt/myscript.sh {} \;
Or with the Processing...
lines (with GNU find):
find /var/data/2017/01 -type f -printf "Processing %p\n" -exec sh /opt/myscript.sh {} \;
If you only want to run on some subdirectories in the tree, you'll need to use -path
or -name
and -prune
to limit the matches.
Upvotes: 1
Reputation: 19315
dir_proc=/var/data/2017/01
# maybe {1..31} because month can have 31 days
for day in {1..30}; do
# check directory exist || otherwise continue with next day
[[ -e $dir_proc/$day ]] || continue
for file in "$dir_proc/$day/"*; do
# check file exist || otherwise continue with next file
[[ -e $file ]] || continue
# do something with "$file"
done
done
EDIT with months:
dir_proc=/var/data/2017
for month in {01..12}; do
for day in {01..31}; do
# check directory exist || otherwise continue with next day
[[ -e $dir_proc/$month/$day ]] || continue
for file in "$dir_proc/$month/$day/"*; do
# check file exist || otherwise continue with next file
[[ -e $file ]] || continue
# do something with "$file"
done
done
done
Or shorter, with one for
dir_proc=/var/data/2017
for month_day in {01..12}/{01..31}; do
# check directory exist || otherwise continue with next day
[[ -e $dir_proc/${month_day} ]] || continue
for file in "$dir_proc/${month_day}/"*; do
# check file exist || otherwise continue with next file
[[ -e $file ]] || continue
# do something with "$file"
done
done
Upvotes: 4