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 need to create an md5 hash of every directory and file inside of one main directory. The only thing that is keeping me from success is figuring out a way around files with a space in the path.

I am using find for the recursive listing (I have read that find is the best way of doing this):

c5-26-1# find /root/newdir
/root/newdir
/root/newdir/1
/root/newdir/2
/root/newdir/3
/root/newdir/4
/root/newdir/5
/root/newdir/newdir2
/root/newdir/newdir2/1
/root/newdir/newdir2/2
/root/newdir/new
/root/newdir/dir
/root/newdir/new dir

When I try this with md5 I get two different outcomes, neither of which work:

c5-26-1# md5 $(find /root/newdir) # same outcome using for loop
MD5 (/root/newdir) = bc79a580f6c932937f6fcd454747db72
MD5 (/root/newdir/1) = 94ca98295946310ce88e185ea57486d5
MD5 (/root/newdir/2) = 8432051f64459be5a5e73dc2abd91795
MD5 (/root/newdir/3) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/4) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/5) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/newdir2) = 722165901468b9596dbdddfe118759fb
MD5 (/root/newdir/newdir2/1) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/newdir2/2) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/new) = 89d042c5f9d6ba485a654b543685ea86
MD5 (/root/newdir/dir) = 148538718feba14839f5d1072854c5f4
MD5 (/root/newdir/new) = 89d042c5f9d6ba485a654b543685ea86
MD5 (dir) = 148538718feba14839f5d1072854c5f4

or

c5-26-1# find -X /root/newdir | xargs md5
find: /root/newdir/new dir: illegal path
MD5 (/root/newdir) = bc79a580f6c932937f6fcd454747db72
MD5 (/root/newdir/1) = 94ca98295946310ce88e185ea57486d5
MD5 (/root/newdir/2) = 8432051f64459be5a5e73dc2abd91795
MD5 (/root/newdir/3) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/4) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/5) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/newdir2) = 722165901468b9596dbdddfe118759fb
MD5 (/root/newdir/newdir2/1) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/newdir2/2) = d41d8cd98f00b204e9800998ecf8427e
MD5 (/root/newdir/new) = 89d042c5f9d6ba485a654b543685ea86
MD5 (/root/newdir/dir) = 148538718feba14839f5d1072854c5f4

How do I account for directories with spaces?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

xargs is rarely useful, because it expects input quoted in a highly peculiar way that no common tool produces. And as you've noticed mycommand $(find …) is no good, because it first concatenates all the file names and then splits at whitespace.

Use the -exec primary of find to make it execute md5 with no intervening shell that would require quoting. If your implementation of find is reasonably current, then you can make it do what xargs is famous for, which is to invoke the md5 program once per batch of arguments.

find /root/newdir -type f -exec md5 {} +

If your find doesn't support -exec … {} +, replace the + by \;. This makes find invoke md5 for each file in turn. It's slightly slower, but available everywhere.

share|improve this answer

If you use the -exec switch to find I think that will automatically escape it for you.

share|improve this answer

Why not just use the md5deep tool that does exactly this? Most Linux distributions include it, usually in a package called md5deep.

share|improve this answer
1  
I am working on with a FreeBSD variant, I don't have the option to install anything that isn't already on it and md5deep is not installed. –  MaQleod Nov 5 '11 at 2:22

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.