Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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:

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

Better as:

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

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.