Linux - Replacing spaces in the file names

前端 未结 11 658
臣服心动
臣服心动 2021-01-29 18:21

I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?

相关标签:
11条回答
  • 2021-01-29 18:41

    Use sh...

    for i in *' '*; do   mv "$i" `echo $i | sed -e 's/ /_/g'`; done
    

    If you want to try this out before pulling the trigger just change mv to echo mv.

    0 讨论(0)
  • 2021-01-29 18:43

    If you use bash:

    for file in *; do mv "$file" ${file// /_}; done
    
    0 讨论(0)
  • 2021-01-29 18:44

    This should do it:

    for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
    
    0 讨论(0)
  • 2021-01-29 18:46

    Quote your variables:

    for file in *; do echo mv "'$file'" "${file// /_}"; done
    

    Remove the "echo" to do the actual rename.

    0 讨论(0)
  • 2021-01-29 18:46

    To rename all the files with a .py extension use, find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"

    Sample output,

    $ find . -iname "*.py" -type f                                                     
    ./Sample File.py
    ./Sample/Sample File.py
    $ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
    $ find . -iname "*.py" -type f                                                     
    ./Sample/Sample_File.py
    ./Sample_File.py
    
    0 讨论(0)
  • 2021-01-29 18:53

    The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows

    sed -i 's/\s/_/g' *
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题