LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-07-2018, 02:15 PM   #1
rcmonroig
Member
 
Registered: Mar 2007
Location: Chandler, AZ
Distribution: Fedora 10,12,and 14
Posts: 62
Blog Entries: 1

Rep: Reputation: 15
Question using the mv command to rename multiple files


Can the mv command be used to rename multiple files using file expansion. For example:

mv books{1..2}_chap{1..6}.odf manuals{1..2}_chaps{1..6}.odf
 
Old 06-07-2018, 02:31 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,191

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
No, but the rename command can. Multiple versions exist on various distros so check the man page of yours.

Basically the syntax is the same as used for sed:
Code:
rename -v s/foo/bar/ *.jpg
will rename the foo substring into bar for all jpg file in the current directory. Add -n as option to try first whithout actual execution.

jlinkels
 
1 members found this post helpful.
Old 06-07-2018, 04:22 PM   #3
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,057
Blog Entries: 1

Rep: Reputation: 864Reputation: 864Reputation: 864Reputation: 864Reputation: 864Reputation: 864Reputation: 864
+1 rename

Using mv is bit more difficult. With bash you can for loop and stuff. I tend to multi-step batch it. With ls an awk for a text file of names to change. Another step to make a text file of from and to names. This way I can audit it before breaking things. Then a while read on the file with both halves of the name passed as parameters to mv. Using rename is much simpler for simple things.
 
Old 06-07-2018, 07:24 PM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,417

Rep: Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012
A bash shell construction using brace expansion and parameter expansion.
Code:
for f in books{1,2}_chap{1..6}.odf; do t="${f/books/manuals}"; echo "$f" "${t/chap/chaps}"; done
Change "echo" to "mv" if it is correct.
 
Old 06-08-2018, 02:49 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 14,599
Blog Entries: 9

Rep: Reputation: 4093Reputation: 4093Reputation: 4093Reputation: 4093Reputation: 4093Reputation: 4093Reputation: 4093Reputation: 4093Reputation: 4093Reputation: 4093Reputation: 4093
mv itself can't.
i tend to write small ad-hoc scripts as in the previous post.
 
Old 06-09-2018, 06:39 AM   #6
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Rename is very powerful on Debian. I think other distros basically adopt the same script. But there is a KDE gui program called 'krename' that renames groups of files.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Command to rename multiple files in one go meetvivek72 Linux - Newbie 3 06-29-2013 09:06 AM
[SOLVED] rename multiple files lghizoni Linux - Newbie 7 01-30-2013 08:48 AM
Rename multiple files L1nuxn00b703 Linux - Newbie 4 04-11-2012 02:30 AM
rename multiple files tjay1983 Programming 3 08-10-2010 12:43 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 06:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:35 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration