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.
I have some 650 files all with names such as "2014_NGS_ (99).jpg" i need to remove the ( ) from the names but i have tried every thing i can find i can remove the leading one but not the trailing one any one got any ideas .
Hi
I have some 650 files all with names such as "2014_NGS_ (99).jpg" i need to remove the ( ) from the names but i have tried every thing i can find i can remove the leading one but not the trailing one any one got any ideas .
I dont fancy renaming 653 files by hand really
There is the linux "rename" command, which accepts regular expressions and can do this...you can also do it with a one-liner:
Code:
for i in *\(*\)*; do j=`echo $i |sed -e 's/[()]//g'`; mv "$i" "$j"; done
You're welcome...and by way of explanation to anyone else who stumbles upon this, it's essentially a shell-script that:
Reads a list of files in the current directory into the $i variable: for i in *\(*\)*;
Put the $i through sed to strip out the parens, and assign the new string to be $j: do j=`echo $i |sed -e 's/[()]//g'`;
Move the file from one name to another: mv "$i" "$j"; done
Replace the "mv" with whatever other commands you want. Change the regular expression in the sed command (the [()] ) to do something else if needed, or string more commands together in there or change them entirely. The logic still works.
That's a new one for me....I've gone along with either rename or the aforementioned one-liner to do the deeds. Good find.
Quote:
Originally Posted by szboardstretcher
This removes any characters that aren't letters or numbers from the filename.
Code:
rename 's/[^a-zA-Z0-9]//' *.jpg
Nice regex example....always loved how much power you can pack into a command with them. And how easily you can bork things with it, too, if you're not careful.
Nice regex example....always loved how much power you can pack into a command with them. And how easily you can bork things with it, too, if you're not careful.
Agreed. That's why I'm glad that some commands come with the --dry-run option, or do not make changes to files unless you tell them to specifically (sed -i for example.) And I'm always careful to use the ole online regex testers before trying something out.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.