0

我遇到了一个问题,我试图在目录中的所有文件夹上运行命令。

例如:PWD 是 /home/user/Documents 我希望我的程序/脚本能够运行

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

等等运行命令:

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

我也希望删除所有没有扩展名 .dmp 的文件

如果有人可以指出我的教程或他们会提供的帮助,我将不胜感激。

4

2 回答 2

2
# 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)
于 2013-07-19T17:46:40.203 回答
1

为了使该命令起作用,您必须位于包含您要递归的目录的顶级目录中。

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
于 2013-07-19T17:46:51.187 回答