2

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.

1
  • @Mat: I've updated the question. – user22304 Sep 16 '12 at 12:16
2

Better as:

cd top &&
for i in */; do (cd -- "$i" && sha1sum -- * > sha1sum); done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy