Page 1 of 1

bulk remove part of a file name using find command, How?(SOLVED)

Posted: Tue Mar 04, 2025 7:26 pm
by gychang

I have several files in a photo folder some files are named *.MP.jpg, and *.MP.JPEG and some without MP. How can I remove ".MP" within a file name and convert them to *.jpg?

start files
1.jpg
2.MP.jpg
a.MP.JPEG

desired result
1.jpg
2.jpg
a.jpg

find . -type f -iname '*.MP.jp*' finds files containing .MP but not sure how to remove just .MP in the middle of file name and just have .jpg lower case as the extension.


Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 1:35 am
by rcrsn51

This would get rid of the MP:

Code: Select all

for n in *MP*; do mv $n ${n/.MP/}; done

This would clean up the extensions:

Code: Select all

for n in *.JPEG; do mv $n ${n/JPEG/jpg}; done

Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 2:29 am
by MochiMoppel
gychang wrote: Tue Mar 04, 2025 7:26 pm

find . -type f -iname '*.MP.jp*' finds files containing .MP but not sure how to remove just .MP in the middle of file name and just have .jpg lower case as the extension.

You may have to do this with 2 find commands (and rename, if installed):

Code: Select all

find . -type f -iname '*.MP.jp*' -exec rename .MP. . '{}' \;
find . -type f -iname '*.JPEG'   -exec rename .JPEG .jpg '{}' \;

Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 2:27 pm
by Trapster

If you don't need the find command, rox rename will do it for you. You'll need to do it twice.

in rox, select > all, then, right-click, choose rename

Replace: \.MP > With: <leave blank>
Choose "Apply" to see the changes.
Choose "Rename" to commit the changes.

rox1.png
rox1.png (42.24 KiB) Viewed 1349 times

Then select > all again and choose rename
Replace: \.JPEG > With: .jpg
Choose "Apply", then choose "Rename"

rox2.png
rox2.png (42.22 KiB) Viewed 1349 times
rox3.png
rox3.png (16 KiB) Viewed 1349 times

Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 2:39 pm
by Trapster
test.png
test.png (17.15 KiB) Viewed 1346 times

Code: Select all

~/test$ find . -type f -iname '*.MP.jp*' -exec rename .MP. . '{}' \;
syntax error at (user-supplied code), near "."
syntax error at (user-supplied code), near "."
~/test$ 

This is in VanillaDpup-9.3.14


Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 3:54 pm
by rcrsn51

@Trapster Did you test the other script-based solution provided above?


Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 5:06 pm
by Trapster

Yes, your scripts work.

I did not originally try them as the Op requested using the find command.


Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 6:14 pm
by Burunduk
Trapster wrote: Wed Mar 05, 2025 2:39 pm

Code: Select all

~/test$ find . -type f -iname '*.MP.jp*' -exec rename .MP. . '{}' \;
syntax error at (user-supplied code), near "."
syntax error at (user-supplied code), near "."
~/test$ 

MochiMoppel is using a different command - rename from util-linux.

Your rename is a perl script. With it, this should work (remove the -n option to actually rename files):

Code: Select all

rename -n -e 's/(\.MP)?\.(JPEG|jpg)$/.jpg/' *

Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 7:18 pm
by gychang
MochiMoppel wrote: Wed Mar 05, 2025 2:29 am
gychang wrote: Tue Mar 04, 2025 7:26 pm

find . -type f -iname '*.MP.jp*' finds files containing .MP but not sure how to remove just .MP in the middle of file name and just have .jpg lower case as the extension.

You may have to do this with 2 find commands (and rename, if installed):

Code: Select all

find . -type f -iname '*.MP.jp*' -exec rename .MP. . '{}' \;
find . -type f -iname '*.JPEG'   -exec rename .JPEG .jpg '{}' \;

@MochiMoppel works well! :thumbup: I put it in my .bashrc as an alias and works perfectly. thanks! :thumbup:


Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 7:19 pm
by gychang
rcrsn51 wrote: Wed Mar 05, 2025 1:35 am

This would get rid of the MP:

Code: Select all

for n in *MP*; do mv $n ${n/.MP/}; done

This would clean up the extensions:

Code: Select all

for n in *.JPEG; do mv $n ${n/JPEG/jpg}; done

