How do I enter a file or directory name containing spaces or special characters in the terminal? [duplicate]
I want to enter the following folder in the terminal:
Milano, Torino (Jan)-Compressed
How should I write the command cd
to enter this directory?
Spaces and several other special characters like \
, *
, )
, (
and ?
cause problems when I try to use them in the command line or scripts, e.g.:
$ cd space dir
bash: cd: space: No such file or directory
$ cat space file
cat: space: No such file or directory
cat: file: No such file or directory
$ cat (
bash: syntax error near unexpected token `newline'
$ echo content >\
> ^C
$ ls ?
( ) * ? \
How do I enter file or directory names that contain special characters in the terminal in general?
Solution 1:
That command is ambiguous because spaces are normally used to separate arguments. cd does not know what you want to do but you have two possibilities to solve it:
Either you "mask" the spaces (and all other special characters) so that the terminal knows you mean the space as a character and not as a separator:
cd Milano\,\ Torino\ \(Jan\)-Compressed
Or you put your folder name or path into quotes:
cd "Milano, Torino (Jan)-Compressed"
Solution 2:
A little tip: tab completion ;-)
- Just type the first letter e.g
cd Mi
(or more letters if needed) and press Tab. Terminal will help you by completing the rest words.
Another way: drag and drop
- If you can see the directory and if you want to access it using terminal, just type:
cd
first and then drag and drop the directory on the terminal and hit enter.
Solution 3:
Write it as:
cd 'Milano, Torino (Jan)-Compressed'
Otherwise it treats Milano,
as the folder name. This happens because of the spaces in the name of the folder. Alternatively escape a few of the special characters:
cd Milano\,\ Torino\ \(Jan\)-Compressed/
Solution 4:
tl;dr: To quote a special character either escape it with a backslash \
or enclose it in double " "
or single quotes ' '
. Tab ↹ Completion takes care of proper quoting.
What you're asking for is called Quoting:
Quoting is used to remove the special meaning of certain characters or words to the shell. (…) There are three quoting mechanisms: the escape character, single quotes, and double quotes. [citations taken from
man bash
]
Quoting with the escape character \
A non-quoted backslash (
\
) is the escape character. It preserves the literal value of the next character that follows, with the exception of<newline>
.
So to enter a directory or a file with a special character, escape the latter with \
, e.g.:
cd space\ dir # change into directory called “space dir”
cat space\ file # print the content of file “space file”
echo content > \\ # print “content” into file “\”
cat \( # print the content of file “(”
ls -l \? # list file “?”
bash
's Programmable Completion (aka Tab ↹ Completion) automatically escapes special characters with the escape character \
.
Quoting with double quotes " "
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of
$
,`
,\
, and, when history expansion is enabled,!
.
So to enter a directory or a file with a special character, escape at least the latter or a greater part of your filename or path with double quotes, e.g.:
cd space" "dir # change into directory called “space dir”
cd spac"e di"r # equally
cd "space dir" # equally
cat "space file" # print the content of file “space file”
cat "(" # print the content of file “(”
ls -l "?" # list file “?”
As $
, `
and !
keep their special meaning inside double quotes, Parameter Expansion, Command Substitution, Arithmetic Expansion and History Expansion are performed on double-quoted strings.
Quoting with single quotes ' '
Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
So to enter a directory or a file with a special character, escape at least the latter or a greater part of your filename or path with double quotes, e.g.:
cd space' 'dir # change into directory called “space dir”
cd spac'e di'r # equal
cd 'space dir' # equal
cat 'space file' # print the content of file “space file”
cat '(' # print the content of file “(”
ls -l '?' # list file “?”
echo content > '\' # print “content” into file “\”
You can find more about Quoting in man bash
/QUOTING, on wiki.bash-hackers.org and on tldp.org.