i have variable that i used to represent duplicated files found in a certain directory

ddub=`find $1* -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} find -type f -size {}c -print0 `

I then looped over this directory to compare hard link any duplicates to the original file as well

echo $ddub                              #Print the results

for i in $ddub; do
    for j in $ddub ; do
    if cmp -s "$i" "j" ; then
         ln $i $j
    elif ; 
         echo "csnt hardlink the files"

The problem here is that it tries to hard link every single file in the list which doesn't work as i would like to. I would like to separate the unique files from the rest and then hard link them.

How do i further loop over this so as to hardlink only similar MD5sum files found in the list or files with the same size?

the results i get are as follows which is just the list of all the files i created. please help me figure out how i can seperate them further (note i just created random files)

ln ./test_dir/test_dir2/29.JPG.copy.copy.copy.copy./test_dir/test_dir2/29.JPG.copy.copy.copy./test_dir/test_dir2/29.JPG.copy.copy.copy.copy.copy./test_dir/29.JPG./test_dir/29.JPG.copy./test_dir/29.JPG.copy.copy./test_dir/test_dir2/23.JPG.copy.copy.copy.copy./test_dir/test_dir2/23.JPG.copy.copy.copy.copy.copy./test_dir/09.JPG.copy./test_dir/09.JPG./test_dir/09.JPG.copy.copy./test_dir/39.JPG./test_dir/39.JPG.copy./test_dir/test_dir2/22.JPG.copy.copy.copy.copy./test_dir/test_dir2/22.JPG.copy.copy.copy.copy.copy./test_dir/test_dir2/22.JPG.copy.copy.copy./test_dir/22.JPG./test_dir/22.JPG.copy.copy./test_dir/22.JPG.copy ./test_dir/test_dir2/29.JPG.copy.copy.copy.copy./test_dir/test_dir2/29.JPG.copy.copy.copy./test_dir/test_dir2/29.JPG.copy.copy.copy.copy.copy./test_dir/29.JPG./test_dir/29.JPG.copy./test_dir/29.JPG.copy.copy./test_dir/test_dir2/23.JPG.copy.copy.copy.copy./test_dir/test_dir2/23.JPG.copy.copy.copy.copy.copy./test_dir/09.JPG.copy./test_dir/09.JPG./test_dir/09.JPG.copy.copy./test_dir/39.JPG./test_dir/39.JPG.copy./test_dir/test_dir2/22.JPG.copy.copy.copy.copy./test_dir/test_dir2/22.JPG.copy.copy.copy.copy.copy./test_dir/test_dir2/22.JPG.copy.copy.copy./test_dir/22.JPG./test_dir/22.JPG.copy.copy./test_dir/22.JPG.copy
[asonganyif@ccad2 November_2016]$ vim bashScripass.sh

1 Answer 1


You can't compare files with [ a = b ]. Use cmp:

if cmp -s "$i" "$j"; then
# rest as you were
  • Thank, so is this going to compare similar files amongst the list in the given directory ? and if the output is zero can it just create the hard links then? and possibly delete the rest of the files? Ill like for it to do something like this ...for i in find $ddub -type f; do for j in find $ddub -type f; do if cmp -s "$i" "$j" ; then echo ln $i $j elif ; echo "files are not dupicates" fi done done i appreciate this help ! Jan 27, 2017 at 23:55
  • else, not elif, but possibly should work. Pretty inefficient, looping over both; compares A with B and then B with A, but I'll leave it to you to improve that :)
    – Chris Rees
    May 17, 2017 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .