LinuxQuestions.org
Help answer threads with 0 replies.
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 08-06-2014, 09:09 AM   #1
peten
LQ Newbie
 
Registered: May 2010
Location: West Midlands
Distribution: Opensuse 11.3 & 11.4 both x86_64
Posts: 8

Rep: Reputation: 0
unwanted chars in filename


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

Thanks

Pete ..
 
Old 08-06-2014, 09:38 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by peten View Post
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
 
1 members found this post helpful.
Old 08-06-2014, 11:45 AM   #3
peten
LQ Newbie
 
Registered: May 2010
Location: West Midlands
Distribution: Opensuse 11.3 & 11.4 both x86_64
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
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
Thats the one many thanks problem solved

Pete
 
Old 08-06-2014, 12:35 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by peten View Post
Thats the one many thanks problem solved
Pete
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.
 
Old 08-06-2014, 12:42 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
You might also want to look at a small utility called 'detox'
http://kmandla.wordpress.com/2010/01...me-file-names/
 
1 members found this post helpful.
Old 08-06-2014, 12:43 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
This removes any characters that aren't letters or numbers from the filename.

Code:
rename 's/[^a-zA-Z0-9]//' *.jpg
 
1 members found this post helpful.
Old 08-06-2014, 01:16 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by sycamorex View Post
You might also want to look at a small utility called 'detox'
http://kmandla.wordpress.com/2010/01...me-file-names/
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.
 
Old 08-06-2014, 04:30 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by TB0ne View Post
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.

http://regex101.com/
http://www.regexplanet.com/advanced/java/index.html
http://regexpal.com/

Being the ones that I frequent, to cover my Perl, Ruby, GNU, PHP and Java differences.
 
  


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
[SOLVED] Please help script loop into the /tmp/filename.txt out put with filename and wc. dotran Linux - Newbie 10 06-08-2012 05:02 PM
[SOLVED] sed: replace regexp w/ variable #s of chars with the same # of (diff.) chars? kmkocot Linux - Newbie 6 11-18-2011 05:36 AM
[SOLVED] Special chars on filename problems with Centos and Apache jmandrak Linux - General 6 07-21-2009 07:37 PM
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 09:29 PM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 06:07 AM

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

All times are GMT -5. The time now is 03:56 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