我不确定您是否可以与find
. 但是,您可以尝试以下操作:
第 1 步:通过重定向命令的输出来创建文件find
。
$ ls -lart
total 0
drwxr-xr-x 12 jaypalsingh staff 408 Jun 19 00:53 ..
drwxr-xr-x 2 jaypalsingh staff 68 Jun 19 00:53 .
$ touch .help .yeah .notinmydictionary
$ ls -lart
total 0
drwxr-xr-x 12 jaypalsingh staff 408 Jun 19 00:53 ..
-rw-r--r-- 1 jaypalsingh staff 0 Jun 19 00:54 .yeah
-rw-r--r-- 1 jaypalsingh staff 0 Jun 19 00:54 .notinmydictionary
-rw-r--r-- 1 jaypalsingh staff 0 Jun 19 00:54 .help
drwxr-xr-x 5 jaypalsingh staff 170 Jun 19 00:54 .
$ find . \( -iname '*~' -o -iname '.*' \) -type f > myhiddenfile.list
$ cat myhiddenfile.list
./.help
./.notinmydictionary
./.yeah
第二步:运行以下awk
命令
$ awk -F'/' 'NR==FNR{a[substr($NF,2)]=$0;next}{for(x in a){if(x == $1)system("rm \-i "a[x])}}' myhiddenfile.list /usr/share/dict/words
remove ./.help? y
remove ./.yeah? y
$ ls -lart
total 8
drwxr-xr-x 12 jaypalsingh staff 408 Jun 19 00:53 ..
-rw-r--r-- 1 jaypalsingh staff 0 Jun 19 00:54 .notinmydictionary
-rw-r--r-- 1 jaypalsingh staff 37 Jun 19 00:54 myhiddenfile.list
drwxr-xr-x 4 jaypalsingh staff 136 Jun 19 01:07 .
命令解释awk
:
# Set the field separator to "/"
awk -F'/' '
NR==FNR {
# Slurp your hidden file list in to an array (index is filename;value is complete path)
a[substr($NF,2)]=$0
# Keep doing this action until all files are stored in array
next
}
{
# Iterate over the array
for(x in a) {
# If the filename matches a word in your dictionary
if(x == $1) {
# execute the rm command to delete it
system("rm \-i "a[x])
}
}
}' myhiddenfile.list /usr/share/dict/words
注意:这适用于没有扩展名的文件名。对于带有扩展名.help.txt
的文件名,需要额外的步骤来解析文件名,以便对字典执行查找。