The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Processing extended ascii character file names in UNIX (BASH scipts) peli UNIX for Advanced & Expert Users 9 04-06-2008 06:17 PM
how to extract paragraphs from file in BASH script followed by prefix ! , !! and !!! nabmufti Shell Programming and Scripting 6 02-09-2008 08:32 PM
bash - batch script for extracting one file from multiple tar files kuliksco Shell Programming and Scripting 4 11-13-2007 02:34 AM
Script for file names/sizes ssmiths001 Shell Programming and Scripting 2 05-09-2006 06:55 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 05:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-11-2007
stumpyuk stumpyuk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 10
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 (permalink)  
Old 07-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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 (permalink)  
Old 07-11-2007
stumpyuk stumpyuk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 10
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 (permalink)  
Old 07-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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 (permalink)  
Old 07-11-2007
stumpyuk stumpyuk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 10
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 (permalink)  
Old 07-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0