I have several files with spaces in their names. I wanted to replace the spaces with underscores: For example, I used Code: rename -n 's/ /_/g' *.htm to get Code: 02 09 dp.htm renamed as 02_09_dp.htm 02 10 dp.htm renamed as 02_10_dp.htm 02 11 dp.htm renamed as 02_11_dp.htm In the code I know that "s" stands for substitute but what is "g"? (It should be clear that I don't know perl but I know that spaces in filenames can be bad things in *nix.) This is with Ubuntu. Edit: -n does a mock run to let one know what will happen without actually doing anything. It can be replaced with -v (for verbose) or just removed when doing the real thing.
Okay, it's a modifier: I guess it's needed to take care of multiple spaces? Edit: Oops! That was a modifier for the match operator. Code: g Replaces all occurrences of the found expression with the replacement text is for the substitution operator!
Code: rename ' ' '_' *.htm i hate giving stuff like that without trying it first. so please make a backup first you could try sed too, or thunars bulk renamer, i really like that. EDIT. i see they are file names not contents. i was going to just say use gedit that lets you replace charactors lol
I prefer doing it like this, either singly or in loop: PARAM=`echo "$ORIG" | sed "what i want to replace"` Then, if I'm satisfied: mv $ORIG $PARAM That way I avoid the tiny regex errors and whatnot. Cheers, Mrk
Does that work for you? In what context and with what OS? It doesn't do anything for me on Ubuntu 11.10. As far as I can tell, the Debian version of rename requires a perl expression: Code: SYNOPSIS rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
erm, no. i didn't really think, sorry. i checked this out though and it should work. i wrote it and checked it on other sites o_0 Code: for FILE in *.html ; do NEW=`echo $FILE | sed 's/ /_/g'` ; mv "$FILE" $NEW ; done it's driving me a bit crazy. what's the simplest way to run the command using rename?
No problems! I'll keep sed and awk for another time. Right now I'm quite happy learning to rename a lot of files just the way I like using rename and a smattering of regex. I just managed to change ddmmyy.* to yyyymmdd.* for a bunch of stuff (all of this century ). The next frontier for me is doing such things recursively as opposed to folder-wise.
As I go along, I find that there are at least three related beasts: rename, prename and rename.ul. The last uses a much simpler command than rename and looks very much like what iceni60 has given above. I have to digest this for a while!
According to this link, oldName001.mat, oldName002.mat, ..., oldName200.mat can be renamed to newName001.mat, newName002.mat, ..., newName200.mat using rename Code: rename "s/oldName/newName/" *mat but using rename.ul seems simpler Code: rename.ul oldName newName *mat I don't know how more complex renamings will be handled by the latter.
part of the reason i'm posting a little more is because i'm starting to learn linux again, i've been using it everyday, but it's easy to watch films/TV and documentaries. with a bit of luck i may give some more sensible answers lol
i was just watching some videos and saw this one - http://www.youtube.com/watch?v=MRlUPV1221Y&feature=related
The video left out the all important -n that does a dry run first. But now I have an excuse for watching more YouTube
Hi vasa1, The g at the end of the substitution command actually replaces all occurrences of the found expression with the replacement text multiple times on the same line given it exists multiple times on the same line. That is what they mean by global substitution of g for the range of lines the command has been given - instead of simply replacing only one instance on each line the expression exists which is the default when the g is not used. -- Tom