Bash recursively replace many spaces with names
Can anyone recommend a safe solution for recursively replacing spaces with underscores in file and directory names starting at a given root directory? For example,
$ tree
...
| - a dir
| `- file with spaces.txt
`- b dir
| - another file with spaces.txt
`- yet another file with spaces.pdf
becomes:
$ tree
...
| - a_dir
| `- file_with_spaces.txt
`- b_dir
| - another_file_with_spaces.txt
`- yet_another_file_with_spaces.pdf
I copied the question by another user, which is the main question, but I need to add another problem:
I am using the following solution:
$ find -depth -name '* *' -execdir rename " " "_" {} +;
It works, but only replaces the first whitespace found on an element (dir or file). Any ideas on how to create a loop to find spaces and stop when they go away?
+4
source to share
5 answers
You can also change the variable IFS
to IFS
use line endings instead of spaces in the loop:
IFS=$(echo -en "\n\b") ; for file in $(find . -type f) ; do mv -v $file ${file// /_} ; done
https://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
0
source to share