LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   renaming a bunch of files using for and mv (https://www.linuxquestions.org/questions/linux-newbie-8/renaming-a-bunch-of-files-using-for-and-mv-786776/)

vituttaa 02-03-2010 04:02 PM

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.

devnull10 02-03-2010 04:09 PM

Code:

for i in $(ls *.doc)
do
  mv $i aaa_$i
done


evo2 02-03-2010 04:14 PM

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.

devnull10 02-03-2010 04:16 PM

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!! :)

evo2 02-03-2010 04:25 PM

Quote:

Originally Posted by devnull10 (Post 3851387)
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.

devnull10 02-03-2010 04:47 PM

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!

tredegar 02-03-2010 04:57 PM

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.


All times are GMT -5. The time now is 02:05 PM.