@rcrsn51 works, although I have no idea how..., appreciate your input. :D


Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 7:24 pm
by gychang
Trapster wrote: Wed Mar 05, 2025 2:27 pm

If you don't need the find command, rox rename will do it for you. You'll need to do it twice.

in rox, select > all, then, right-click, choose rename

Replace: \.MP > With: <leave blank>
Choose "Apply" to see the changes.
Choose "Rename" to commit the changes.

rox1.png

Then select > all again and choose rename
Replace: \.JPEG > With: .jpg
Choose "Apply", then choose "Rename"

rox2.png

rox3.png

rox is simply amazing..., thanks!


Re: bulk remove part of a file name using find command, How?

Posted: Wed Mar 05, 2025 9:44 pm
by rcrsn51
gychang wrote: Wed Mar 05, 2025 7:19 pm

works, although I have no idea how...,

The simplest Linux tool for renaming a file is "mv". Do this:
1. Make a file named: a
2. Run: mv a b

The script just "mv"s each file in the loop to a new name made with some bash text substitution.

FWIW, if you don't have a functional 'rename' command, you can use this method along with 'find' to rename files. It would also work recursively on batches of files in subfolders.

Code: Select all

find -name '*.MP*' -exec sh -c 'n={} && mv $n ${n/.MP/}' \;   #replace .MP with nothing
find -name '*JPEG' -exec sh -c 'n={} && mv $n ${n/JPEG/jpg}' \; 

Or you could do it in one line with:

Code: Select all

find -iname '*.jp*' -exec sh -c 'n={} && nn=${n/.MP/} && mv $n ${nn/JPEG/jpg} 2>/dev/null' \;

You could simultaneously change either *.JPEG or *.jpeg to *.jpg with:

Code: Select all

find -iname '*.JPEG' -exec sh -c 'n={} && mv $n ${n%.*}.jpg' \; #strip off the extension and add a new one

You coud rename a bunch of .doc files to remove any spaces in the filenames with:

Code: Select all

find -name '*.doc' -exec sh -c 'n="{}" && mv "$n" ${n// /}' \;    #replace all spaces with nothing - note the extra quoting

If you have a set of files named f1, f2, f3, f45, f56, f789, you could rename them with leading zeros f001, f002, etc.

Code: Select all

find -name 'f*' -exec sh -c 'n={} && nn=$(dirname $n)/f$(printf "%03d" ${n#*f}) && mv $n $nn' \;   #pull out the number and format it with zeros

Here is a way to remove any upper case letters from filenames:

Code: Select all

find -name '*.txt' -exec sh -c 'n={} && mv $n ${n,,}' \;    #use the bash upper-lower case conversion method

Here is an extreme example. It takes a bunch of png files, converts them to jpegs using the image-changer tool and stores them in a temp folder.

Code: Select all

find -name '*.png' -exec sh -c 'n={} && nn=$(basename $n png)jpg && image-changer $n /tmp/pics/$nn' \;

Or here's one that shrinks a collection of jpegs by 50%

Code: Select all

find -name '*.jpg' -exec sh -c 'n={} && nn=$(basename $n) && image-changer $n /tmp/pics/$nn 50' \;

Re: bulk remove part of a file name using find command, How?(SOLVED)

Posted: Thu Mar 06, 2025 12:36 am
by gychang

thanks to all who responded, learned a lot and got it all solved.


Re: bulk remove part of a file name using find command, How?

Posted: Thu Mar 06, 2025 2:45 am
by MochiMoppel
Trapster wrote: Wed Mar 05, 2025 2:27 pm

If you don't need the find command, rox rename will do it for you. You'll need to do it twice.

in rox, select > all, then, right-click, choose rename

Replace: \.MP > With: <leave blank>
Choose "Apply" to see the changes.
Choose "Rename" to commit the changes.
Then select > all again and choose rename
Replace: \.JPEG > With: .jpg
Choose "Apply", then choose "Rename"

It's even simpler because you need to call ROX's "Bulk rename files" dialog only once:

in rox, select > all, then, right-click, choose rename
Replace: \.MP > With: <leave blank>
Choose "Apply" to see the changes.
Choose "Rename" to commit the changes.
Then select > all again and choose rename
Replace: \.JPEG > With: .jpg
Choose "Apply", then choose "Rename"


Re: bulk remove part of a file name using find command, How?

