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

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


当前回答

如果可以更改用于运行该命令的shell,则可以使用ZSH来完成该工作。

#!/usr/bin/zsh

for file in /tmp/**/*
do
    echo $file
done

这将递归循环遍历所有文件/文件夹。

其他回答

只做

find . -name '*.pdf'|xargs rm

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

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

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

下面的函数将递归遍历\home\ubuntu目录(ubuntu下的整个目录结构)中的所有目录,并在else块中应用必要的检查。

function check {
        for file in $1/*      
        do
        if [ -d "$file" ]
        then
                check $file                          
        else
               ##check for the file
               if [ $(head -c 4 "$file") = "%PDF" ]; then
                         rm -r $file
               fi
        fi
        done     
}
domain=/home/ubuntu
check $domain

作为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/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;
}