在ls bash输出中添加md5sum

时间:2011-07-29 12:41:05

标签: bash shell find

我正在尝试查找文件并将其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 个答案:

答案 0 :(得分:6)

这一个班轮可以满足您的需求(通过在我的示例中添加/store/01 -name "*.fits" -exec chmod -x+r {} \;而不是. -type f来编辑查找搜索以满足您的需求):

$ 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

答案 1 :(得分: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

答案 2 :(得分: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

HTH

史蒂夫