![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
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.
|
|
||||
|
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. |
|
|||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|