LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   rename files (https://www.linuxquestions.org/questions/linux-general-1/rename-files-524558/)

linux2man 01-31-2007 11:30 PM

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

druuna 02-01-2007 01:12 AM

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.

SciYro 02-01-2007 11:27 AM

Actually, why not just use "rename"

Code:

rename -www.website.com "" FILES

druuna 02-01-2007 11:40 AM

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

Quigi 02-01-2007 01:28 PM

Quote:

Originally Posted by SciYro
Code:

rename -www.website.com "" FILES

I created linux2man's files, then tried the above, which gave an error. A slightly modified command did it:
Code:

$ ls
file1-www.website.com.mpg  file3-www.website.com.mpg
file2-www.website.com.mp3  file4-www.website.com.avi
$ rename -www.website.com "" FILES
Unknown option: w
Unknown option: w
Unknown option: w
Unknown option: .
Unknown option: w
Unknown option: e
Unknown option: b
Unknown option: s
Unknown option: i
Unknown option: t
Unknown option: e
Unknown option: .
Unknown option: c
Unknown option: o
Unknown option: m
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
$ rename s/-www.website.com// *
$ ls
file1.mpg  file2.mp3  file3.mpg  file4.avi

(My input bold, correct command green.) This is on Ubuntu Dapper, and "rename" runs (through 2 symlinks) /usr/bin/rename -> /etc/alternatives/rename -> usr/bin/prename, Perl script by Larry Wall and Robin Barker. I take "alternatives" as a hint that other "rename"s exist, and SciYro probably has a different one.

SciYro 02-01-2007 01:55 PM

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.

Xeratul 02-03-2007 02:24 AM

FRONTENDS:

I think it exist krename

and

gnome-commander has powerful advanced rename tool (ctrl m)

Enjoy !

Xeratul 02-03-2007 02:27 AM

Quote:

Originally Posted by druuna
Hi,

Here's one one of doing this:

ls -1 *www* | sed 's/\(.*\)\(-www.website.com\)\(.*\)/mv \1\2\3 \1\3/' | bash


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 ?

druuna 02-03-2007 05:26 AM

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.