Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.