LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-03-2010, 05:02 PM   #1
vituttaa
LQ Newbie
 
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
renaming a bunch of files using for and mv


hello, this is a rather dumb question I suppose, but here goes anyway.

Basically I need to rename a bunch of .doc files using the for-structure and the mv command (w/ wildcards) in bash. I guess this would be a bit easier if I'd use the rename command, but since this is a school assignment of sorts I need to use for & mv.

The .doc files are named "1filename.doc", "2filename.doc" etc.
And I've got to rename them to "aaa_1filename.doc", "aaa_2filename.doc", "aaa_3filename.doc" and so on.

Tried to dabble quite a bit with the for and mv commands, basically just got a bunch of errors. Every damn time. For 2 hours. The most common error was "mv: missing destination file operand after ..."

So, what would be the right way to do this using for and mv? Thanks in advance, and I apologize if this is in the wrong section or just completely useless in other possible ways.
 
Old 02-03-2010, 05:09 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Code:
for i in $(ls *.doc)
do
  mv $i aaa_$i
done
 
Old 02-03-2010, 05:14 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,720

Rep: Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704
But you'll be in trouble any of the files has a space in its name. Better to use
Code:
for i in *.doc
do
   mv "$i" "aaa_$i"
done
Evo2.
 
Old 02-03-2010, 05:16 PM   #4
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
I did think that when I posted it but I'd already edited twice to correct syntax errors so didn't again lol as the OP gave some example filenames.
Who puts spaces in filenames anyway?!?!? tut tut!!
 
Old 02-03-2010, 05:25 PM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,720

Rep: Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704Reputation: 1704
Quote:
Originally Posted by devnull10 View Post
Who puts spaces in filenames anyway?!?!? tut tut!!
Horrible people do. I just recently switched to using Chromium... and what do you know about the config files. "Don't be evil" hah!

Evo2.
 
Old 02-03-2010, 05:47 PM   #6
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
lol. Just realised actually that my example wouldn't work with spaces because ls when used in a for loop tokenizes on spaces. When I renamed about 40K files to remove the space a while back I used something like:

Code:
find -depth ~/mydir/ | while read i
do
  mv "$i" $(echo "$i" | sed "s/ /_/g")
done
The advantage here is that the depth first search renames the files in a directory before the directory itself (ie, so you don't rename the directory and then the old location becomes invalid! ). Was surprisingly quick to run to be honest!
 
Old 02-03-2010, 05:57 PM   #7
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Quote:
hello, this is a rather dumb question I suppose, but here goes anyway.
No question is "dumb", but yours is "homework" I think. There are LQ rules about this [See the panels at the right ... LQ Rules]

Quote:
Tried to dabble quite a bit with the for and mv commands, basically just got a bunch of errors
If you had posted your scripts (preferably within CODE tags ["Go advanced" from the quick reply button thingy]) we might be able to help you further.

"a bunch of errors" [ "No cheese in fridge. HALT" ] is not helpful, at all, to anyone.

Please help us help yourself.
 
  


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
How can I download a bunch of files from an http site? abefroman Linux - Desktop 4 12-16-2009 10:00 PM
Rename a bunch of files auwolf Linux - Newbie 9 10-06-2006 02:55 AM
un-TARring a bunch of files at once grapnell Linux - General 2 02-12-2006 10:18 AM
un-TARring a bunch of files at once grapnell Linux - Software 4 02-12-2006 10:07 AM
Need helpThis script erased a bunch of my files HELP VisionZ Linux - Newbie 12 03-25-2004 11:02 AM

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

All times are GMT -5. The time now is 01:22 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