Spaces and special characters in file names for linux

Published on
11,614 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.
Author:Steven Vona
Enjoy this complimentary article view.

Get unlimited access to our entire library of technical procedures, guides, and tutorials written by certified industry professionals.

Get 7 days free
Click here to view the full article

Using this article for work? Experts Exchange can benefit your whole team.

Learn More
Experts Exchange is a tech solutions provider where users receive personalized tech help from vetted certified professionals. These industry professionals also write and publish relevant articles on our site.
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Learn from the best.