且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Shell脚本查找特定文件类型的文件

更新时间:2023-02-08 18:32:00

我假设通过文件类型"pdf","doc","txt",您的意思是带有这些扩展名的文件名.

I assume that by file types "pdf", "doc", "txt", you mean filenames with those extensions.

如果文件类型的数量很少(少于几十个),那么您可以构建一个参数数组,以以下格式传递给 find :

If the number of file types is reasonably small (less than a few dozen), then you could build an array of arguments to pass to find in the format:

... -name '*.pdf' -o -name '*.doc' -o -name '*.txt' ...

假设文件类型数组不为空,这是一种处理方法(感谢 @ mike-holt ):

Assuming that the array of file types is not empty, here's one way to do it (thanks @mike-holt):

arr=(pdf doc txt)

findargs=()

for t in "${arr[@]}"; do
    findargs+=(-name "*.$t" -o)
done

find . -type f \( "${findargs[@]}" -false \)