Most people that use GNU/Linux do not like to deal with the command-line at all, though having a basic understanding of it (such as memorizing commands for mouting devices and copying files for instance) can come in real handy sometimes.
That said however, when dealing with files under command-line, say that you had to use it to backup your data because the desktop session was not working anymore, it is pretty common that one should come across files that contain spaces in their names. And unless you are aware of this simple trick, it can be quite frustrating, because for each space you have to add a backslash ( \ ) when using the command-line.
For example, let us assume that I have a file called Data backup 2.tar on my Home folder, and that I need to copy it to /media/pen location using the ‘cp’ command. If I enter the below command then I will receive an error saying that there exists no such file/directory.
sudo cp Data backup 2.tar /media/pen
Note: I am running ‘cp’ with ‘sudo’ (administrative) privileges.
In fact, if you carefully look at the output, you can see that command-line has treated the file name as three different files due to the three spaces that are present in the file name. So how can we overcome this ?
Well, you can add backslashes as mentioned before and below is the correct command using that.
sudo cp Data\ backup\ 2.tar /media/pen
But as you can see, not only it is time consuming, but since we are not that used to seeing backslashes on file names, it can be really confusing as well. So instead, you can put the file name that contains spaces, between two apostrophes. For this example, I will use the below command and the copying will be carried on without errors.
sudo cp 'Data backup 2.tar' /media/pen
As you see, once you add the first apostrophe, then you can type the file name freely, and when done entering the name, make sure to add another apostrophe (this is very important). That is all there is to it.
If a path contains two or more file names with spaces …
You can do the same. You can add apostrophes separately to each name, or you can put that particular path which contains names with spaces, between two apostrophes, as a whole, which is much easier. Coming back to the above example, let us say the Data backup 2.tar file is inside a folder called untitled folder that is located on my Home folder, then I will use the below command for copying it to the same destination.
sudo cp 'untitled folder/Data backup 2.tar' /media/pen
If both file paths (source and destination) contain such names (or even a mix -- a file path that contains names with spaces and names without them), then you can add apostrophe pairs to each path, separately. Let us take the same example and say that this time we need to copy it over to /media/pen 2/new folder (as you can see this path contains two names with spaces in their names and one without).
For that I will use the below command.
sudo cp 'untitled folder/Data backup 2.tar' '/media/pen 2/new folder'
(Again, please remember that I have used ‘sudo’ with ‘cp’ because I am copying files to a location which requires administrative privileges. Here I have also colored the apostrophe pairs differently so it is easy to understand).
But the rule is, no matter what you do, you should treat the two paths (file source and the destination) individually. In other words, you cannot put the whole command (two file paths) inside two apostrophes, if you do so, then ‘cp’ will treat it as a the file source and will give an error saying the destination is missing.
sudo cp 'untitled folder/Data backup 2.tar /media/pen 2/new folder'
To finish, not just with ‘cp’, you can use this trick with almost any command that requires you to deal with a file name that contain spaces under the command-line in GNU/Linux. Good luck.