Posted: Thu Mar 06, 2025 8:49 am
by greengeek
gychang wrote: Wed Mar 05, 2025 7:18 pm

@MochiMoppel works well! :thumbup: I put it in my .bashrc as an alias and works perfectly. thanks! :thumbup:

Hi gychang - could you give any more detail about how you did this please? Sounds interesting - but what is an alias within .bashrc? How is this done please?


Re: bulk remove part of a file name using find command, How?(SOLVED)

Posted: Thu Mar 06, 2025 12:53 pm
by gychang

@greengeek
alias is a command type u type in a terminal, so when I type in j it runs the alias.
place the lines in .bashrc

#removes MP and JPEG in name and start gthumb
alias j='find /mnt/sda1/Pictures/P4 -type f -iname '*.MP.jp*' -exec rename .MP. . '{}' \; && find /mnt/sda1/Pictures/P4 -type f -iname '*.JPEG' -exec rename .JPEG .jpg '{}' \; && jh.sh && exit'

jh.sh is a script in /root/my-applications/bin, has to be executable with chmod +x jh.sh
basically converts a picture file e.g 1.jpg to 2025-03-06.jpg and open it in gthumb so I can edit the photo.

Code: Select all

#!/bin/sh

wmctrl -s 2 &

sleep 0.5 &

cd1=/root/Pictures/P4
cd2=/mnt/sda1/Pictures/2021-2025/202503

j="exiv2 -F -r%Y-%m-%d-%H%M%S rename *.jp*"
t=~/.Trash

#move jpg from Camera folder to P4
#mv -f $cd1/Camera/*.jpg $cd1 &&

#move jpg/jpeg files in sub folders to P4
find $cd1 -type f -iname "*.jp*" -exec mv -t $cd1 {} \; &

#moves all .mov and .mp4 files
sleep .5
find $cd1 -type f -iname "*.m*" -exec mv -t $cd2 {} \; &

#saves in Trash and renames .jpg in 2 folders
rm -rf $t/* &
rm -rf $t/.* &
cp -r $cd1/. $t/ &
mv $cd1/*.m?? $cd2/ &

#renames multiple folders

sleep 1
cd $cd1 && $j &

sleep 1.5
cd $cd2 && $j &

gthumb $cd1 &

Re: bulk remove part of a file name using find command, How?

Posted: Thu Mar 06, 2025 1:19 pm
by Trapster
Burunduk wrote: Wed Mar 05, 2025 6:14 pm
Trapster wrote: Wed Mar 05, 2025 2:39 pm

Code: Select all

~/test$ find . -type f -iname '*.MP.jp*' -exec rename .MP. . '{}' \;
syntax error at (user-supplied code), near "."
syntax error at (user-supplied code), near "."
~/test$ 

MochiMoppel is using a different command - rename from util-linux.

Your rename is a perl script. With it, this should work (remove the -n option to actually rename files):

Code: Select all

rename -n -e 's/(\.MP)?\.(JPEG|jpg)$/.jpg/' *

This did indeed work

Thanks!


Re: bulk remove part of a file name using find command, How?

Posted: Fri Mar 07, 2025 3:30 pm
by rcrsn51
Burunduk wrote: Wed Mar 05, 2025 6:14 pm

Code: Select all

rename -n -e 's/(\.MP)?\.(JPEG|jpg)$/.jpg/' *

I have been trying to understand this sed-like syntax used by the perl version of rename. Could you please explain it? How is it doing two substitutions at once?


Re: bulk remove part of a file name using find command, How?

Posted: Fri Mar 07, 2025 7:05 pm
by Burunduk
rcrsn51 wrote: Fri Mar 07, 2025 3:30 pm

How is it doing two substitutions at once?

It doesn't. It makes only one substitution:

(\.MP)? - match ".MP" if possible, match "" (an empty string) otherwise (there always is an empty string in between any two adjacent characters)

\. - match "."

(JPEG|jpg) - match "JPEG" OR "jpg"

$ - at the end of the filename

then replace the whole matched part with ".jpg"


Re: bulk remove part of a file name using find command, How?(SOLVED)

Posted: Fri Mar 07, 2025 7:12 pm
by rcrsn51

Got it. It's removing the .MP because it's directly adjacent to the .jpeg extension.