rename files
Hello
I've more than 20 files all files' name contain word such as "website.com" i need to remove this word by scripts i this for looping should do it but have no idea to use it. example:- file1-www.website.com.mpg file2-www.website.com.mp3 file3-www.website.com.mpg file4-www.website.com.avi would like to rename it to file1.mpg file2.mp3 file3.mpg file4.avi Thanks in advace |
Hi,
Here's one one of doing this: ls -1 *www* | sed 's/\(.*\)\(-www.website.com\)\(.*\)/mv \1\2\3 \1\3/' | bash The ls -1 *www* part should list all the appropriate files. The output is piped to sed, and using backreferencing the following output is created for each file: mv fileX.www.website.com.extension fileX.extension. Sed's output is given to bash, which performs the actual move. Try using the command without the | bash part first to see if the output is correct/what you want. In case backreferencing is new to you: The part between \( and \) in the searchstring can be represented as \1 (\2 \3 etc) in the replacement part. Your original files is broken up into 3 parts: The filename (file1, file2 etc), the part that you don't need (www.web....com) and the extension. Hope this helps. |
Actually, why not just use "rename"
Code:
rename -www.website.com "" FILES |
Hi,
That's indeed a lot cleaner and much more resource friendly! The sad part is: I do know the rename command exists....... Well, at least the one-liner I gave is creative :) |
Quote:
Code:
$ ls |
yes, im using the one that ships with util-linux, the perl version isn't compatible as you can tell, but with regular expressions its also a bit more complex and flexible. The one that ships with util-linux just so happens to not have help output, and appears to take no options anyways.
|
FRONTENDS:
I think it exist krename and gnome-commander has powerful advanced rename tool (ctrl m) Enjoy ! |
Quote:
This sed like it like you need to drink lot of wine to be able to understand or read it ... indeed . rename is cool. sed is quite not easy for noobs:newbie: It is like replacing some txt1 in one contentfile by txt2, it is not easy to avoid \ / \ / /// , no ? |
Hi,
If this is the first time you encounter sed it's not one of the easiest examples :) In the example the backslashes are indeed needed and cannot be avoided because (, ), 1, 2 and 3 are special (related to backreferencing). If you do not use the backslashes sed sees them as normal characters. If you are interested in knowing more about sed: 1) Pick up the Oreily book (Sed&Awk), 2) IBM sed part I 3) IBM sed part II 4) seder's grab bag 5) sed $home BTW: it's too bad that rename comes in different flavors, I tried SciYro command on my box before and it worked (with your example files). I also have the one that comes with util-linux. |
All times are GMT -5. The time now is 08:28 AM. |