Space in the file name


#1

Hi. I know how to get to the other disks now. However, one of my disks lists a directory called “z Ubuntu” which I’m unable to access it. The message says that the directory does not exists. I believe this is because of the space withing directory name. What should I do?


#2

Since your directory contains space within its name you will need to enclose the directory with quotes ". So for example to navigate to a directory which contains space eg.: “z Ubuntu” enter the following cd command:

$ cd "z Ubuntu"

The same principle applies also for most other commands. For example the following error will occur with ls command when attempting to list directory with space within its name:

$ ls z Ubuntu
ls: cannot access 'z': No such file or directory
ls: cannot access 'Ubuntu': No such file or directory

To avoid the above error you need to enclose the directory name with ":

$ ls "z Ubuntu"

Yet, another alternative is to escape the space within the file/directory name with back slash \. For example the following command will produce the same result as when using quotes:

$ ls z\ Ubuntu
OR
$ cd z\ Ubuntu

Linux shells are sensitive to space within directory and files names. For example when accessing the directory with name “My Directory” the ls command will threat both arguments separately. For example:

$ ls My Directory

In this case the ls command thinks that the user wishes to list two directories named My and Directory. Enclosing the directory name within quotes " instructs the ls command to access existing directory “My Directory” instead.

It is never good idea to create file or directory name containing space. If you wish to have space within a file/directory name use underscore “_” character instead. For example the correct name for the above directory name will be “My_Directory”.

To remove the space within the directory name with “_” simply rename the directory. For example:

$ mv "My Directory" My_Directory

For more information on how to batch remove/replace space within file/directory name read: Remove or substitute space within a file name.

Hope this helps

Lubos