Linux - SoftwareThis 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
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.
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.
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.
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
$ 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
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
@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.
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.