【问题标题】:Add md5sum in the ls bash output在 ls bash 输出中添加 md5sum
【发布时间】:2011-07-29 12:41:05
【问题描述】:

我正在尝试查找文件并将它们的 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 

【问题讨论】:

    标签: bash shell find


    【解决方案1】:

    这一个班轮会做你想做的(编辑查找搜索,通过在我的示例中添加/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

    【讨论】:

    • 谢谢!!但我需要投入 $9="" 来生产我需要的东西。
    【解决方案2】:

    这将起作用:

    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
    

    【讨论】:

      【解决方案3】:

      这个怎么样?

      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

      史蒂夫

      【讨论】:

      • 感谢回复,我想输出 ls -l 和额外的 md5sum 结果列。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多