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

Issues and / or general discussion relating to Puppy


Moderator: Forum moderators

Post Reply
User avatar
gychang
Posts: 651
Joined: Fri Aug 28, 2020 4:51 pm
Location: San Diego, CA
Has thanked: 234 times
Been thanked: 73 times

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

Post 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.

Last edited by gychang on Thu Mar 06, 2025 12:35 am, edited 1 time in total.

======

Puppy Bytes, utube videos
https://www.youtube.com/channel/UCg-DUU ... u62_iqR-MA

======

User avatar
rcrsn51
Posts: 1486
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 424 times

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

Post 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
Last edited by rcrsn51 on Wed Mar 05, 2025 4:01 am, edited 1 time in total.
User avatar
MochiMoppel
Posts: 1343
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 521 times

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

Post 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 '{}' \;
User avatar
Trapster
Posts: 228
Joined: Sat Aug 01, 2020 7:44 pm
Location: Texas
Has thanked: 2 times
Been thanked: 71 times

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

Post 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 1280 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 1280 times
rox3.png
rox3.png (16 KiB) Viewed 1280 times
User avatar
Trapster
Posts: 228
Joined: Sat Aug 01, 2020 7:44 pm
Location: Texas
Has thanked: 2 times
Been thanked: 71 times

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

Post by Trapster »

test.png
test.png (17.15 KiB) Viewed 1277 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

User avatar
rcrsn51
Posts: 1486
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 424 times

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

Post by rcrsn51 »

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

User avatar
Trapster
Posts: 228
Joined: Sat Aug 01, 2020 7:44 pm
Location: Texas
Has thanked: 2 times
Been thanked: 71 times

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

Post by Trapster »

Yes, your scripts work.

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

Burunduk
Posts: 268
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 7 times
Been thanked: 135 times

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

Post 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/' *
User avatar
gychang
Posts: 651
Joined: Fri Aug 28, 2020 4:51 pm
Location: San Diego, CA
Has thanked: 234 times
Been thanked: 73 times

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

Post 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:

======

Puppy Bytes, utube videos
https://www.youtube.com/channel/UCg-DUU ... u62_iqR-MA

======

User avatar
gychang
Posts: 651
Joined: Fri Aug 28, 2020 4:51 pm
Location: San Diego, CA
Has thanked: 234 times
Been thanked: 73 times

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

Post 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

======

Puppy Bytes, utube videos
https://www.youtube.com/channel/UCg-DUU ... u62_iqR-MA

======

User avatar
gychang
Posts: 651
Joined: Fri Aug 28, 2020 4:51 pm
Location: San Diego, CA
Has thanked: 234 times
Been thanked: 73 times

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

Post 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!

======

Puppy Bytes, utube videos
https://www.youtube.com/channel/UCg-DUU ... u62_iqR-MA

======

User avatar
rcrsn51
Posts: 1486
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 424 times

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

Post 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' \;
Last edited by rcrsn51 on Sat Mar 08, 2025 6:00 pm, edited 16 times in total.
User avatar
gychang
Posts: 651
Joined: Fri Aug 28, 2020 4:51 pm
Location: San Diego, CA
Has thanked: 234 times
Been thanked: 73 times

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

Post by gychang »

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

======

Puppy Bytes, utube videos
https://www.youtube.com/channel/UCg-DUU ... u62_iqR-MA

======

User avatar
MochiMoppel
Posts: 1343
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 521 times

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

Post 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"

User avatar
greengeek
Posts: 1549
Joined: Thu Jul 16, 2020 11:06 pm
Has thanked: 651 times
Been thanked: 228 times

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

Post 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?

User avatar
gychang
Posts: 651
Joined: Fri Aug 28, 2020 4:51 pm
Location: San Diego, CA
Has thanked: 234 times
Been thanked: 73 times

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

Post 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 &
Last edited by gychang on Thu Mar 06, 2025 1:46 pm, edited 2 times in total.

======

Puppy Bytes, utube videos
https://www.youtube.com/channel/UCg-DUU ... u62_iqR-MA

======

User avatar
Trapster
Posts: 228
Joined: Sat Aug 01, 2020 7:44 pm
Location: Texas
Has thanked: 2 times
Been thanked: 71 times

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

Post 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!

User avatar
rcrsn51
Posts: 1486
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 424 times

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

Post 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?

Burunduk
Posts: 268
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 7 times
Been thanked: 135 times

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

Post 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"

User avatar
rcrsn51
Posts: 1486
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 424 times

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

Post by rcrsn51 »

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

Post Reply

Return to “Users Help”