How can I run script for all directories?

PorteM

Suppose I am at the main directory which contains script.sh and subdirectories, each of subdirectories contain images.
The script.sh is a script for resizing images. I want to apply this script to every subdirectories, so after searching for solution I created another script which is

SAVEIFS=$IFS #Since the subdirectories contain whitespaces in their name
IFS=$(echo -en "\n\b")
for d in ./*; do
  if [ -d "$d" ]; then
    echo "$d" && cp ./script.sh ./$d/script.sh && cd "$d" && exec sh ./script.sh && cd ..
  fi
done
IFS=$SAVEIFS

The problem is this script stops after it's done at the first subdirectory. How can I make it runs for all subdirectories? Or are there better way to make script.sh runs for all subdirectories?

RonJohn

find to the rescue!

CURDIR=`pwd`
IFS=$'\n'
for d in $(find . -type d); 
do 
    cd $CURDIR/"$d"
    $CURDIR/script.sh
done

Even better... strip the "all files in the directory" stuff from your script, and:

IFS=$'\n'
for f in $(find . -type f); 
do 
    ./script.sh "$f"
done

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make the "du" command run without outputting all the directories, like quiet mode?

How can I remove specific directories that all start with a common letter?

How can I run mocha with npm script?

How can i run all migrations for all plugins in cakephp 3?

How can I get a listing of all directories with videos on them?

How can I run a script on multiple folders

How can I recursively delete all empty files and directories in Linux?

How can I run a Python 3 script?

How can i move all files and folders from multiple directories?

How can I run a script recursively in a directory

How can I run commands in bash script?

How can i delete all the files in all directories?

How can i run this excel script on all sheets ? Remove all hyperlinks in entire workbook

How can I run a Powershell script in PHP?

How can I list all files, but only in directories that have no subdirectories?

How can I run a script on resolution change?

How can I get all of my GMail directories to appear in Thunderbird?

How can I convert from uppercase to lowercase all directories and sub-directories in Ubuntu 16.04.2?

How can I recursively find directories and parse them into python script call within bash script?

How to run a loop in all directories inside a directory

How can I use bindkey to run a script?

How can I run executable in shell script?

How can I run a bash script on all files with a json extension in a directory?

How can I run my script on several data directories

How can I run a script after importing it?

How can i run script in firebase automatically

How can I list (and find the number of files) of directories with a bash script?

How can I loop over all directories and subdirectories and find files with a specific extension, then run FFMPEG on them, then copy them to a new lib

How can I make this script run in the background?