2011-07-29 41 views
4

我正在嘗試查找文件並將它們的md5sum添加到表中。在ls bash輸出中添加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 

回答

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

+0

謝謝!但我需要把$ 9 =「」來產生我需要的東西。 – Arman

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

史蒂夫

+0

感謝的答覆,我想有一個輸出的ls -l和EXT ra列的md5sum結果。 – 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