Category Archives: linux

How to use Windows-style file names having spaces when using Linux commands

Here’s a quick thing that might help someone out.

Here’s the scenario:  you have a home or work network that has both windows computers and linux computers.  Some of the directories are the windows-style and contain spaces, such as “My Documents”.  This is fairly common, especially as some networks use windows workstations but a linux server.

The problem comes in when you are attempting to access directories or files from the command line in linux.  If you type:

cd My Documents

you will get something like this in return:

bash: cd: My: No such file or directory

There is a very easy solution:  simply enclose the directory or file name containing spaces in single or double quotes.  For example,

cd 'My Documents'

works like a charm.  Remember when you are using linux systems that all entries in the command line are case sensitive, so

cd 'my documents'

is not the same as

cd 'My Documents'

There is another way that will work.  You can use the “\” character to escape the space.  Note that this will work for other special characters in file or directory names also.  In the case of a directory or file name containing spaces, the space constitutes a special character, so

cd My\ Documents

will work fine as well.

I hope this will help someone and save you time.  I know this is an irritating part of working on a network that has both windows and linux computers, but it isn’t really that big of a problem if you are armed with a little knowledge.