在ls命令的输出中添加md5sum

7

我正在尝试查找文件并将它们的md5sum添加到表中。

find /store/01 -name "*.fits" -exec chmod -x+r {} \; -exec ls -l {} \; | tee ALL_FILES.LOG

如何在 ls -l 的输出中添加文件的 md5sum 值?

我希望它能够显示 ls -l 并额外显示一个 md5sum 结果的列。

例如:

-rw-r--r-- 1 data user 221790 Jul 28 15:01 381dc9fc26082828ddbb46a5b8b55c03 myfile.fits 
3个回答

8
这个一行代码可以完成你想要的操作(根据你自己的需求,将我的示例中的 . -type f 替换为 /store/01 -name "*.fits" -exec chmod -x+r {} \;):
$ find . -type f -exec sh -c 'printf "%s %s \n" "$(ls -l $1)" "$(md5sum $1)"' '' '{}' '{}' \;

例子:

/etc/samba$ find . -type f -exec sh -c 'printf "%s %s \n" "$(ls -l $1)" "$(md5sum  $1)"' '' '{}' '{}' \; 
-rw-r--r-- 1 root root 8 2010-03-09 02:03 ./gdbcommands 898c523d1c11feeac45538a65d00c838  ./gdbcommands 
-rw-r--r-- 1 root root 12464 2011-05-20 11:28 ./smb.conf 81ec21c32bb100e0855b96b0944d7b51  ./smb.conf 
-rw-r--r-- 1 root root 0 2011-06-27 10:57 ./dhcp.conf d41d8cd98f00b204e9800998ecf8427e  ./dhcp.conf 

为了得到您想要的输出,您可以按以下方式删除字段$8。
/etc/samba$ find . -type f -exec sh -c 'printf "%s %s \n" "$(ls -l $1)" "$(md5sum  $1)"' '' '{}' '{}' \; | awk '{$8=""; print $0}'
-rw-r--r-- 1 root root 8 2010-03-09 02:03  898c523d1c11feeac45538a65d00c838 ./gdbcommands
-rw-r--r-- 1 root root 12464 2011-05-20 11:28  81ec21c32bb100e0855b96b0944d7b51 ./smb.conf
-rw-r--r-- 1 root root 0 2011-06-27 10:57  d41d8cd98f00b204e9800998ecf8427e ./dhcp.conf

HTH


谢谢!但是我需要将$9 =“”才能得到我需要的结果。 - Arman

3

这将起作用:

find /store/01 -name "*.fits" -exec chmod -x+r {} \; \
   | awk '{
line=$0;
cmd="md5sum " $9;
cmd|getline;
close(cmd);
print line, $1;
}' > ALL_FILES.LOG

0

这个怎么样?

find /store/01 -name "*.fits" -exec chmod -x+r {} \; \
   |  xargs -i md5sum {} > ALL_FILES.LOG

ls 命令会搞乱它,而且不需要。

如果你“真的”需要那个 ls

for file in `find /store/01 -name "*.fits"`; do
   chmod -x+r $file; 
   echo -n `ls -l $file` " " ; 
   echo ` md5sum $file | cut -d " " -f 1`; 
done

希望有所帮助

史蒂夫


感谢您的回复,我想要输出 ls -l 命令的结果,并附加一个 md5sum 结果的额外列。 - Arman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接