« Back to the arch btw Forum

Filenames with spaces, apostrophes, etc.

Posted by Relish Ubiquitous

posted
updated

Forum: arch btw Group

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



$ less filename\ with\ spaces

or

$ touch 'filename with spaces'

or 

$ 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"

and

$ touch '05".txt'


Files that start with -


rm ./-iputadashinthefoldername


Other


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
`cmd`
Command substitution
$(cmd)
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
#
Comment
$
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


\NNN
The character whose ASCII code is NNN (octal)
\ \
Backslash
\a
Alert (bel)
\b
Backspace
\c
Suppress trailing newline
\f
Form feed
\n
New line
\r
Carriage return
\t
Horizontal tab
\v
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

posted

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).


Permalink Report Reply

Reply by Macky

posted

I made a blog post about quoting and blackslash escaping characters

https://macklemurr.wordpress.com/2022/05/12/quoting-and-backslash-escaping/


Permalink Report Reply

Reply by Macky

posted

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


Permalink Report Reply

Reply by Relish Ubiquitous

posted

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


Permalink Report Reply