LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-28-2013, 05:33 AM   #1
Jhinukk
LQ Newbie
 
Registered: Jan 2012
Posts: 8

Rep: Reputation: Disabled
Linux Rename


Hi,

I have 2 files. Which looks like

1st file -------------- 2nd file
-------------- ----------------
ABC_old.txt ------------ ABC_new.txt
XYZ_old.txt ------------ XYZ_new.txt

Now I have to use 'mv' command using this files in my script such as it will come like,

mv ABC_old.txt ABC_new.txt
mv XYZ_old.txt XYZ_new.txt

TIA,
Jhinukk

Last edited by Jhinukk; 03-28-2013 at 05:35 AM.
 
Old 03-28-2013, 05:40 AM   #2
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
and what exactly is the function of all those dashes? Are they always the same amount of dashes?

If the amount of dashes differs, you could approach it like this

Code:
while read line
do
OLDNAME=$(echo $line | cut -d ' ' -f 1)
NEWNAME=$(echo $line | rev | cut -d ' ' -f 1 | rev)
mv ${OLDNAME} ${NEWNAME}
done
This is provided that there are never spaces in the filenames and is pretty quick and dirty.
 
Old 03-28-2013, 06:16 AM   #3
Jhinukk
LQ Newbie
 
Registered: Jan 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks Ramurd . It solved my problem.
 
Old 03-28-2013, 06:47 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
1) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

2) There have been many, many previous threads on how to bulk rename files in the shell. A bit of searching through the archives would certainly have turned up the answers you needed. We'd be here afterwards if you still needed help.


But for my own suggestion, I'm assuming that the OP just has two files, with each line in the first file having the old name, and with the second file holding a corresponding new name.

For that I'd recommend a couple of simple bash arrays:

Code:
mapfile -t oldname <file1.txt
mapfile -t newname <file2.txt

for i in "${!oldname[@]}"; do
    echo mv "${oldname[i]}" "${newname[i]}"
done
It also assumes one line per name pair, with none of the names containing newlines themselves, and that the filenames either exist in the current directory or have absolute paths.

I added an echo to the front of mv so you can confirm the commands before actually executing the changes. Just remove it when ready.
 
Old 03-28-2013, 06:48 AM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
You're welcome;

Please mark the thread as solved if it did solve your issue...

And to make things a bit easier, I can explain what the snippet does:

Code:
while read line
do
done < ${INPUTFILE}
This part walks throug the file, line by line.

Code:
OLDNAME=$(echo $line | cut -d ' ' -f 1)
Split the line on spaces and take the first word; The weakness is that if the filename contains a space, the cutting is done on that space.

Code:
NEWNAME=$(echo $line | rev | cut -d ' ' -f 1 | rev)
This one takes the same line, but prints it in reverse (rev), cuts it on a space (again) and takes the first word (since the line is in reverse, this is actually the last "word"); Of course, we have to reverse the reverse, so things are in the correct order: that's what the second "rev" does.
 
Old 03-28-2013, 07:28 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Well that's a lot of trouble. Why not just use the shell's built-in parameter substitutions?

Code:
oldname=${line%% *}
newname=${line##* }
Another option is to use read itself (assuming bash's array support).

Code:
While read -ra line; do

     oldname=${line[0]}
     newname=${line[-1]}
     <mv...>

done <infile.txt
But then again these are assuming a single file, with the first and last "words" being the filenames, which is not what the OP described.
 
Old 03-28-2013, 04:44 PM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Many distributions have a rename command. Beware that some have a version which uses perl expressions and others have a version that uses old and new parameters. With the former, you could do:
Code:
rename 's/_old.txt/_new.txt/' *_old.txt
with the latter you could do:
Code:
rename _old.txt _new.txt *_old.txt
 
  


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
IBM sftp from Linux: Need Linux commands to search/view/rename/delete files ddw Linux - Newbie 3 09-17-2012 11:13 AM
Rename network interface (Rename ppp0 ...) Nicolas1390 Linux - Newbie 2 08-08-2011 02:14 AM
Rename files Linux rahul_x Programming 6 11-17-2006 10:35 AM
rename linux ethernet adapter samills70 Linux - Newbie 2 06-09-2004 03:44 AM
How to rename Linux server owensct Linux - Networking 4 11-03-2003 04:49 PM

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

All times are GMT -5. The time now is 12:43 AM.

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