Linux - Replacing spaces in the file names

前端 未结 11 829
臣服心动
臣服心动 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:57

    Here is another solution:

    ls | awk '{printf("\"%s\"\n", $0)}' | sed 'p; s/\ /_/g' | xargs -n2 mv
    
    1. uses awk to add quotes around the name of the file
    2. uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name
    3. xargs takes 2 lines at a time and passes it to mv

提交回复
热议问题