Using Quotation Marks

With the Nautilus file manager, it is easy to create files and folders that include spaces in their names. You can also copy and move these about by dragging and dropping them without any problem.

But when you come to access such files from the command line, you must remember to enclose them in quotation marks. For example, if your username is jenny and you have a folder called My Projects in your home folder that you would like to access from the command line, you cannot use the cd command in the normal way, like this:

cd /home/jenny/My Projects

The reason is that Ubuntu will think you want to change to a folder called /home/jenny/My and will report the following error:

bash: cd: /home/jenny/My: No such file or directory

To prevent this, the correct way to change to such a folder is to enter the following, with double quotation marks (or you can use single quotation marks instead if you prefer):

cd "/home/jenny/My Projects"

Likewise, using any other Bash commands with spaces in file or folder names requires similar use of quotation marks. The following collection of commands all illustrate the correct way to handle spaces in folder and filenames:

mkdir "/home/jenny/My Projects"
cd ~/"My Projects"
ls -al "/home/jenny/My Projects/January"
rm "Backups/Old|Report.doc"

The fourth line doesn’t have any spaces but it still requires the use of quotation marks. The reason is that the | character—which, like the space, is a normal character when used on the desktop—is ...

Get Ubuntu: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.