文件命令108k文件:太多Args

nit*_*s24 0 linux ksh file find

我有一个目录,里面有108k文件.我在RHEL5上使用KSH shell

ls *
-dash_bin_ksh: ls: /bin/ls: cannot execute [Argument list too long]
Run Code Online (Sandbox Code Playgroud)

只有看起来有效的命令才是find命令.

find .
file 1
file 2 
file n
Run Code Online (Sandbox Code Playgroud)

我尝试使用find with then exec选项来运行文件命令,但我没有得到任何结果.

find . -exec file {}
find: missing argument to `-exec'
Run Code Online (Sandbox Code Playgroud)

我错过了什么?我只想在这个目录中的每个文件上运行file命令并输出到file_output.txt

Ala*_*ack 6

对于find的exec,你必须用with结束参数 \;

你也可以尝试:

find . -print0 | xargs -0 file
Run Code Online (Sandbox Code Playgroud)

xargs通过获取其STDIN并将每个元素(行或分隔的字符串)作为参数添加到给定的可执行文件中,以尽可能少地执行.参数列表由--max-chars(平台依赖高达128Kib)拆分成组以供执行.

-print0添加空字符而不是新行,这使得带空格的文件名安全.-0on xargs用于识别空字符.

-print0并且-0是GNU扩展,可以为非GNU环境删除,但代价是多功能性.

xargs还有一个-I选项,使其更像find -exec是为每个元素运行可执行文件的地方.

感谢@glennjackman对这个主题的深入了解.