Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glob renaming in bash

I'm fairly new to bash so sorry if this is kind of a basic question. I was trying to rename a bunch of mp3 files to prepend 1- to their filenames, and mv *.mp3 1-*.mp3 didn't work unfortunately. So I tried to script it, first with echo to test the commands:

for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done

Which seems to output the commands that I like, so I removed the echo, changing the command to

for f in *.mp3 ; do mv \'$f\' \'1-$f\'; done

Which failed. Next I tried piping the commands onward like so

for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done | /bin/sh

Which worked, but if anyone could enlighten me as to why the middle command doesn't work I would be interested to know. Or if there is an more elegant one-liner that would do what I wanted, I would be interested to see that too.

like image 673
wim Avatar asked May 04 '11 11:05

wim


People also ask

How do I rename a file in bash?

You can use the built-in Linux command mv to rename files. Here are some of the options that can come in handy with the mv command: -v , --verbose : Explains what is being done. -i , --interactive : Prompts before renaming the file.

How do I rename a file in a sequence?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.


1 Answers

I think you have to change the command to

for f in *.mp3 ; do mv "$f" "1-$f"; done

Otherwise you would pass something like 'file1.mp3' and '1-file1.mp3' to mv (including the single quotes).

like image 53
bmk Avatar answered Oct 23 '22 20:10

bmk