Linux Commands

How to reference filename with spaces in Linux

For Linux, the user terminal is the most crucial program to manage almost everything in the operating system. But many people do not prefer the command line because they assume that it is a very technical program and require particular expertise to use it. Though knowing it is pretty handy.

Various reasons keep a new Linux user away from the command line, one of them handling files in the terminal. Files and directories in Linux can have different names, and some names can have “spaces” in them. So what’s the big deal? The issue is, the terminal reads the “space” differently; for instance, if your directory has the name “new folder,” the terminal will assume two different directories, i.e., “new” and “folder.” Accessing such files in the terminal can become really frustrating. Luckily, there are various methods to deal with the files/folders with spaces in their names.

This guide is focusing on how to reference filename with spaces in Linux with different approaches. So let’s begin:

How to access files/directories in the terminal with spaces in the name:

In this section, we will be creating a file first with a “space” in its name; then, we will learn what errors we face while referring to it and then how to access it correctly in the terminal:

How to create a file and directory with space in its name:

Creating a file in Linux with “space” in its name is straightforward, open terminal, and run the command mentioned below:

$touch ‘my file

or

$touch my\ file

The file can either be created by using apostrophes or quotation marks. The procedure of creating a directory is quite similar:

$mkdire ‘my directory’

Or:

$mkdire my\ directory

You can verify it by using the “ls” command in the terminal.

How to read a file with space in its name:

Before we learn the correct way to read a file with “space” in its name, let’s identify the error it can give. So when you try to read the above-created file (my file), you will get an error:

$cat my file

Now, let’s see what happens when you try to write something to the file:

$echo “This is Linux” >> my file

As it can be seen that the above command, instead of writing the “my file,” creating a new file by the name of “my” and saving text to it. So, how to access such a file? Well, there are two approaches:

  • Using escape character, i.e., “\<space>”
  • Using apostrophes or quotation marks

So first of all, let’s insert some text into the above-created file using ;“\<space>”:

$echo “This is Linux” >> my\ file

To read it, use:

$cat my\ file

Or:

$echo “This is Linux” >> ‘my file

Now, to read it, use:

$cat ‘my file

How to access a directory with space in its name:

While working in a terminal, accessing a different directory is one of the common tasks. So when you try to access the directory with a “space” in its name, then it would give an error:

$cd my directory

It becomes challenging, especially to access a path that has a folder with “space” in its name. So, the easiest way is to use apostrophes, quotation marks, or escape character (\).

$cd dir/’my directory’

You can use backslash “\” as well:

$cd dir/my\ directory

How to access two or more files/directories with space in their names:

To access multiple files/directories, you can either use apostrophes separately with each file/directory name or apply apostrophes on the whole path. Let’s understand it with an example:

$sudo cp ‘my dir/’my file’ files

Or:

$sudo cp ‘my dir/my file’ files

In the above command, I am copying a file “my file” from the directory “my dir” to the “files” folder. In the first command, I used apostrophes separately, while in the second command, I used them with the whole path, both will do the same job, but the latter would be much easier to remember.

Conclusion:

Accessing a file/directory in the terminal with “space” in its name sound like an easy job until you get an error, significantly while changing the directory or copying files to a path. There are two main ways to handle such files or directories; one uses escape characters, i.e., backslash (\<space>), and the second is using apostrophes or quotation marks. Using backslash can be confusing; it’s easy and better to use quotation marks or apostrophes. Pressing “tab” is another quick way to autocomplete the path while working in the terminal.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.