[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Spaces and special characters in file names for linux

Published on
11,547 Points
1 Endorsement
Last Modified:
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer.

The problem occurs when at the command line. Although Linux does not care what you name a file, spaces and special characters cause problems when navigation via a shell.

Spaces usually separate commands, the command arguments or multiple file names. The shell in Linux does not know that all this text is one filename (e.g. My Documents). Also special characters already have a function within the shell (e.g. * is a wildcard) and they cause problems when used in a filename.

There are two different ways I have found around this issue. I will explain them starting with the simplest (in my opinion).

Quoting: This is when you put a sting of text inside of quotes.

For this example with will try to view the contents of a file named "filename with spaces".  The contents of this file is just one line of text, OK NOW IT WORKS! :). The cat command is a program that allows you to view the text inside of a file.

# cat filename with spaces
cat: filename: No such file or directory
cat: with: No such file or directory
cat: spaces: No such file or directory

As you can see, the shell assumed we were trying to run the cat command against three different files named filename, with, and spaces.
If we place the same file name inside of quotes or single quotes the shell with treat the quoted text as one string.

# cat 'filename with spaces'

As you can see when I use the command, cat filename with spaces, it presumed I was speaking of three different files not a single file with spaces in the name.

I corrected this by using quotes (' or ") with the command, cat 'filename with spaces', which tells Linux to treat this text as one word (or treat the spaces as characters).

Another way to deal with special characters in a file name is to escape the characters. You put a backslash ( \ ) in front of the special character or space. This makes the bash shell treat the special character like a normal character.

# cat filename with spaces
cat: filename: No such file or directory
cat: with: No such file or directory
cat: spaces: No such file or directory

# cat filename\ with\ spaces'

This can be quite confusing for someone just beginning to use Linux.  I hope this helps some new Linux users out there as it was hard for me to understand when first learning the new operating system.
1 Comment

Expert Comment

It's very simple to overcome this problem by using tab to auto-complete filenames.
The shell is aware of filenames with spaces and in fact all unix shells are much more powerful than cmd.exe. But even cmd.exe has filename completion with correct space handling.

But how should the shell know if you want to call the program with one argument with spaces or with seperate arguments?
The shell knows... if you use quotes or prefix the space with a \.

Featured Post

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Join & Write a Comment

Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month