Solved Need a command to replace underscores in file names

Solved issue

ron.alan

Active Member
Joined
Dec 24, 2023
Messages
144
Reaction score
109
Credits
997
I have a bunch of pdf files (all in same folders) that all have underscores in the file name. For example:

aaa_bb_cc.pdf
ddd_eee_ffff_gg.pdf
Etc. and etc.

What is the/a terminal command I can use to change them all to either periods (.), or dashes (-), or whatever? Thanks.
 


Two methods showing change of underscores to hyphens:

Using find and rename:
Code:
[tom@min ~]$ ls
aaa_bb_cc.pdf  ddd_eee_ffff_gg.pdf

[tom@min ~]$ find . -name "*" -type f | rename 's/_/-/g'

[tom@min ~]$ ls
aaa-bb-cc.pdf  ddd-eee-ffff-gg.pdf


Using bash with it's substitution code:
Code:
[tom@min ~]$ ls
aaa_bb_cc.pdf  ddd_eee_ffff_gg.pdf

[tom@min ~]$ for f in *\_*;do mv "$f" "${f//_/-}";done

[tom@min ~]$ ls
aaa-bb-cc.pdf  ddd-eee-ffff-gg.pdf

Use the commands in the directory in which the files are located, or adjust accordingly.
 
Thank you. But while you were writing your response, I found out about caja-rename, and since I use Mate as my desktop, I went ahead and installed it. I just tested it and it works fine.
 
find ./ -depth -name "*_*" -execdir rename 's/_/-/g' "{}" \;

That should do it.

Though I see @osprey beat me to it. Probably while I was testing.
 

Members online


Top