How to Tackle Filenames With Spaces in Linux
The one thing you'll notice that files in Linux usually do not contain spaces in their names. Your teacher or colleague use underscore instead of spaces in file and directory names.
It's not that you cannot use spaces in file names in Linux terminal. It's just that it creates additional pain and that's why you should avoid it wherever possible.
Why? Let me show that with examples. You know the generic syntax for Linux commands:
command [options] argument1 argument2
In here, the arguments are separated by spaces. If you try to use filenames with spaces directly, it will be treated as separate arguments rather than just one argument.
In the above screenshot, when I try to use cat agatha books
command, it doesn't understand that agatha books
is a single argument. It treats agatha
and books
as different filenames.
How do you deal with spaces in filename, then? There are two ways:
Wrap the entire filename between quotes:
"file name withn spaces"
Escape every space using backslash key:
file\ name\ with\ spaces
I'll show it in detail.
Read a file with spaces in filename
To use a filename with spaces in it, you can wrap it in quotes like this:
cat "file name with spaces"
You may also escape every space with backslash but it is more work and more confusing than the previous method:
cat file\ name\ with\ spaces
Basically, you put a \
before every space in the filename.
You could also use single quotes instead of double quotes.
cat 'file name with spaces'
Create a file with space in filename
Now, you need to type space in terminal to create the filename here. Use backslash or quotes again.
Similar to the previous section, you can create new files with spaces in the filename using quotes:
touch "file name with spaces"
Or the backslash as well:
touch file\ name\ with\ spaces
Dealing with space in folder name
You can create a directory with space in its name the same way you create a file.
mkdir "new dir"
Now, if you want to switch to this directory, you'll have a path with spaces.
But that should not be a problem for you anymore. To cd into a directory with space, use quotes or backslash again.
cd "new dir"
cd new\ dir
Basically, whenever you have to deal with spaces in names, you use quotes or backslash keys.
Suppose you have to copy a file my file
from this new dir
. Here's what you can do:
cp new\ dir/my\ file
Now it starts to get confusing a bit, right? There are backslashes and forward slashes. It could intimidate a new user or even a seasoned one if there are way too many of those slashes.
It gets even messier when there are backslashes in the filename. Then you'll be seeing double backsplashes.
This is the reason why you should try and avoid using spaces or other special characters in file names. To separate the words in a file name, use underscore.
touch a_very_long_file_name_with_too_many_words
This makes the filenames easier to read and you won't have to make the extra effort to deal with those spaces in the filenames.