据我所知,没有选项告诉find
您从文件中读取模式。一个简单的解决方法是将要排除的模式保存在文件中,然后将该文件作为反向输入传递grep
。例如,我创建了以下文件和目录:
$ tree -a
.
├── a
├── .aa
├── .aa.bak
├── a.bck
├── b
├── .dir1
│ └── bb1.bak
├── dir2
│ └── bb2.bak
├── b.bak
├── c
├── c~
├── Documents
│ └── Documents.bak
├── exclude.txt
├── foo.backup
└── Music
└── Music.bak
如果我理解你正确地张贴的例子,你要移动a.bck
,.aa.bak
,b.bak
,c~
,foo.backup
和dir2/bb2.bak
垃圾桶,休假.aa.bak
,.dir1/bb1.bak
,Documents/Documents.bak
和Music/Music.bak
他们在哪里。因此,我创建了exclude.txt
具有以下内容的文件(您可以根据需要添加任意数量):
$ cat exclude.txt
./.*/
./Music
./Documents
我./.*/
之所以使用,是因为我理解您的原始发现意味着您要移动.foo
当前目录中的隐藏备份文件(),但不包括隐藏目录(.foo/bar
)中的所有备份文件。因此,我现在可以运行find
命令并用于grep
排除不需要的文件:
$ find . -type f | grep -vZf exclude.txt | xargs -0 --no-run-if-empty trash-put
Grep选项:
-v, --invert-match
Invert the sense of matching, to select non-matching
lines. (-v is specified by POSIX.)
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty
file contains zero patterns, and therefore matches
nothing. (-f is specified by POSIX.)
-Z, --null
Output a zero byte (the ASCII NUL character) instead of
the character that normally follows a file name. For
example, grep -lZ outputs a zero byte after each file
name instead of the usual newline. This option makes
the output unambiguous, even in the presence of file
names containing unusual characters like newlines.
This option can be used with commands like find
-print0, perl -0, sort -z, and xargs -0 to process
arbitrary file names, even those that contain newline
characters.