Arman
Arman

Reputation: 4636

Add md5sum in the ls bash output

I am trying to find files and add their md5sum into a table.

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

How do I add in the ls -l output the md5sum of the file?

I would like to have it output the ls -l and an extra column of md5sum result

E.g.:

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

Upvotes: 7

Views: 2610

Answers (3)

hmontoliu
hmontoliu

Reputation: 4019

This one liner will do what do you want (edit the find search to fit your needs by adding the /store/01 -name "*.fits" -exec chmod -x+r {} \; instead of the . -type f in my example):

$ find . -type f -exec sh -c 'printf "%s %s \n" "$(ls -l $1)" "$(md5sum $1)"' '' '{}' '{}' \;

Example:

/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 

To get the output as you wanted, you can remove the field $8 as follows

/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

Upvotes: 8

rh0dium
rh0dium

Reputation: 7052

How about this?

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

The ls is messing it up and is not needed.

Edit If you "really" want that 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

Steve

Upvotes: 0

Arnaud F.
Arnaud F.

Reputation: 8462

this will work:

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

Upvotes: 3

Related Questions