how to rename or use mv command with filenames with spaces

Discussion in 'Suggest HOWTO' started by nibbles, May 7, 2008.

  1. nibbles

    nibbles New Member


    I hope no one minds answering this question. I have several files in which I've downloaded but approx. 30 of them have spaces in the filename itself.

    wanting to perform a rename using mv command but doesn't seem to let me make the change.

    Anyone know how to address this?

    I need to move and rename some files and folders that have this little problem.

    thanks in advance.
  2. Mosquito

    Mosquito New Member

    You need to use backslashes to escape the space like so:
    mv File\ With\ Spaces /new/path/location
    This will move "File With Spaces" to the new directory and keep the same name.
  3. nibbles

    nibbles New Member

    Thanks for the tip. Much appreciated. So for example:

    moving the following file lke: "Great Lakes of the great outdoors.doc" located let's said on /raid/data/documents to be moved to
    raid1/data/documents. I'm assuming the following command could be used from the current path for the file:

    "mv Great\ Lakes\ of\ the\ great\ outdoors.doc raid1/data/documents/Great\ Lakes\ of\ the\ great\ outdoors.doc"

    Last edited: May 13, 2008
  4. burschik

    burschik New Member

    Yes, that should work. The following commands should also work:

    mv Great\ Lakes\ of\ the\ great\ outdoors.doc raid1/data/documents/
    mv "Great Lakes of the great outdoors.doc" raid1/data/documents/
    mv 'Great Lakes of the great outdoors.doc' raid1/data/documents/

    You can use the name of a directory as the last argument to "mv" if you want the file to have the same base name in the other directory, as Mosquito indicated.

    But you really need to understand both how "mv" interprets its command line and how the shell processes the command line *before* passing it to "mv". Whatever you type into your terminal is interpreted by the shell first. The shell interprets many characters (most characters on your average keyboard that are neither letters nor digits, in fact) in a special way. It uses spaces, for example, to delimit words. As a result, you need to quote all special characters, not only spaces, using either the backslash, which quotes the character that immediately follows, or single quotes, which quote all characters in between, or double quotes, which quote most characters in between (but not "$" or "!").

    So, if you type

    mv Great Lakes of the great outdoors.doc raid1/data/documents/

    the shell actually sees eight words. The first is "mv", the command you want to use. The next seven are passed to "mv" as arguments *separately*. So, "mv" starts looking for a file named "Great" in the current directory, then for a file named "Lakes" and so on. Presumably these files do not exist, and you get an appropriate error message.

    By quoting "Great Lakes of the great outdoors.doc" you make sure that it is passed to "mv" as a *single* argument, which is what you want.

Share This Page