简体   繁体   中英

Shell scripting in nested directories

I'm having an issue where I am trying to run commands on all folders within a directory.

for instance: PWD is /home/user/Documents I want my program/script to work through

/home/user/Documents
/home/user/Documents/directory1
/home/user/Documents/directory1/foo
/home/user/Documents/directory2
/home/user/Documents/directory2/bar

etcetera running the command:

tranalyzer -r <file in folder> -w OUT_<name of folder>

I'm also hoping to delete all files which DON'T have the extension .dmp

If there a tutorial someone could point me to or assistance they would offer, I would greatly appreciate it.

# iterate over a NUL-delimited stream of directory names
while IFS='' read -r -d '' dirname; do
  # ...then list files in each directory:
  for file in "$dirname"/*; do
    # ignore directory contents that are not files
    [[ -f $file ]] || continue
    # delete files which do not have the .dmp extension
    if [[ $file != *.dmp ]]; then
      rm -f "$file"
      continue
    fi
    # run analysis tool
    tranalyzer -r "$file" -w "OUT_${dirname##*/}"
  done
done < <(find . -type d -print0)

In order for this command to work you must be in the top directory containing the directories you'd like to recurse over.

set -f
OIFS="$IFS"
IFS=$'\n' 
for file in `find . -type f`;
do
     tranalyzer -r "$file" -w OUT_<name of folder>
done
IFS="$OIFS"
set +f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM