
When working from a graphical interface such as Gnome and KDE, are you putting spaces in the filename or directory names? Did you know that doing so will make it harder to work with the files from the command line on UNIX like systems?
In this post, we are going to explore this in depth best practices in both how to handle and, how to avoid using spaces in filenames.
Understanding The Impact
This isn’t an extensive list, but the two most common reasons I have encountered for not wanting spaces in the file name are listed below.
Command-Line: When working from a command prompt spaces, and other special characters make it harder because the path can’t be taken as is. This is because certain special characters including the space have a special meaning in shells like bash. The space character is used to specify separate arguments in most command lines.
Shell Scripts: When writing scripts, most Linux commands
How To Deal With Spaces In Filenames
So now that we have gone over why spaces in file names can make things a bit more complicated, what can be done about it?
- Using backslashes to escape spaces is one option, but this means you have to add a backslash before each occurrence.For example, if this is the file you are working with:
/home/user/My Documents/Projects/My Awesome Project/Test File.txt
It would turn into:/home/user/My\ Documents/Projects/My\ Awesome\ Project/Test\ File.txt
Although using an escape character like this does work, it makes things harder to read and can be rather inconvenient for many spaces in filenames. - Using single quotes around the file path If you wrap the path in single quotation marks, you don’t have to modify more. So if you start with the same path as before, You will then end up with the following:
'/home/user/My Documents/Projects/My Awesome Project/Test File.txt'
Note that anything but an orphaned single quote will be taken literally, this includes characters like a backslash, dollar sign, or Backticks. - Using double quotes around the file system path works almost the same as single quotes but allows a few more things to work outside the literal interpretation of single quotes. So characters like a backslash, dollar sign, or Backticks will have meanings beyond a literal interpretation.
"/home/$USER/documents/My\ Project/Backup\ Files"
- Avoiding spaces, the other option is to avoid spaces and to substitute characters like the most popular are the dash or an underscore. So for example:
/home/user/documents/My-Project/Backup-Files
In this example, we are using a dash to replace any spaces.
Conclusion
When working from a graphical interface, putting spaces in the filename or folder name will make it harder to work with the files from the command line. In this post, we explore the impact and what can be done about it on Linux-based operating systems. We looked for ways to handle it, and alternatives by substituting space for other characters.
Looking for More
Done reading and looking for more, why not check out these links?

Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply