Find the answer to your Linux question:
Results 1 to 5 of 5
I often get files with many spaces as part of their names. I would like to automatically replace these spaces with underscores, but otherwise not change the file name. Is ...
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Location
    Ohio
    Posts
    12

    Replace Spaces with Underscores in File Names

    I often get files with many spaces as part of their names. I would like to automatically replace these spaces with underscores, but otherwise not change the file name. Is there a way to do this task with just the bash shell? Thanks for the help!

  2. #2
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    85
    in vi editor

    :%s/ /_/g

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Location
    Ohio
    Posts
    12
    So I understand the 's/ /_/g' portion for 's/old/new/g' as replacing all the occurrences of spaces within the text document with underscores. However, I do not know what the % does in this particular example. Also, It seems like this command replaces all occurrences of a space character with an underscore within the document. I do not want to alter the file contents, but merely the name of the file itself.

    For example:

    This is my file.pdf ------> This_is_my_file.pdf

    Thanks again.

  4. #4
    Just Joined!
    Join Date
    Dec 2010
    Location
    India
    Posts
    45
    this script will convert the name of all the files present in that directory.


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

  5. #5
    Just Joined!
    Join Date
    Sep 2010
    Location
    Ohio
    Posts
    12
    Thank you very much. That worked perfectly!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...