<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

Spaces and special characters in file names for linux

Published on
11,285 Points
4,185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Join & Write a Comment

Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month