Pretty new to Linux, but have discovered the 'dislike' Linux have for spaces in names.
By using :rename 's/ /_/g' *
it renames everything in that directory by adding a _underscore instead of the spaces.
So "test dir nr 1 becomes "test_dir_nr_1" and "test file 1.txt" becomes "test_file_1.txt"
But! Is there anyway to make it automated and recursive (is that the right word?, ) doing subdirectories in subdirectories as well?
With GNU find
:
find . -name "* *" \( -type f -o -type d \) -execdir rename -v 's/ /_/g' {} +
This searches for regular files and directories containing a space character in their names using your current directory (.
) as start directory and renames them recursively. I just added the -v
erbose flag so you can see what happens.
To list the files and directories which would be affected by the command, remove the -execdir
part:
find . -name "* *" \( -type f -o -type d \)
You can of course replace the .
with an absolute or relative path.