How to replace spaces from directories with an underscore in linux?

A Bash command like the following can do this. On your terminal, you need to be in the directory going down from where you want to change the directories. Please note that the following command does the replacements recursively. SO, even the sub-directories would have its spaces replaced with an underscore:

   find -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

If you do not want to do it recursively also into sub-directories, you should fire the following command:
   find -name "* *" -type d | rename 's/ /_/g'



How do to the same in the files?

Find the file names in a directory and its subdirectory containing spaces. Replace the space between the stars with any string and it will find the files containing that string.bio
    find -name "* *" -type f

To replace a string in the file name under that directory/sub-directory, use the following command:
    find -name "* *" -type f | rename 's/ /_/g'