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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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


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
up vote 2 down vote accepted

Better as:

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

Assuming you're already in top:

find . -type f -execdir sh -c 'sha1sum -- "${0#*/}" >> sha1sum' {} +

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
What does the "${0##*/}" do? – hft Feb 20 '15 at 22:11
@hft - in this case it's actually "${0#*/}" (corrected my mistake) = parameter expansion, see here; so the shortest match (./) is deleted. – don_crissti Feb 21 '15 at 1:30
Thanks for the explanation. Using "find . -type f -execdir sh -c 'sha1sum -- $0 >> ./sha1sum' {} \;" also seems to work on my system. No leading "./". But maybe that's a Mac thing... – hft Feb 21 '15 at 2:24

Your Answer


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.