Managing files, such as creating, copying, reading, editing, and moving them, is a basic task in Linux. But working with a file with spaces in the filename differs from a normal file. Many people ask the question Does Linux allow spaces in filenames? The answer is yes; we can perform a task with a file with spaces in the filename.
The filename with spaces with a command is considered another file in Linux. In this article, we will explain the methods to use the filename with spaces using the following supporting content:
- How to Reference File Name with Spaces in Linux?
- Create File and Directory with Spaces in Filename Using Quotes
- Create File and Directory with Spaces in Filename Using Backslash
- Copy or Move a File and Directory with Spaces in Filename
- Change Directory of a File and Directory with Spaces in Filename
- Bonus Tip: Use the Tab Key for Auto-Completion
Let’s start with the reference filename basics.
How to Reference File Name with Spaces in Linux?
Sometimes, we need to create, copy or move a file with spaces in its names, but it does not work like normal files. We have to use two methods to manage the files with spaces in their names: the “quotes” and “backslash” methods.
Let’s create a file using the quotes.
Create File and Directory with Spaces in Filename Using Quotes
The simplest way to manage the files or directories having spaces in their name is by using a single (‘’) or double quotes (“”). The filename enclosed in quotes will be considered a single filename. Several commands are used to create a file in the terminal, which will be utilized in this section to create a file with spaces.
The touch command helps the users to create files by using the quotes in their names. For instance, to create a file named “my new file”, the below command is executed in the terminal:
$ touch 'my new file'
The “my new file” is created.
The double quotes (“”) can be utilized to create the file having the filename with spaces as shown below:
$ touch "my new file"
The cat command can be used to create a file with spaces in the filename. For example, the below cat command is used to create and read the content of a file named “my new file”:
$ cat > 'my new file'
Another method, the redirect method is commonly used to create a file in Linux. By using the > operator with the quotes can create files with reference filename with spaces as performed below:
$ > 'my new file'
The output shows that “my new file” is created.
The echo command is utilized with the redirect operator to create a file in Linux, such as the below command creates and reads the content of the “my new file”:
$ echo > 'my new file'
Different editors are used to create a file where the most popular editors are Linux default nano and the vim editor. For example, the below CLI command creates the file in the nano editor where can desired content can be added directly:
$ sudo nano 'my new file'
Similarly, writing the filename within quotes with the vim or vi keywords creates the file in vim/vi editor as given below:
$ vim 'my new file.txt'
Any shell script file can also be created with spaces in the filename. For instance, to create a Bash Script file concerning filename with spaces in the name, use the below command:
$ touch 'automation.sh'
The bash script file “automation.sh” is created.
The directory with spaces in the filename can be created within the quotes. For example, the below commands mkdir command creates a folder with spaces named “Test Folder”:
$ mkdir 'Test Folder'
Create File and Directory with Spaces in Filename Using Backslash
The backslash (\) is used to manage the filenames with spaces in Linux. We can use the backslash as the escape character for the spaces in the file name. Let’s use the backslash \ to manage the files regarding filename with spaces in Linux.
The touch command uses backslashes to add the spaces within the filename. Such as, the below command will insert space in place of the backslash (\) as shown below:
$ touch my\ new\ file1
The file is created with spaces in the filename “my new file1”.
The file can be created using the“cat” command with backslashes as shown below:
$ cat > my\ new\ file2
The below command will create the file with the filename “my new file3” using the redirect operator (>):
$ > my\ new\ file3
The “my new file3” named file is created.
Copy or Move a File and Directory with Spaces in Filename
To copy or move a file having spaces within the filename; we must use the quotes or backslash. For instance, the below two commands will copy the “my new file” to the Documents folder having spaces in the filename:
$ cp 'my new file' ~/Documents $ ls ~/Documents
The “my new file” is copied to the “Documents” folder.
The “mv” command moves a file or directory to another directory. To move a file concerning filename with spaces to “Downloads”, the below commands can be used in the terminal:
$ mv 'my new file' ~/Downloads $ ls ~/Downloads
We can deal with a file having spaces in the filename & directory in Linux using the quotes and backslash method as well. For example, to copy a file having spaces within the directory name and filename as well the following syntax is used:
$ sudo cp 'Test Folder'/'my new file' ~/Desktop
Change Directory of a File and Directory with Spaces in Filename
The “cd” commands changes the directory and to change the directory of a file having spaces in the filename the quotes or backslashes are used. For instance, any of the following commands changes the directory to the “Test Folder” directory:
$ cd 'Test Folder'
The directory is changed to “Test Folder”.
Bonus Tip: Use the Tab Key for Auto-Completion
If the file has spaces within the filename and is unique, you can simply write the first part of the name and press the “Tab” key for the auto-completion For instance, you have a single file named “automation file”, you just enter the “automation” word in the terminal and press the “Tab” key the filename will automatically be completed.
For instance, in the below command, the “automation” word is entered, and then pressing the “Tab” key auto-completes the filenames starting with the “automation” word:
$ ls automation
That’s all from this reference filename tutorial.
Two methods are used concerning filenames with spaces in Linux, using the “quotes” or “backslash” with the filenames. We can create, copy and move the files having spaces with the filename using the single quotes (‘’), double quotes (“”), or backslash (\). Moreover, to autocomplete a filename or directory name having spaces in the filename, just enter the first word of the name and press the “Tab” key to autocomplete.