有没有一种简单的方法来递归查找目录层次结构中的所有文件, 不以扩展名列表结束? 如不在的* .dll或* .EXE的所有文件
UNIX / GNU发现,强大,因为它是,似乎并不具有exclude
模式(或者说我失踪了),我一直觉得很难用正则表达式找到的东西不匹配特定的表达。
我是在Windows环境中(使用的GnuWin32大部分GNU工具端口),所以我同样打开Windows的唯一解决方案。
有没有一种简单的方法来递归查找目录层次结构中的所有文件, 不以扩展名列表结束? 如不在的* .dll或* .EXE的所有文件
UNIX / GNU发现,强大,因为它是,似乎并不具有exclude
模式(或者说我失踪了),我一直觉得很难用正则表达式找到的东西不匹配特定的表达。
我是在Windows环境中(使用的GnuWin32大部分GNU工具端口),所以我同样打开Windows的唯一解决方案。
或没有(
和需要逃避它:
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
find . ! \( -name "*.exe" -o -name "*.dll" \)
$ find . -name \*.exe -o -name \*.dll -o -print
前两个选项-name没有-print选项,所以他们跳过。 一切是打印。
你可以使用grep命令做一些事情:
find . | grep -v '(dll|exe)$'
该-v
旗grep
具体意思是“发现事情不这个表达式匹配。”
多一个 :-)
$ 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命令参考
find /data1/batch/source/export -type f -not -name "*.dll" -not -name "*.exe"
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命令一起使用。 对于与正则表达式结合excecuting grep命令的目的,我发现Perl的风格是最有力的风格各地。
(2)在grep的v选项指示壳排除满足正则表达式的任何文件
(3)在说的端$字符“.dll文件$”是告诉该文件名的字符串用“的.dll”结束该壳的定界符控制字符
此页面上的其他解决方案都是不可取的,如果你有扩展的一个长长的清单-保持很长的序列-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找到代替。