How to Read a Filename with Spaces in Linux

Written by: Linuxopsys   |   Last updated: June 22, 2022

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

touch'firstname secondname'

For example, to create a file called 'personal docs' use the syntax below

touch 'personal docs'
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 '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
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 'personal files' run

mkdir personal\ files
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 'personal 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 'personal files'  to /home/james path execute

cp -R 'personal files'  /opt/
copy directory with spaces in directory name

or

$ cp -R personal\ files /opt/
another method to copy directory with spaces in directory name

Conclusion

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.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

1 Comment

1 thought on “How to Read a Filename with Spaces in Linux”

  1. Graphical file managers like Dolphin and Nautilus allow you to browse and access files with spaces without needing special characters. I appreciate the insights you provided in your blog. Thank you for sharing this valuable information.

    Reply

Leave a Comment