UNIX查找找到不以特定扩展名结尾的文件名?

时间:2009-08-27 14:24:46

标签: command-line find

是否有一种简单的方法可以递归查找目录层次结构中的所有文件,在扩展列表中结束?例如。所有不是* .dll或* .exe

的文件

UNIX / GNU find,功能强大,似乎没有exclude模式(或者我错过了它),而且我总是发现很难用正则表达式来查找东西匹配特定表达式。

我在Windows环境中(使用大多数GNU工具的GnuWin32端口),因此我同样适用于仅限Windows的解决方案。

8 个答案:

答案 0 :(得分:308)

或者没有(并且需要逃避它:

find . -not -name "*.exe" -not -name "*.dll"

并且还要排除目录列表

find . -not -name "*.exe" -not -name "*.dll" -not -type d

或以正逻辑形式; - )

find . -not -name "*.exe" -not -name "*.dll" -type f

答案 1 :(得分:43)

find . ! \( -name "*.exe" -o -name "*.dll" \)

答案 2 :(得分:8)

$ find . -name \*.exe -o -name \*.dll -o -print

前两个-name选项没有-print选项,因此它们被跳过。其他一切都打印出来。

答案 3 :(得分:4)

您可以使用grep命令执行某些操作:

find . | grep -v '(dll|exe)$'

-v上的grep标志具体表示“找到匹配此表达式的内容。”

答案 4 :(得分:2)

还有一个: - )

$ ls -ltr
total 10
-rw-r--r--    1 scripter     linuxdumb         47 Dec 23 14:46 test1
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:40 test4
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:40 test3
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:40 test2
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file5
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file4
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file3
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file2
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file1
$ find . -type f ! -name "*1" ! -name "*2" -print
./test3
./test4
./file3
./file4
./file5
$

Unix find command reference

答案 5 :(得分:1)

Linux / OS X:

从当前目录开始,递归查找以.dll或.exe

结尾的所有文件
find . -type f | grep -P "\.dll$|\.exe$"

从当前目录开始,以递归方式查找所有不以.dll或.exe结尾的文件

find . -type f | grep -vP "\.dll$|\.exe$"

注意:

(1)grep中的P选项表示我们使用Perl样式编写正则表达式以与 grep 命令一起使用。为了将 grep 命令与正则表达式结合使用,我发现Perl样式是最强大的样式。

(2)grep中的v选项指示shell排除满足正则表达式的任何文件

(3)结尾处的$字符说“.dll $”是一个分隔符控制字符,告诉shell文件名字符串以“.dll”结尾

答案 6 :(得分:1)

find  /data1/batch/source/export   -type f -not  -name "*.dll" -not -name "*.exe"

答案 7 :(得分:0)

如果你有很长的扩展名列表,那么这个页面上的其他解决方案是不可取的 - 维持一长串-not -name 'this' -not -name 'that' -not -name 'other'会很繁琐且容易出错 - 或者如果搜索是程序化的并且列表扩展是在运行时构建的。

对于这些情况,可能需要更清楚地将数据(扩展列表)和代码(参数设置为find)分开的解决方案。给定一个目录&文件结构如下所示:

.
└── a
    ├── 1.txt
    ├── 15.xml
    ├── 8.dll
    ├── b
    │   ├── 16.xml
    │   ├── 2.txt
    │   ├── 9.dll
    │   └── c
    │       ├── 10.dll
    │       ├── 17.xml
    │       └── 3.txt
    ├── d
    │   ├── 11.dll
    │   ├── 18.xml
    │   ├── 4.txt
    │   └── e
    │       ├── 12.dll
    │       ├── 19.xml
    │       └── 5.txt
    └── f
        ├── 13.dll
        ├── 20.xml
        ├── 6.txt
        └── g
            ├── 14.dll
            ├── 21.xml
            └── 7.txt

您可以这样做:

## data section, list undesired extensions here
declare -a _BADEXT=(xml dll)

## code section, this never changes
BADEXT="$( IFS="|" ; echo "${_BADEXT[*]}" | sed 's/|/\\|/g' )"
find . -type f ! -regex ".*\.\($BADEXT\)"

结果是:

./a/1.txt
./a/b/2.txt
./a/b/c/3.txt
./a/d/4.txt
./a/d/e/5.txt
./a/f/6.txt
./a/f/g/7.txt

您可以在不更改代码块的情况下更改扩展名列表。

注意不适用于原生OSX find - 请改用gnu find。

相关问题