LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-19-2011, 02:24 AM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Rep: Reputation: 30
remove part of multiple filenames?


hey all i have a folder with lots of random jpegs but they all have the words 'SOMETHINGRANDOM' in there name that i want to remove and i'm trying something like this but it just renames all the files to 'newname' any help?


Code:
for filename in *.jpg; do newname=`echo $filename | sed -e 's/SOMETHINGRANDOM//g'` mv $filename newname; done
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-19-2011, 02:53 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
Hi,

Code:
for THISFILE in *.jpg; do rename "SOMETHINGRANDOM" "" $THISFILE; done
Hope this helps.
 
Old 01-19-2011, 03:04 AM   #3
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
just tested that on some test files and it gave this error:

Quote:
$ ls
test1.jpg test2.jpg test3.jpg test4.jpg test5.jpg
$ for THISFILE in *.jpg; do rename "test" "" $THISFILE; done
Bareword "test" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "test" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "test" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "test" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "test" not allowed while "strict subs" in use at (eval 1) line 1.
thanks so far
 
Old 01-19-2011, 03:24 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
Hi,

edit: See post #7, rename has 2 different versions

Strange, works here:
Code:
$ touch test1.jpg test2.jpg test3.jpg test4.jpg test5.jpg

$ ls *.jpg
test1.jpg  test2.jpg  test3.jpg  test4.jpg  test5.jpg

$ for THISFILE in *.jpg; do rename "test" "" $THISFILE; done

$ ls *.jpg
1.jpg  2.jpg  3.jpg  4.jpg  5.jpg
I was assuming that the command is executed from the command line, but the ...in use at (eval 1) line 1. makes me wonder.

Try using single quotes instead of double: rename 'test' ''

Hope this helps.

EDIT:

BTW: test is a unix/linux command, try to avoid using it for other purposes.

Last edited by druuna; 01-19-2011 at 03:41 AM. Reason: Added comment about test
 
Old 01-19-2011, 03:26 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by steve51184 View Post
hey all i have a folder with lots of random jpegs but they all have the words 'SOMETHINGRANDOM' in there name that i want to remove and i'm trying something like this but it just renames all the files to 'newname' any help?
Your solution is plain and simple and it would work if it were not for some syntax errors. You missed a semi-colon to separate the sed and the mv command and you missed the $ sign in the reference of the variable newname. Check this:
Code:
for filename in *.jpg; do newname=`echo $filename | sed -e 's/SOMETHINGRANDOM//g'`; mv $filename $newname; done
 
1 members found this post helpful.
Old 01-19-2011, 03:35 AM   #6
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna View Post
Hi,

Strange, works here:
Code:
$ touch test1.jpg test2.jpg test3.jpg test4.jpg test5.jpg

$ ls *.jpg
test1.jpg  test2.jpg  test3.jpg  test4.jpg  test5.jpg

$ for THISFILE in *.jpg; do rename "test" "" $THISFILE; done

$ ls *.jpg
1.jpg  2.jpg  3.jpg  4.jpg  5.jpg
I was assuming that the command is executed from the command line, but the ...in use at (eval 1) line 1. makes me wonder.

Try using single quotes instead of double: rename 'test' ''

Hope this helps.

EDIT:

BTW: test is a unix/linux command, try to avoid using it for other purposes.
single quotes still didn't work but thanks for the help

Quote:
Originally Posted by colucix View Post
Your solution is plain and simple and it would work if it were not for some syntax errors. You missed a semi-colon to separate the sed and the mv command and you missed the $ sign in the reference of the variable newname. Check this:
Code:
for filename in *.jpg; do newname=`echo $filename | sed -e 's/SOMETHINGRANDOM//g'`; mv $filename $newname; done
oops silly me... just tested and its working perfect thank you
 
Old 01-19-2011, 03:35 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
@druuna: since rename is a mass renaming command, you don't really need a loop. The following should work for all the JPG files in the current working dir:
Code:
rename "test" "" *.jpg
Anyway, take in mind that the rename command has two different versions with two different syntax. The one you've used in your example is an ELF executable coming from the util-linux package and it's typically installed on RPM based systems. The other one is a perl script typically available on Debian-based systems.

For the latter the syntax is:
Code:
rename <options> perlexpr [ files ]
so that a correct version for the OP could be:
Code:
rename "s/test//" *.jpg
 
2 members found this post helpful.
Old 01-19-2011, 03:38 AM   #8
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
so that a correct version for the OP could be:
Code:
rename "s/test//" *.jpg
also works perfectly.. i might use that command instead and it's a lot simpler
 
Old 01-19-2011, 03:39 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
@colucix: Learned something today, didn't know there are 2 versions (I indeed have the ELF version, not the perl script).
 
Old 01-19-2011, 03:47 AM   #10
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

@druuna: we're so lucky that we can still learn, that's what life's all about.

@colucix: Thanks for the update, I also have learned something new. The suggestion from druuna gave me the same error as reported on my Debian based system.

Kind regards,

Eric
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 36 07-31-2013 03:10 AM
How do you remove Parenthesis from filenames using sed xmrkite Linux - Software 11 06-09-2010 04:30 PM
Using Linux to remove directory containing invalid windows filenames? ellentk Linux - Newbie 7 06-11-2009 10:23 AM
please help me remove the spaces in filenames in a directory! asilentmurmur Linux - Newbie 3 07-14-2006 08:17 PM
need help with script to remove all metachars from filenames BrianK Programming 5 08-21-2005 12:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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