LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2009, 03:59 AM   #1
AutoC
Member
 
Registered: Jan 2008
Posts: 30

Rep: Reputation: 15
bash script automatic file rename if exists


Hi,

I have a folder that has files of the form
1.wav
2.wav
3.wav
|
|
10.wav

Now, I rename these files using a script as
He.wav
was.wav
going.wav
|
|

Now, if a word repeats, It should not overwrite.It should become something like
and.wav
and_1.wav

Is there someway I can do this?
 
Old 09-03-2009, 04:32 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe not exactly what you're looking for, but take a look at the --backup option of mv. You can select --backup=numbered to make a numbered copy of the destination file, if it already exists. However, the format will be:
Code:
and.wav~1~
You can also customize the suffix, for example by putting the current date in the name of the backup:
Code:
mv --backup=simple --suffix=$(date +%Y%m%d%H%M%S) <source> <destination>
but in this case if you want to do a numbered copy you have to deal with the assignment of the correct progressive number and things are more complicate. In other words you cannot use automatic numbered backup and suffix customization at the same time.
 
Old 09-03-2009, 04:40 AM   #3
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
TFT... another nifty tool to stick in my bag of tricks. I really should get out and read the man more often..
 
Old 09-03-2009, 04:46 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Admiral Beotch View Post
TFT... another nifty tool to stick in my bag of tricks. I really should get out and read the man more often..
He he... you never stop learning something new in the *nix world!
 
Old 06-23-2015, 03:31 PM   #5
mastermindx
LQ Newbie
 
Registered: Aug 2014
Posts: 2

Rep: Reputation: Disabled
Just for the new users, you can simply add the time to the filename and it will never repeat:

Code:
cp /srcdir/example.mp4 /destdir/example-`date +%Y%m%d%H%M%S`.mp4
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using if in bash script to check if prog exists karuna-bdc Linux - Newbie 6 06-21-2009 07:23 PM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Print a file if it exists and rename it at the same time. bhar0761 Linux - Enterprise 2 08-01-2005 11:51 PM
Print a file if it exists and rename it at the same time. bhar0761 Linux - General 3 08-01-2005 04:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration