查找文件并打印信息

查找文件并打印信息

我可以轻松找到名称中具有给定字符串的文件:

me@comp:/usr/local/hydra/hydra-7.4.2$ find -D stat -name "*hack*"
./hack881663129.txt
./hack881663129_7.txt
./hack881663129_5.txt
./hack881663129_4.txt
./hack881663129_4_7.txt
./hack881663129_6.txt
./hack881663129_1_6.txt
./hack881663129_8.txt

我还如何能打印文件详细信息,如大小、创建日期等?

答案1

只需使用-printf带有适当参数的参数:

$ find -name "*hack*" -printf '%m %p %a\n'
644 ./hack881663129.txt Sat Feb 16 02:27:16.0189270840 2013
644 ./hack881663129_7.txt Sat Feb 16 05:30:12.0673185691 2013
644 ./hack881663129_5.txt Sat Feb 16 05:24:57.0441188136 2013
644 ./hack881663129_4.txt Sat Feb 16 05:22:21.0209189346 2013
664 ./hack881663129_4_7.txt Wed Feb 20 11:09:49.0786644191 2013
644 ./hack881663129_6.txt Sat Feb 16 05:26:49.0297187267 2013
664 ./hack881663129_1_6.txt Mon Feb 18 11:40:05.0991189262 2013
644 ./hack881663129_8.txt Sat Feb 16 05:31:37.0689185031 2013

man find并搜索-printf其他占位符。

答案2

对找到的每个文件执行 stat:

find . -name '*hack*' -exec stat {} \;

同样的,使用xargs:

find . -name '*hack*' | xargs stat

更好的是,当文件名中有分隔符时:

find . -name '*hack*' -print0 | xargs -0 stat

答案3

来自此问答:如何使定位输出看起来像“ll”或“ls -la”但更漂亮?考虑使用locate比 快得多的命令find。我有一个小脚本可以调用它并设置标题和stat信息以提供我认为您想要的格式:

$ time llocate zhack
ACCESS      OWNER  GROUP  SIZE  MODIFIED    NAME (updatdb last ran: 2018-05-22 20:45:05)
drwxr-xr-x  root   root   4096  2018-05-17  /usr/src/linux-headers-4.4.0-124/zfs/cmd/zhack
-rw-r--r--  root   root   6     2018-05-02  /usr/src/linux-headers-4.4.0-124/zfs/cmd/zhack/Makefile.in
drwxr-xr-x  root   root   4096  2018-05-22  /usr/src/linux-headers-4.4.0-127/zfs/cmd/zhack
-rw-r--r--  root   root   6     2018-05-19  /usr/src/linux-headers-4.4.0-127/zfs/cmd/zhack/Makefile.in

real    0m0.661s
user    0m0.665s
sys     0m0.003s

请参阅链接以获取llocate脚本。

相关内容