Search code examples
linuxreplacefindcommandbatch-rename

Linux command to replace spaces with -(dash) from directories name recursevily?


How to replace spaces with -(dash) from directory name in Linux using command line?

Note: There are hundreds of directories and all of them have sub directories too.

I have tried below command but it return a message 'call: rename from to files...' and all names are still unchanged.

find /home/jjj/ddd -name "* *" -type d | rename 's/ /-/g'

I would like Change the "Directory Name" to "Directory-Name".


Solution

  • You can try using shell instead rename

    find /home/jjj/ddd -depth -name "* *" -type d -print0 | while read -d $'\0' dir; do mv -v "$dir" "${dir// /-}"; done
    

    added -depth to make it proper as per requirement.