简体   繁体   中英

Renaming/removing spaces in directories

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM