renaming multiple files

The place to post if you need help or advice

Moderators: ChriThor, LXF moderators

renaming multiple files

Postby guy » Thu Jan 19, 2017 6:07 pm

Dear Linux geeks, this is the twenty-first century.
I want to rename multiple files in a folder, all with the form [stuff]crop.jpg to [stuff].jpg.
A simple task for bash surely.
[shudder!] regular expressions, switches, a salad of forward- and back-slashes, yecch!
Why can I not do something simple like for example:
Code: Select all
rename *crop.jpg *.jpg

But no, I get error messages about the file not being a folder and suchlike.

Please, without becoming a perl expert (I have only one life left), is there an easier way?
"We don't need no frikkin' aliens, we c'n do this ourselves!" — anon.
guy
LXF regular
 
Posts: 1323
Joined: Thu Apr 07, 2005 12:07 pm
Location: Worcestershire

Re: renaming multiple files

Postby jonni » Fri Jan 20, 2017 11:02 am

The POSIX way to do this is:

Code: Select all
for file in *crop.jpg
do
    mv "${file}" "${file%-crop.jpg}".jpg
done

There are many alternatives, including the Linux-specific rename command, or regular expressions. Here we use the % operator to lop "-crop.jpg" off the end of our string. % matches the shortest occurence of whatever you put after it, %% matches the longest, but there is no distinction in this case.
User avatar
jonni
 
Posts: 92
Joined: Thu Jun 26, 2014 1:59 pm

Re: renaming multiple files

Postby guy » Sat Jan 21, 2017 10:59 am

Thanks.

The above code is partly working but is not removing the crop.jpg suffix, just appending .jpg to lengthen it to crop.jpg.jpg

Jonni wrote:There are many alternatives, including the Linux-specific rename command, or regular expressions.


Yes. Perl argument hell, regular expression hell, or both to choice. My exact reason for asking for a 21st century way.
"We don't need no frikkin' aliens, we c'n do this ourselves!" — anon.
guy
LXF regular
 
Posts: 1323
Joined: Thu Apr 07, 2005 12:07 pm
Location: Worcestershire

Re: renaming multiple files (Solved)

Postby guy » Sat Jan 21, 2017 5:02 pm

All my files had the same prefix, then a number, then the troublesome suffix
So I tried this approach:
Code: Select all
for (( i=XX; i<=YY; i++)) ; do
mv "my-prefix"$i"crop.jpg" "my-prefix"$i".jpg"
done;

Worked like a charm.

Would still be nice to know what Jonni's suggestion missed.
"We don't need no frikkin' aliens, we c'n do this ourselves!" — anon.
guy
LXF regular
 
Posts: 1323
Joined: Thu Apr 07, 2005 12:07 pm
Location: Worcestershire

Re: renaming multiple files

Postby nelz » Sat Jan 21, 2017 8:25 pm

You can't use * in a shell command because the shell expands the wildcard before passing the filenames to the command.

It sounds like you want something like krename, which does the job very simply. For a command line alternative, try rename or mmv.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 9038
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Re: renaming multiple files

Postby jonni » Mon Jan 23, 2017 10:32 am

My exact reason for asking for a 21st century way.


As Neil says, perhaps krename or the new batch rename functionality in Gnome Files are the droids you're looking for.

Would still be nice to know what Jonni's suggestion missed.


Yes it would, if no one points out my mistakes I'll just keep making 'em. I can see that the hyphen should really be included in the wildcard, but that wouldn't matter unless you had files ending in both hyphened and non-hyphened ways. Also if your files end with, e.g. -Crop.jpg or -crop.jpeg then it'll mess things up. Here's how it behaves on my setup:

Make some files with your filename spec:

Code: Select all
$ mkdir test
$ cd test
$ for ((i=1; i<=10; i++)); do touch "thing"$i"-crop".jpg; done
$ ls
thing10-crop.jpg  thing2-crop.jpg  thing4-crop.jpg  thing6-crop.jpg  thing8-crop.jpg
thing1-crop.jpg   thing3-crop.jpg  thing5-crop.jpg  thing7-crop.jpg  thing9-crop.jpg


Yep. Those are some files. Now we run my supposedly shoddy code:

Code: Select all
$ for file in *crop.jpg; do mv "${file}" "${file%-crop.jpg}".jpg; done
$ ls
thing10.jpg  thing2.jpg  thing4.jpg  thing6.jpg  thing8.jpg
thing1.jpg   thing3.jpg  thing5.jpg  thing7.jpg  thing9.jpg


Seems like the desired outcome, no?
User avatar
jonni
 
Posts: 92
Joined: Thu Jun 26, 2014 1:59 pm


Return to Help!

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests