Bash Script duplicate file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script duplicate file names
# 1  
Old 07-11-2007
Bash Script duplicate file names

I am trying to write a housekeeping bash script. Part of it involves searching all of my attached storage media for photographs and moving them into a single directory. The problem occurs when files have duplicate names, obviously a file called 001.jpg will get overwritten with another file called 001.jpg. What I am trying to do is to get the script to rename a file with a duplicate filename on the fly, so that the name of the file with the duplicate name will be (for instance) 001[1].jpg the next file with the same file name will be 001[2].jpg etc etc.

So, if I have a line that reads:

Code:
 find / -name 000*.jpg

How can I get the script to modify the filename on the fly if a file with the same name already exists in my target directory?

Thanks in advance!
# 2  
Old 07-11-2007
To have each file with an unique name, regardless:
Code:
typeset -i mCnt=1
for mFile in `find / -name 000*.jpg`
do
  mFirstPart=`echo $mFile | sed 's/\.jpg//'`
  mOutFile=${mFirstPart}'_'${mCnt}'.jpg'
  mCnt=${mCnt}+1
done

# 3  
Old 07-11-2007
Thanks for the reply Shell_Life. It seems that your solution re-names each file in each directory, however, it still creates duplicate file names across directories. For instance, my first directory contains a file called 0000004.jpg...but so does my 10th and 13th directory. I can see from your code what it should be doing, it seems okay to me...I can't figure out why it isn't working as expected.
# 4  
Old 07-11-2007
Stumpyuk,
Run the following and you will what it is doing:
Code:
typeset -i mCnt=1
for mFile in `find / -name 000*.jpg`
do
  mFirstPart=`echo $mFile | sed 's/\.jpg//'`
  mOutFile=${mFirstPart}'_'${mCnt}'.jpg'
  echo "New file = "${mOutFile}
  mCnt=${mCnt}+1
done

# 5  
Old 07-11-2007
Thanks again, Shell_Life

When executed, the script prints a number of lines similar to:

New file = /home/usr/photos/00000021_254.jpg

However, when I go to the /home/usr/photos directory, the corresponding picture is still named '00000021.jpg' , none of the files have the expected '_##' suffix i.e they haven't actually been renamed.

I have tried adding the following line to your suggested code just before the "done" invocation:

Code:
mv $mFile output_dir

However, all of the jpgs still have the format "0000001.jpg" etc. Therefore files with duplicate names have been overwritten.
# 6  
Old 07-11-2007
Stumpyuk,
I just wrote the basic script.
To rename the files, use the following:
Code:
typeset -i mCnt=1
for mFile in `find / -name 000*.jpg`
do
  mFirstPart=`echo $mFile | sed 's/\.jpg//'`
  mOutFile=${mFirstPart}'_'${mCnt}'.jpg'
  echo "New file = "${mOutFile}
  mv ${mFile} ${mOutFile}
  mCnt=${mCnt}+1
done

# 7  
Old 07-11-2007
It works great...thanks very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Homework & Coursework Questions

Bash script for printing folder names and their sizes

1. The problem statement, all variables and given/known data: The task is to create a script that would reproduce the output of 'du' command, but in a different way: what 'du' does is: <size> <folder name>and what is needed is <folder name> <size>We need to show only 10 folders which are the... (3 Replies)
Discussion started by: UncleIS
3 Replies

3. Shell Programming and Scripting

Bash script for printing folder names and their sizes

Good day, everyone! I'm very new to bash scripting. Our teacher gave us a task to create a script that basically does the same job the 'du' command does, with the difference that 'du' command gives an output in the form of <size> <folder name>and what we need is <folder name> <size>As for... (1 Reply)
Discussion started by: UncleIS
1 Replies

4. Shell Programming and Scripting

Using awk to append incremental numbers to the end of duplicate file names.

I'm currently working on a script that extracts files from a .zip, runs an sha1sum against them and then uses awk to pre-format them into zomething more readable thusly: Z 69 89e013b0d8aa2f9a79fcec4f2d71c6a469222c07 File1 Z 69 6c3aea28ce22b495e68e022a1578204a9de908ed File2 Z 69... (5 Replies)
Discussion started by: Ethereal
5 Replies

5. Shell Programming and Scripting

bash keep only duplicate lines in file

hello all in my bash script I have a file and I only want to keep the lines that appear twice in the file.Is there a way to do this? thanks in advance! (4 Replies)
Discussion started by: vlm
4 Replies

6. Shell Programming and Scripting

Find the number of non-duplicate names recursively.

Hi, here comes another newbie question: How to find the number of non-duplicate names recursively? For example, my files are stored in the folders like: If I do find . -depth -name "*.txt" | wc -l This will gives out a result "4". One .txt file named "1.txt" in folder "1", and... (2 Replies)
Discussion started by: jiapei100
2 Replies

7. Shell Programming and Scripting

duplicate index names renamed

Hello everyone ! Please have a minute and see if you know how to script this I have a file like this: "create table .... ... create index n112 on ... ... create table ... .... create index n113 on... ... create table ... create index n112 on ...! duplicate ... (1 Reply)
Discussion started by: sotoc79
1 Replies

8. UNIX for Dummies Questions & Answers

Some questions - renaming duplicate names

I have a file that looks like this 2 4 10 500 tim9 5 8 14 700 tim9 3 5 15 432 john1 1 4 12 999 ellen2 So basically what i want to do is fine duplicate names on column 5 and rename it with an extention (i.e. tim9_1 and tim9_2). so the output file will look like this 2 4 10 500 tim9_1... (1 Reply)
Discussion started by: kylle345
1 Replies

9. Shell Programming and Scripting

How to : Find duplicate number from file? with bash

Thanks AVKlinux (6 Replies)
Discussion started by: avklinux
6 Replies

10. UNIX for Advanced & Expert Users

Processing extended ascii character file names in UNIX (BASH scipts)

Hi, I have a accentuated letter (ö) in a script for an Installer. It's a file name. This is not working and I'm told to try using the octal value for the extended ascii character. Does anyone no how to do this? If I had the word "filförval", can I just put in the value between the letters, like... (9 Replies)
Discussion started by: peli
9 Replies
Login or Register to Ask a Question