我需要对大约1500个文件名进行查找,并想知道是否有一种方法可以同时执行多个查找命令。
现在我做的是类似于:
for fil in $(cat my_file)
do
find . -name $fil >> outputfile
done
有没有办法生成多个find实例来加快进程速度。目前每次只能运行一个文件,需要7小时才能完成循环。
tini ~ > time find . -name IMG_0772.JPG -ls
25430459 9504 lrwxrwxrwx 1 omg omg 9732338 Aug 1 01:33 ./pix/rainbow/IMG_0772.JPG
20341373 5024 -rwxr-xr-x 1 omg omg 5144339 Apr 22 2009 ./pc/2009-04/IMG_0772.JPG
22678808 2848 -rwxr-xr-x 1 omg omg 2916237 Jul 21 21:03 ./pc/2012-07/IMG_0772.JPG
real 0m15.823s
user 0m0.908s
sys 0m1.608s
tini ~ > time find . -name IMG_0772.JPG -ls
25430459 9504 lrwxrwxrwx 1 omg omg 9732338 Aug 1 01:33 ./pix/rainbow/IMG_0772.JPG
20341373 5024 -rwxr-xr-x 1 omg omg 5144339 Apr 22 2009 ./pc/2009-04/IMG_0772.JPG
22678808 2848 -rwxr-xr-x 1 omg omg 2916237 Jul 21 21:03 ./pc/2012-07/IMG_0772.JPG
real 0m0.715s
user 0m0.340s
sys 0m0.368s
find
运行得更快,因为操作系统仍然有来自第一个find
的RAM缓冲区。[在我的小型Linux 3.2.0-32系统上,根据top
目前有2.5GB的RAM用于缓存,0.3GB是空闲的,3.8GB正在使用(即大约1.3GB用于程序和操作系统)。]locate
命令。查询time locate IMG_0772.JPG
updatedb
。请参见man updatedb
。如果find
中的目录“.”仅占整个文件系统的一小部分,以至于locate
数据库包括许多不相关的文件,请在运行updatedb
时使用各种prune
选项,以最小化在运行locate
时访问的locate
数据库的大小;之后,运行一个普通的updatedb
将其他文件名恢复到locate
数据库中。使用locate
,您可能可以将运行时间缩短到20分钟。find . \( -name file1 -o -name file2 -o ... \) >outputfile
根据 my_file
中名称的数量,您可以构建此类行:
find . \( $(xargs <my_file printf "-name %s -o " | sed 's/-o $//') \) >outputfile
find
和fgrep
命令:find . | fgrep -f my_file > outputfile
我假设my_file
是一个包含你要查找的文件列表的文件,每个文件名都在单独的一行上。
find
命令查找当前目录中的所有文件(包括目录)。它的输出是一个文件/目录列表,每行一个。fgrep
命令从find命令的输出中搜索,但不像在命令行上指定搜索词,它从my_file
获取搜索词--这就是-f
标志的作用。outputfile
locate
。 - Michael Piefel