我需要递归地遍历一个目录,并删除所有扩展名为.pdf和.doc的文件。我设法递归地循环通过一个目录,但不设法过滤与上述文件扩展名的文件。

我目前的代码

#/bin/sh

SEARCH_FOLDER="/tmp/*"

for f in $SEARCH_FOLDER
do
    if [ -d "$f" ]
    then
        for ff in $f/*
        do      
            echo "Processing $ff"
        done
    else
        echo "Processing file $f"
    fi
done

我需要帮助来完成代码,因为我没有得到任何地方。


当前回答

这并不能直接回答你的问题,但是你可以用一句话来解决你的问题:

find /tmp \( -name "*.pdf" -o -name "*.doc" \) -type f -exec rm {} +

一些版本的find (GNU, BSD)有一个-delete操作,你可以使用它来代替调用rm:

find /tmp \( -name "*.pdf" -o -name "*.doc" \) -type f -delete

其他回答

如果你想递归地做一些事情,我建议你使用递归(是的,你可以使用堆栈等来做,但是嘿)。

recursiverm() {
  for d in *; do
    if [ -d "$d" ]; then
      (cd -- "$d" && recursiverm)
    fi
    rm -f *.pdf
    rm -f *.doc
  done
}

(cd /tmp; recursiverm)

也就是说,find可能是一个更好的选择,就像已经建议的那样。

下面的代码将递归地遍历给定目录并列出所有内容:

for d in /home/ubuntu/*; 
do 
    echo "listing contents of dir: $d"; 
    ls -l $d/; 
done

作为mouviciel回答的后续,您还可以将其作为for循环来执行,而不是使用xargs。我经常发现xargs很麻烦,特别是当我需要在每次迭代中做一些更复杂的事情时。

for f in $(find /tmp -name '*.pdf' -or -name '*.doc'); do rm $f; done

正如许多人所评论的那样,如果文件名中有空格,这将失败。您可以通过临时将IFS(内部字段分隔符)设置为换行符来解决这个问题。如果有通配符\[?文件名中的*。您可以通过暂时禁用通配符展开(globbing)来解决这个问题。

IFS=$'\n'; set -f
for f in $(find /tmp -name '*.pdf' -or -name '*.doc'); do rm "$f"; done
unset IFS; set +f

如果文件名中有换行符,那么这也行不通。你最好使用基于xargs的解决方案:

find /tmp \( -name '*.pdf' -or -name '*.doc' \) -print0 | xargs -0 rm

(这里需要转义的括号,以使-print0应用于两个或子句。)

GNU和*BSD find也有一个-delete操作,看起来像这样:

find /tmp \( -name '*.pdf' -or -name '*.doc' \) -delete

我认为最直接的解决方案是使用递归,在下面的例子中,我打印了目录及其子目录中的所有文件名。

您可以根据自己的需要修改。

#!/bin/bash    
printAll() {
    for i in "$1"/*;do # for all in the root 
        if [ -f "$i" ]; then # if a file exists
            echo "$i" # print the file name
        elif [ -d "$i" ];then # if a directroy exists
            printAll "$i" # call printAll inside it (recursion)
        fi
    done 
}
printAll $1 # e.g.: ./printAll.sh .

输出:

> ./printAll.sh .
./demoDir/4
./demoDir/mo st/1
./demoDir/m2/1557/5
./demoDir/Me/nna/7
./TEST

它也适用于空格!

注意: 你可以使用echo $(basename "$i") # print文件名来打印不包含路径的文件名。

或:使用echo ${i%/##*/};#打印运行速度非常快的文件名,不需要调用外部basename。

这个方法很好地处理了空格。

files="$(find -L "$dir" -type f)"
echo "Count: $(echo -n "$files" | wc -l)"
echo "$files" | while read file; do
  echo "$file"
done

编辑,逐个修复

function count() {
    files="$(find -L "$1" -type f)";
    if [[ "$files" == "" ]]; then
        echo "No files";
        return 0;
    fi
    file_count=$(echo "$files" | wc -l)
    echo "Count: $file_count"
    echo "$files" | while read file; do
        echo "$file"
    done
}