
How to Deal with a Filename with Spaces in Linux/Ubuntu?
In Linux, the files and directory names are usually without white spaces. This is because, in Linux/Ubuntu, the spaces separate the arguments in Linux/Ubuntu commands. Spaces in file names are tricky for users who are new to the Linux environment and new Linux users face unexpected errors while trying to perform operations on a file having spaces in its name. This article will demonstrate the below-listed methods to deal with a filename with spaces on Linux/Ubuntu:
- Why File Name with Spaces Cause an Error?
- How to Manage File Names with Spaces In Linux Ubuntu?
- How to Create a File with Spaces in its Name in Linux/Ubuntu?
- How to Write/Edit a File with Spaces in its Name in Linux/Ubuntu?
- How to Read/Access a File with Spaces in its Name in Linux/Ubuntu?
- How to Move a File with Spaces in its name in Linux/Ubuntu?
- How to Delete a File with Spaces in its Name in Linux/Ubuntu?
Why File Name with Spaces Cause an Error?
In Linux/Ubuntu, the file names are usually without spaces. This is because, in Linux, the arguments are followed by the command as shown in the syntax below:
$ Command [Options] argument1 argument2 argument3
So, if a file name contains a space, Linux assumes that space is a separate argument rather than a file name with spaces as one argument resulting in errors. For example, to access the file via the cat command, execute the following command:
$ cat Sample File 1.txt

As seen from the figure above, the cat command didn’t execute and Linux assumed “Sample”, “File”, and “1” as separate arguments, which do not exist resulting in the “No such file or directory” error.
How to Manage File Names With Spaces In Linux Ubuntu?
Files that contain spaces in their name can be managed either by wrapping the file name in Quotes: Double (“”) or Single(‘’) or by escaping every space by using a backslash (\). In the following sections, the creation, writing, reading, moving, and deletion of files with spaces in their names will be demonstrated by using both quotations and backslash.
How to Create a File With Spaces in its Name in Linux/Ubuntu?
A file name with spaces is created by using double quotations, as depicted in the following command:
$ touch “Sample File 1.txt”
Similarly, a file name with spaces is created by specifying single quotations, as shown in the following command:
$ touch ‘Test File.txt’

The above output indicates that files “Sample File 1.txt” and “Test File.txt” are created using the touch command by wrapping the file name in double and single quotations respectively.
In the following example, a file name with spaces is created by using a backslash(\), as shown in the following command:
$ touch A\ Guide\ To\ Linux.txt

The above output indicates that the file “A Guide To Linux.txt” is created using the touch command and escaping every space using a backslash (\).
How to Write/Edit a File With Spaces in its Name in Linux/Ubuntu?
To write something in a file having spaces in its name, use the echo command with double quotations, as shown in the following command:
$ echo “This is a Sample File” >> “Sample File 1.txt”
Similarly, you can write data into a file having spaces in its name by using the echo command with single quotations as follows:
$ echo “Test Test” >> ‘Test File.txt’

In the following example, a file having white spaces in its name is edited by using backslash(\), as shown in the following command:
$ echo “This is Linux” >> A\ Guide\ To\ Linux.txt

From the above image, it can be seen that the data is successfully written in the selected file without throwing any errors. This can be verified by reading the files in the next section.
How to Read/Access a File with Spaces in Linux/Ubuntu?
A file name with spaces is read by executing the cat command followed by the file name enclosed in double quotations as follows:
$ cat “A Guide to Linux.txt”
Similarly, a file name with spaces is read by using single quotations by executing the following command:
$ cat ‘Test File.txt’

In the following example, a file having white spaces in its name is read by specifying a backslash(\) as shown in the below command:
$ cat Sample\ File\ 1.txt

From the above images, it can be verified that all three files, i.e., “A Guide to Linux.txt”, “Test File.txt”, and “Sample File 1.txt” contain the same content that was written in the previous section.
How to Move a File With Spaces in its name in Linux/Ubuntu?
This section will demonstrate how to move files having spaces in their name from the “Others” directory to the “Test” directory. For demonstration purposes, the “Sample File 1.txt”, “A Guide to Linux.txt” and “Test File.txt” files will be used.
A filename with spaces is enclosed in the double quotations and is executed using the mv command as follows:
$ mv “Sample File 1.txt” /home/linuxuser/Documents/Test
Similarly, a file name with spaces is moved by using single quotations as follows:
$ mv ‘Test File.txt’ /home/linuxuser/Documents/Test
$ ls /home/linuxuser/Documents/Test

From the above image, it can be observed that files, “Sample File 1.txt”, and “Test File.txt” are successfully moved to the “Test” directory from the “Others” directory. Additionally, it can be verified that these files “Sample File 1.txt” and “Test File.txt” no longer exist in the “Others” directory via the ls command:
$ ls

Now let’s move a file having spaces in its name by using a backslash as follows:
$ mv A\ Guide\ To\ Linux.txt /home/linuxuser/Documents/Test

The above output indicates that the file, “A Guide To Linux.txt” is successfully moved to the “Test” directory from the “Others” directory. Additionally, it can be verified that the file “A Guide To Linux.txt” no longer exists in the “Others” directory.
How to Delete a File with Spaces in its Name in Linux/Ubuntu?
A file name with spaces can be deleted by executing the rm command followed by the file name enclosed within double quotations as follows:
$ rm “Sample File 1.txt”
Similarly, a file name with spaces is deleted by using the rm command and single quotations as follows:
$ rm ‘A Guide To Linux.txt’

The above output indicates that the files, “Sample File 1.txt”, and “A Guide To Linux.txt” are successfully deleted and just one file is left in the Test directory.
In the following example, a filename with spaces is deleted by using a backslash as follows:
$ rm Test\ File.txt

The above output indicates that the file, “Test File.txt” is successfully deleted as the Test folder is empty.
Conclusion
Files that contain spaces in their name can be managed either by wrapping the file name in Quotes: Double (“”) or Single(‘’) or by escaping every space by using a backslash (\). This article has demonstrated a detailed process of dealing with “file names with spaces”, i.e., creating, writing, reading, moving, and deleting files on Linux/Ubuntu 22.04 LTS.