How to rename files recursively?

luisam_95

New Member
Joined
Feb 11, 2023
Messages
4
Reaction score
2
Credits
36
I need a script for recursively renaming files, directories and subdirectories. The names of the those contain spaces, and I want to replace those spaces with underscores. How can I do that?

1691515516886.png
 


There are a few few ways to replace spaces with underscores. Below is one example where the files with spaces are created in the directory /home/flip/testdir, the "find and rename" command is used to make the change, and then ls is run to check that the spaces have indeed been replaced by underscores:
Code:
[flip@flop ~]$ cd /home/flip/testdir ; touch "file a" "file b" "file c"
[flip@flop ~]$ ls
'file a'  'file b'  'file c'
[flip@flop ~]$ find /home/flip/testdir -name "* *" -type f | rename 's/ /_/g'
[flip@flop ~]$ ls
file_a  file_b  file_c
Bear in mind that if /home/flip/testdir had directories beneath it with filenames which had spaces, that these also would be altered to replace those spaces with underscores, so the effect of the find command is recursive in that respect.
 
Last edited:

Members online


Top