It's not very common in Linux to handle filename with spaces but sometimes files copied or mounted from windows would end up with spaces.
While it is not recommended to have file names with spaces, let's discuss how to manage filename with spaces in a Linux system.
In this guide, we cover how to create, read and copy a file that has spaces in its filename.
1. Creating file names with spaces
To create files with spaces in file names, run the command as shown
For example, to create a file called 'personal docs' use the syntax below
touch 'personal docs'
If you want to view such a file with space in the file name, use the same principle of enclosing the file names inside the quotation marks.
2. Read a File with spaces in filename
You can use 'cat' command or open the document using your preferred text editor such as vim, nano or gedit.
cat 'personal docs'
Alternatively, you can use the syntax below
cat file\ name\ with\ spaces
Let's add some text to the
'personal docs' file
echo "Hello guys! Welcome to Linux" >> 'personal docs'
To view the file execute the command below
$ cat personal\ docs
3. Creating directory names with spaces
To create directory names with space in between use the syntax below
mkdir firstname\ secondname
Please note the space after the backslash
For example, to create a directory called 'personal files' run
mkdir personal\ files
4. Navigating to a directory with spaces in the directory name
To navigate to a directory with spaces in its directory name, use the syntax below
$ cd directory\ name
To navigate to the directory 'personal files' execute the command below
$ cd linoxide\ files
5. Copying a directory with spaces in the directory name
To copy a directory with spaces in its directory name to a different location use the syntax below
cp -R directory\ name /destination/path
cp -R 'directory name' /destination/path/
For example to copy
'personal files' to
/home/james path execute
cp -R 'personal files' /opt/
$ cp -R personal\ files /opt/
In this guide, we learned how to manage filename with spaces. Thanks for taking the time to read this article and please leave your comments.