the rename command
Good rename tutorial here. And, on the same site, read the Perl Regular Expression page. Rename is debian is the perl method, by the way, so don't get confused by various howto pages if you use debian. Check out regular-expressions.info for more regex stuff.
In my case, I was looking to change a group of files from an error named: *.jpg.jpg to *.jpg That's a k3b gallery glitch. the solution, in debian, is this: :: Code :: rename 's/\.jpg\.jpg/\.jpg/' *.jpg.jpgthat is, rename \.jpg\.jpg to .jpg for all files ending with .jpg.jpg Next I wanted to add the prefix thmb_ to all thumbnails. This simple command took care of that: :: Code :: rename 's/^/thmb_/' *and that's it, learn once, then use forever, nice work debian guys. Back to top |
For renaming a group of files with spaces, like
A B C jeff A B C bob A B C dan A B C greg you can do it either of these ways, but obviously rename is far more simple: :: Code :: # with loops and sed:
for file in *;do file2=$(echo $file | sed 's/A B C //');mv -T "$file" $file2;done # breaking that into more readable format: for file in * do file2=$(echo $file | sed 's/A B C //') mv -T "$file" $file2 done # using rename [and how could you not choose rename, it's so much more simple: rename 's/A B C //' * #Note that you have to escape each space character Key parts here are, in the sed/loop method, "$file" must be in quotation marks to avoid error from spaces. For removing all spaces, no matter how few or many there are, or replacing them with something else, without knowing what patterns the names have, you can run something like this: :: Code :: for file in *;do file2=$(echo $file | sed 's/ //g');mv -T "$file" $file2;done
# or, expanded: for file in * do file2=$(echo $file | sed 's/ //g') mv -T "$file" $file2 done # to replace a space with say an underscore, change file2=$(echo $file | sed 's/ //g') # to file2=$(echo $file | sed 's/ /_/g') # or, to keep it simple with rename: # to remove spaces rename 's/ //g' * # to replace spaces with _ rename 's/ /_/g' * :: Quote :: To rename recursively: prompt$ find ./ -type f -exec rename 'y/A-Z/a-z/' {} \;
use man find for more info src Back to top |
After asking about how to use rename, jbs1136 quickly taught us something:
:: Quote :: I think I figured out what the g stands for, global. I tried the expression without the g and it only did the first occurance and with the g it did all. This is one great tool. I used this:
:: Code :: rename -v 's/ /_/g' */*and it affected every file in every folder in the directory /media/sda3/music. Did the same method to convert cases to lower and to strip out some other stuff. I can't believe how quick and simple it is. Potentially dangerous if you're not careful though. great job, even more fun with rename. Back to top |
All times are GMT - 8 Hours |