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 'rootadminz docs' use the syntax below

$ touch 'rootadminz docs'

 

Sample Output:

root@rootadminz:/opt# $ touch 'rootadminz docs'
root@rootadminz:/opt# 
root@rootadminz:/opt# ls
'rootadminz 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 'rootadminz docs'

 

Alternatively, you can use the syntax below

$ cat file\ name\ with\ spaces

 

Let's add some text to the 'rootadminz docs' file

$ echo "Hello World! Welcome to rootadminz" >> 'rootadminz docs'

 

To view the file execute the command below

$ cat rootadminz\ docs

 

Sample Output:

root@rootadminz:/opt# echo "Hello World! Welcome to rootadminz" >> 'rootadminz docs'
root@rootadminz:/opt# 
root@rootadminz:/opt# $ cat rootadminz\ docs
Hello world! Welcome to rootadminz
root@rootadminz:/opt#

 

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 'rootadminz files' run

$ mkdir rootadminz\ files

 

Sample Output:

root@rootadminz:/opt# mkdir rootadminz\ files
root@rootadminz:/opt# 
root@rootadminz:/opt# ls -l
total 4
-rw-r--r-- 1 root root       0  Aug 6 15:54  'rootadminz docs' 
drwxr-xr-x 2 root root    4096  Aug 6 16:20  'rootadminz files' 
Hello world! Welcome to rootadminz
root@rootadminz:/opt#

 

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 'rootadminz files' execute the command below

$ cd rootadminz\ 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

OR

$ cp -R 'directory name'  /destination/path/

 

For  example to copy 'rootadminz files'  to /home/vyga path execute

$ cp -R 'rootadminz files'  /home/vyga/

OR

$ cp -R rootadminz\ files /home/vyga

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)