Spaces in Filename on Linux with Examples

This guide will show you how to Handle Spaces in Filename on Linux distributions with Command Line Examples. You may notice that filenames in Linux do not include spaces. It doesn’t mean that you can’t use spaces, it will be more complicated to have spaces in the filename on Linux distros. You can follow the rest of the article to see the examples of filenames with spaces and how they work.

Handle Spaces in Filename on Linux with Command Line Examples

To complete this guide, you must have access to your server as a root or non-root user with sudo privileges. For this purpose, you can visit the Oracore website and check for the Linux server’s initial setup guides.

Write Spaces in Linux Filename from Command Line Terminal

When you want to create a file that includes spaces in the filename, you may use the following syntax:

touch new file file.txt

Then, use the ls command to verify whether your file has been created or not:

ls

In your output, you will see:

Filename in Linux with SpacesFilename in Linux with Spaces

As you can see, you have three files including new, file, and file.txt. So what should we do to append spaces in the filename?

To write a filename with spaces, you can use quotation. For example, to create the above file as a single filename, you can use the following syntax:

touch 'new file file.tx'

Then, check your file with the ls command:

ls

In your output, you will see you have the file in a single filename:

Create a filename with spacesCreate a filename with spaces

Alternatively, you can use the backslash character “\” to write spaces in filenames. For example:

touch my\ second\ file.txt

Next, run the ls command again:

ls

In your output, you will see you have a single file named my second file.txt:

Write spaces in Linux filenameWrite spaces in Linux filename
Read Linux Filenames with Spaces from Command Line Terminal

If you want to read your filenames that include spaces, you can easily use the cat command to read the files. For example:

cat my\ second\ file.txt

Or you can open your files with your desired text editors like vi editor or nano editor. For example:

vi 'new file file.tx'
Why should you not use spaces in a filename in Linux?

As you can see, writing spaces in a filename or directory may cause errors and problems during your tasks. It will make your tasks more complicated. For example, if you want to copy a single file in a directory and save it in another file, the syntax is like the following command:

cp dir one/my\ text\ file.txt dir\ two/my\ text file.bak

As you can see, the command will be so confusing. It will slow down doing your tasks.

So it is recommended to not use spaces in your filenames and directories.

Use Underscore or Camel case instead of Spaces in Filenames

As we said, it is not recommended to use the spaces. You can easily rename or create a file by using the dashes, underscore, or Camel case to make your file readable. For example:

touch new_file_file.txt
Or
touch new-file-file.txt

Or you can use Camel case. For example:

touch NewFileFile.txt
Conclusion

At this point, you have learned Handle Spaces in Filename on Linux with Command Line Examples. You can easily create your filenames with spaces. But it is not recommended to do this. You can use underscore, dashes, or camel case in your filenames to make your file readable instead of using spaces. Hope you enjoy it.

Clear Chrome Cache in Linux From CLI and GUI

WineHQ LTS Setup on Fedora Linux 39

Leave a Comment

Scroll to Top