我需要递归地遍历一个目录,并删除所有扩展名为.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

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


当前回答

对于bash(自版本4.0起):

shopt -s globstar nullglob dotglob
echo **/*".ext"

这是所有。 拖尾扩展”。在这里选择具有该扩展名的文件(或dirs)。

选项globstar激活**(递归搜索)。 选项nullglob在不匹配文件/dir时删除*。 选项dotglob包含以点开始的文件(隐藏文件)。

注意,在bash 4.3之前,**/也会遍历到目录的符号链接,这是不可取的。

其他回答

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

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

#!/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。

没有理由将find的输出管道到另一个实用程序。Find有一个内置的-delete标志。

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

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

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
}

提供的其他答案将不包括以。开头的文件或目录。下面的方法对我很有效:

#/bin/sh
getAll()
{
  local fl1="$1"/*;
  local fl2="$1"/.[!.]*; 
  local fl3="$1"/..?*;
  for inpath in "$1"/* "$1"/.[!.]* "$1"/..?*; do
    if [ "$inpath" != "$fl1" -a "$inpath" != "$fl2" -a "$inpath" != "$fl3" ]; then 
      stat --printf="%F\0%n\0\n" -- "$inpath";
      if [ -d "$inpath" ]; then
        getAll "$inpath"
      #elif [ -f $inpath ]; then
      fi;
    fi;
  done;
}

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

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

(cd /tmp; recursiverm)

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