Spaces and special characters in file names for linux

Steven Vona
CERTIFIED EXPERT
Published:
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'
OK NOW IT WORKS! :)


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'
OK NOW IT WORKS! :)


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
4,711 Views
Steven Vona
CERTIFIED EXPERT

Comments (1)

Commented:
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 \.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community