Linux Commands

How to Deal with Spaces in File Path Linux

Using Linux operating system and facing problems while dealing with the spaces in file path? Many Linux users encounter this issue. In the Linux operating system, we can run commands by passing multiple arguments. A space separates each argument. So, if we give the path that has a space, it will be considered two different arguments instead of one a single path.

In this article, we will dive deeper into how to deal with spaces in file path Linux? But, before that, we need to know the ls command, the uses of the ls command, the syntax for writing the command on the terminal.

How to Deal with Spaces in File Path Linux

Suppose you are dealing with a specific directory in the Linux operating system. In that case, we can see the list of all files and folders within that particular directory using the ls command. Also, the details such as file owner, permissions can be viewed by using the ls command. The syntax for using any command is given as follow:

Command arg1 arg2

Here you can notice that there is a space between the command and each argument. So if we want to use the ls command, we can simply write as ls on the terminal and execute it.

This will easily return the list of all files and folders on disk in the specified directory. We can also pass arguments along with the command ls:

ls –l

This command will print the list of files with detailed information. Suppose you want to see the detailed information of a specific file, then you can give a second argument as file path after –l flag.

ls –l /etc/paswd

So this is how ls or any other command works. The real struggle is dealing with the spaces given in the path. Suppose you give the file name desktop/My Work in the following way:

$ls –l /desktop/My Work

In this case –l will be considered the 1st argument, /desktop/My will be considered the 2nd argument, and Work will be considered the third argument. That means a single path having space will be considered as two different arguments. In order to avoid that issue, we propose the following:

Use Single or Double Quotes

The most common solution to avoiding the spaces problem in the path is to use double or single quotes for the pathname. If you do so, the system will consider the complete path as a single argument. Let’s see the example here:

ls –l “/desktop/My Work”
ls –l ‘/desktop/My Work’

You can notice that there is a space between My and Work. But we have enclosed the path in the quotes. So, both of these methods will work and give you a result.

Use the Backslash to Avoid the Space

The second method we used here is making use of backslash just before the space between paths. So, we can write our command as:

$ls –l /desktop/My\ Work

This statement looks clumsy, but the backslash avoids the spaces and returns the output.

Things to avoid

  1. Don’t use one single quote and one double quote
  2. Don’t use backslash and quotes together


In this article, we have discussed how to deal with spaces in file path Linux. Moreover, we have discussed the ls command and syntax, the solution to avoid the space in the file path, and things to avoid when applying the solution.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.