Manage file name with spaces in linux
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 discuss how to manage filename with spaces in a Linux system.
We will cover how to create, read and copy a file which has spaces in their filename.
1) Creating file names with spaces
To create files with spaces in file names, run the command as shown
$ touch'firstname secondname'
For example, to create a file called 'linoxide docs' use the syntax below
$ touch 'linoxide docs'
Output
create and view files with spaces
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 'linoxide docs'
Alternatively, you can use the syntax below
$ cat file\ name\ with\ spaces
Let's add some text to the 'linoxide docs'
file
$ echo "Hello guys! Welcome to Linoxide" >> 'linoxide docs'
To view the file execute the command below
$ cat linoxide\ docs
Output
cat file names with spaces
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 'linoxide files' run
$ mkdir linoxide\ files
Output
create directory names with spaces
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 'linoxide files' execute the command below
$ cd linoxide\ files
cd into directory with directory name with spaces
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
OR
$ cp -R 'directory name' /destination/path/
For example to copy 'linoxide files'
to /home/james
path execute
$ cp -R 'linoxide files' /home/james/
copy directory with spaces in directory name
or
$ cp -R linoxide\ files /home/james
copy directory with spaces in directory name-2
Hope this article explained well on how to manage filename with spaces. Thanks for taking the time to read this article and please leave your comments.