-6

I have 3 folders named like FOLDER1, FOLDER2, FOLDER3 and I have couple of files in those folders like wise file1.txt, file2.txt, file3.txt .... etc. ( in those 3 folder don't have sub folders).

In those folders some files are duplicates.

FOLDER1 has file1.txt and FOLDER2 also has file1.txt

OR

FOLDER2 has file1.txt and FOLDER3 has file1.txt

OR

FOLDER1 has file1.txt and FOLDER3 has file 1.txt.

Need to rename another duplicate file1.txt in another folder as file1DUPLICATE.txt.

Suppose if FOLDER1 and FOLDER2 have file1.txt, I need to rename the file1.txt in the FOLDER1 as file1DUPLICATE.txt and retain the file1.txt in the FOLDER2 as original.

If duplicate occurs in FOLDER1 and FOLDER2 I need to rename in the FOLDER1 only.

If Duplicate occurs in the FOLDER2 and FODLER3, I need to rename in the FOLDER3 only.

Simply I dont want to rename the files in the FOLDER2

If duplicate occurs in the FOLDER1 and FOLDER3, I can rename the file.txt whichever folder I want.

Anyone have idea for implementing the script for that ?

Community
  • 1
  • 1
phpjsnerd
  • 3
  • 5
  • if you are using php then use `scandir()` function to check the filenames – prakash tank Feb 23 '17 at 06:08
  • refer the link : http://stackoverflow.com/questions/2059891/php-get-list-of-all-filenames-contained-within-my-images-directory – prakash tank Feb 23 '17 at 06:09
  • 3
    The lack of code and array of tags you've chosen suggests that you're hoping someone will drop a complete program in your lap. Please take a [tour] of SO and learn [ask] good questions to find out why this question will almost certainly be downvoted and closed. – Matt Jacob Feb 23 '17 at 06:23
  • @phpjsnerd: If you want someone to do your work for you then please "passby". Your question isn't wanted here, as so many downvotes and close votes should tell you. – Borodin Feb 23 '17 at 14:46
  • @phpjsnerd I'm afraid you are mistaken in your understanding of how SO works. There is an established set of rules and guidelines that everyone is expected to follow, and when people don't follow those rules, the community responds with downvotes and close votes. – Matt Jacob Feb 23 '17 at 15:20
  • @phpjsnerd Also, you mention someone with @, not # – Matt Jacob Feb 23 '17 at 15:21

1 Answers1

0

You don't show much effort to let us help you solving this. But since I'm learning bash scripting, I took your question as practicing.

So... here is a bash script to rename duplicated files.

/!\ The renamed files will be in the folder of first argument. So, in your case, you'll have to launch it like this :

  • Rename duplicated files from folder1 and folder2 :

    $ mv_duplicates_script.sh /path/to/FOLDER1 /path/to/FOLDER2

  • Rename duplicated files from folder1 and folder3 :

    $ mv_duplicates_script.sh /path/to/FOLDER1 /path/to/FOLDER3

  • Rename duplicated files from folder3 and folder2 :

    $ mv_duplicates_script.sh /path/to/FOLDER3 /path/to/FOLDER2

Note that you have to set FOLDER2 at last argument for the renamed files go to FOLDER3.

#!/bin/bash

# if missing args, print Usage
##############################
if [[ -z $1 ]] || [[ -z $2 ]]
  then
    echo "Usage : `basename $0` /absolute/path/to/folder1 /absolute/path/to/folder2"

    # exit with failed status
    exit 1
fi

# test if folders exist
#######################
if [[ ! -d $1 ]]
  then
    echo "ERROR : Folder $1 not found..."

    exit 1
fi

if [[ ! -d $2 ]]
  then
    echo "ERROR : Folder $2 not found..."

    exit 1
fi

# get filenames from folder 1
#############################

cd $1

i=0
for f in *.txt
  do
    folder1_files[$i]=$f
    ((i++))
done



# get filenames from folder 2
#############################

cd $2

i=0
for f in *.txt
  do
    folder2_files[$i]=$f
    ((i++))
done


# compare and move duplicates
#############################

for f in ${folder1_files[@]}
  do
    for g in ${folder2_files[@]}
      do
        if [[ $f == $g ]]
          then
            echo "Duplicate file : $g"
            echo "Renaiming to DUPLICATE_$g in $1"
            mv $1/$g $1/DUPLICATE_$g
        fi
    done
done

exit 0

Use it as :

$ mv_duplicates_script.sh /absolute/path/to/folder1 /absolute/path/to/folder2

Hope it helps.

PS : This can certainly be improved and your comments/tips are welcome.

JazZ
  • 4,469
  • 2
  • 20
  • 40