I have a directory tree with many .dat files, where many of them contain special characters. They are used by another process which is only able to process files containing ascii characters.

My idea was to find all the files, build a new file name based on the md5 hash of their file path, and copy them to a new directory (they aren't very large, and I may not alter the originals).

This is what I've tried

find dat -type f -name "*.dat" -print0|xargs -0 -I file cp 'file' "datnew/$(echo file|md5).dat"

Unfortuntately it only builds a single hash, and copy all files to that output file name:

$ touch dat/foo.dat
$ touch dat/bar.dat
$ find dat -type f -name "*.dat" -print0|xargs -0 -I file cp 'file' "datnew/$(echo file|md5).dat"
$ ls datnew

Can you please tell me what my mistakte is? Am I using xargsin a wrong way?

  • what is the output of echo file | md5? It should be bbe02f946d5455d74616fc9777557c22 as file will not be interpreted in $( echo file | md5 ) – Fiximan Aug 12 '15 at 10:15
  • @Fiximan yes, please see the last line of my result. That is exactly how the results looks. Is there any way to get the file there to be replaced as well? – muffel Aug 12 '15 at 10:17
  • You will need to use -n1 to the xargs command. – Lambert Aug 12 '15 at 10:34

As file in $( echo $file | md5 ) will not be interpreted, you need a workaround.

One possibility is to simply pipe it into a while loop and read each output - in that case better skip xargs as a whole

find ... | while read file ; do cp "$file" "datnew/$( echo "$file" | md5 )" ; done

For using it with -print0 replace the null character with a newline using tr

find ... -print0 | tr '\000\' '\n' | while read ; do ... ; done
  • this does not seem to work with -print0 of find, does it? I need this option because of some of the special characters – muffel Aug 12 '15 at 10:40
  • replace the null character by a newline with with tr : find -print0 | tr '\000' '\n' | while read ... – Fiximan Aug 12 '15 at 10:45

Your Answer

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

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