<

Go Premium for a chance to win a PS4. Enter to Win

x

Spaces and special characters in file names for linux

Published on
11,323 Points
4,223 Views
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'
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
Comment
Author:savone
1 Comment
 
LVL 3

Expert Comment

by:pitt7
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 \.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Join & Write a Comment

Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to moveā€¦
Suggested Courses
Course of the Month12 days, 3 hours left to enroll
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month