« Back to the arch btw Forum

Filenames with spaces, apostrophes, etc.

Posted by Relish Ubiquitous


Forum: arch btw Group

Filenames with spaces can be worked with in a couple different ways:

$ less filename\ with\ spaces


$ touch 'filename with spaces'


$ touch "filename with spaces"

In the first example the escape character (\) is used, e.g.

$ cd /media/Stuff/My\ Data/

same as,

$ cd /media/Stuff/"My Data"

What if you have quotes in the filename??

In this rare occurrence, you simply wrap the filename with the opposite (single or double) quotation!

$ touch "05'.txt"


$ touch '05".txt'

Files that start with -

rm ./-iputadashinthefoldername


Other special characters such as ? or # can be dealt with similarly to working with spaces, using the escape character to change the interpretation.

UNIX shell metacharacters

Output redirection
Output redirection (append)
Input redirection
File substitution wildcard; zero or more characters
File substitution wildcard; one character
[ ]
File substitution wildcard; any character between brackets
Command substitution
Command substitution
Pipe (|)
Command sequence, sequences of commands
OR conditional execution
AND conditional execution
( )
Group commands, sequences of commands
Run command in the background, background processes
Expand the value of a variable
Prevent or escape interpretation of the next character
Input redirection 

stderr and stdout redirection

Redirect stderr to a file using the 2> metacharacter:

$ command 2> file

Redirect stdout and stderr to the same file:

$ command > file 2>&1

Redirect stdout and stderr to different files:

$ command > file 2> errors

Escaped characters

The character whose ASCII code is NNN (octal)
\ \
Alert (bel)
Suppress trailing newline
Form feed
New line
Carriage return
Horizontal tab
Vertical tab

Further reading

Reserved Characters and Words

Fixing Unix/Linux/POSIX Filenames:  Control Characters (such as Newline), Leading Dashes, and Other Problems

Report Topic

4 Replies

Sort Replies:

Reply by Relish Ubiquitous


All of this basically has to do with how the shell interprets input or stdin.

In UNIX-like systems there are further wildcard specificities. e.g. 

[!abc] to match a character not in the bracket
[!a-z] to match character not in the range

I/O redirection and globbing are sort of their own topics but related, and as one goes further into patterns and strings, stuff like regular expressions and sed & AWK (which I have a longstanding fascination with but haven't delved too deeply into yet).

Report Reply

Reply by Macky


I made a blog post about quoting and blackslash escaping characters


Report Reply

Reply by Macky


Also, I am basically finishing up a presentation for the introduction to the Linux Command Line. ur prolly dont need it but if anyone is interested i can send it lol

Report Reply

Reply by Relish Ubiquitous


Absolutely.  I learn basic stuff that I never knew about all the time!

Report Reply