Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have several directories that share a common parent directory. In each directory there are regular files, but no other subdirectories. Something like this:

top/dir-1
top/dir-1/file-11
top/dir-1/file-12
top/dir-2
top/dir-2/file-21
top/dir-2/file-22
top/dir-3
top/dir-3/file-31
top/dir-3/file-32

I'd like to run sha1sum on those files and obtain the following checksum files:

top/dir-1/sha1sum
top/dir-2/sha1sum
top/dir-3/sha1sum

top/dir-1/sha1sum, for example, should contain only the checksums for file-11 and file-12:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx file-11
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy file-12

I have tried several commands involving find, sha1sum, cd, a for loop and subshells, but without much success.

Last thing: both directories and files can have spaces in their names, so any solution should consider this.

Any ideas?


Edit. I finally tried with:

cd top
for i in *; do cd "${i}"; (sha1sum * > sha1sum); cd ..; done

It seems to work, but I'm still not sure if it's the right way for doing this kind of job.

share|improve this question
    
@Mat: I've updated the question. –  Francesco Turco Sep 16 '12 at 12:16
add comment

2 Answers 2

up vote 2 down vote accepted

Better as:

cd top &&
for i in */; do (cd -- "$i" && sha1sum -- * > sha1sum); done
share|improve this answer
add comment

A more generic approach (assuming you're already in top):

find . -type f -execdir bash -c 'for f; do sha1sum "${f##*/}" >> sha1sum; done' bash {} +

This should work regardless of your subdir structure and always output the checksum file in the same directory with the checked file(s).

share|improve this answer
    
With your solution I get leading ./ symbols in the content of sha1sum files, second column. –  Francesco Turco Sep 17 '12 at 13:12
    
@FrancescoTurco - fixed, thanks for the heads-up. –  don_crissti Sep 17 '12 at 13:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.