LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-31-2007, 11:30 PM   #1
linux2man
Member
 
Registered: Oct 2006
Posts: 34

Rep: Reputation: 16
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
 
Old 02-01-2007, 01:12 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 02-01-2007 at 01:49 AM.
 
Old 02-01-2007, 11:27 AM   #3
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Actually, why not just use "rename"

Code:
rename -www.website.com "" FILES

Last edited by SciYro; 02-01-2007 at 12:30 PM.
 
Old 02-01-2007, 11:40 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
Old 02-01-2007, 01:28 PM   #5
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
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.
 
Old 02-01-2007, 01:55 PM   #6
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
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.
 
Old 02-03-2007, 02:24 AM   #7
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
FRONTENDS:

I think it exist krename

and

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

Enjoy !
 
Old 02-03-2007, 02:27 AM   #8
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
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

It is like replacing some txt1 in one contentfile by txt2, it is not easy to avoid \ / \ / /// , no ?

Last edited by Xeratul; 02-03-2007 at 02:31 AM.
 
Old 02-03-2007, 05:26 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename files Linux rahul_x Programming 6 11-17-2006 10:35 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 06:06 AM
rename files allelopath Linux - General 5 07-05-2005 03:00 AM
quick rename many files cadj Programming 7 12-29-2004 04:14 AM
Can not rename files. Maximus2000 Linux - General 0 04-22-2004 01:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration