0

So by using some info from other threads on this site I have managed to get a script to copy files to another directory, then add the md5sum into the duplicate file names. However, I'm not fully satisfied as I'm still receiving a couple of BASH notifications when I execute the script which I could do with some help understanding/resolving. Firstly here is the script.

cp -r $dir1/* $dir2 cd $dir2 fdupes -r $dir2 | while read i; do bn="${i%.*}" ext="${i##*.}" md5=$(md5sum "$i" | awk '{ print $1 }') mv -v "$i" "${bn}_${md5}.${ext}" done 

The messages I am receiving when the script is run are:

md5sum: : No such file or directory mv: cannot stat ‘’: No such file or directory 

As I say, the script does seem to work, but any ideas on why I receiving these messages would be appreciated, as would any suggestion to tweak/improve the script. Thanks

1 Answer 1

0

From my understanding, i guess there is an issue with fdupes command which output an empty line at the end :

[PRD][]localhost:~ 12:17:09 $ fdupes -r work/ work/sockperf-sockperf_v2/news work/sockperf-sockperf_v2/src/.dirstamp work/sockperf-sockperf_v2/src/.deps/.dirstamp [PRD][]localhost:~ 12:17:19 

Then while processing in the loop, the last element is empty which produces an error in md5sum like when file is nonexistant :

$ md5sum ' ' md5sum: : no such file of directory 

And then the script tries to move this empty name file which produces also the mv error.

4
  • 1
    Immediately below the while read i; do, add this line test -z "$i" && continue Commented Oct 13, 2015 at 10:38
  • roaima- thanks that worked a treat :)))) Don't suppose you could explain why that worked and your logic could you? No worries if not, you've already helped me lots. Commented Oct 13, 2015 at 12:04
  • test -z $i will test if Variable i is empty. If not the && continue will continue to loop into the while. Don't forget to accept the answer if you are happy with the outcome. Commented Oct 13, 2015 at 12:08
  • 1
    thanks netmonk, that makes your original answer more understandable to a noob like me, cheers. I've accepted the original answer now I understanding it more. Commented Oct 13, 2015 at 13:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.