How to Work with File Names containing spaces in Linux

How to Work with File Names containing spaces in Linux

How to Work with File Names containing spaces in Linux


You have probably heard about this notion that using File Names that have spaces is just inviting trouble, and you’d probably like to prove otherwise. Well, you are CORRECT if you differ with the former statement, because it is very possible to work with files whose names have spaces/space characters.

If you have interacted with Linux, you probably know that when you want to work, or reference a certain file that has Space character(s) in its file name, you cannot just type in the file name as-is and expect to achieve what you intended. This is especially noticeable when you are using the Linux Terminal.


How do you work with file names containing spaces?

Whether you are creating/removing/copying/moving files that have Space characters in their file names, you will need to add some hacks to successfully achieve what you desire with the file at hand.

NOTE:// When using, a mouse to drag-and-drop for copying/moving files, Delete Key or Shift + Delete combination to remove files and Text Editors for creating files, you can work with file names containing spaces with NO hitch. However, if you move away from GUI for these basic functionalities and when using various commands (vi, rm, scp, …) to do whatever, then you WON’T proceed with no hitch!


Here is that recipe you’ve been waiting for. There are two ways to Escape the Spaces to prevent the system from viewing the spaces as Delimeters!
Delimeters in this case are character(s) used to indicate the beginning/end of an argument/command/string.
When you hear escape, I know those of you who have interacted with programming languages already know what this means. For those who don’t, Escaping characters is basically just that, escaping or assuming characters.

1. Using Back-Slash (\)

In this technique, we use the Back-Slash (\) special symbol/character, recognized by the Linux-based/UNIX-based operating system as an escape sequence. The Back-Slash (\) here basically makes the Space characters part of the String (the sequence of characters/words used as your file name)!

2. Using Quotation Marks (‘file name’, “file name”, ”’file name”’, “””file name”””)

As you have already noticed, you can use the multiple types of quotation marks Around (at the beginning and end of) the file name.
In this technique, you have 3 options;

i) Single Quotation Marks (‘file name’)
ii) Double Quotation Marks (“file name”)
iii) Tripple Quotation Marks; Single Quotation Tripple Quotation Marks (”’file name”’) && Double Quotation Tripple Quotation Marks (“””file name”””)

NOTE:// The Quotation Marks around the file name have to be balanced and similar. Double Quotation Marks are not the same as Double Single Quotation Marks, that is, “file name” != ”file name”!



How to Work with File Names containing spaces in Linux
Hacking | thetqweb