53

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?

2

8 Answers 8

55

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"
0
29

A little tip: tab completion ;-)

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

  1. 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.
0
24

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/
0
23

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.

0
12

C-like strings and $'string'

Among other things, one can use $'...' type of quoting to make use of ANSI-C backslash characters such as \n and \t, including those you've mentioned. From bash 4.3 manual:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

This is particularly useful with files which contain newlines, tabs, when you're writing complex awk lines where you need to make use of different ways to distinguish between single and double quotes, when filenames themselves contain single/double quote mishmash,etc.

For example, creating and listing such files:

$ touch a$'*'b  c$'\n'd                                                     
$ ls  a$'*'b  c$'\n'd                                                       
a*b  c?d

You can make use of character hex values, such as:

$ touch 'file(name'
$ ls file$'\x28'name
file(name

printf

Same idea as before - take advantage of escape characters:

$ ls "$(printf "file\x28name")"                                             
file(name
$ echo "Hello World"  >  c$'\n'd                                            
$ cat "$(printf "c\nd")"                                                    
Hello World

Use inodes:

Every file or directory has special data structure associated with it called inode, which are referenced by a particular decimal number. So you can use that to indirectly locate the file with particular inode via find command, and do something with it:

$ echo "This is a test" > file$'('name1
$ ls -i
5898996 file(name1  5898997 file?name2
$ find -type f -inum "5898996" -exec cat {} \;
This is a test

Avoid dealing with individual files when you can use glob

When you don't have to deal with individual files, just take advantage of * glob character in shell and quote variables when passing them to other commands. It makes dealing with difficult filenames much easier:

$ for f in ./*; do echo "$f" ; done
file    name2
file(name1

Note the use of ./ - a safeguard against filenames which may contain leading - in them.

0
6

To open a folder containing a space surround it in quotes like cd "Some Directory" or escape the space with a backslash, like: cd /home/kudic/Radna\ površina.

3
  • 3
    Or escape the space with a backslash, like: cd /home/kudic/Radna\ površina
    – Timo
    Jul 10, 2012 at 13:43
  • Great point! I forgot to mention that. I normally use quotes out of habit, but the backslash is actually better to use in the long run. Jul 10, 2012 at 13:45
  • 1
    Or use single quotes ('Radna površina'), if you don't want environment variables ($VARNAME) to be expanded and commands enclosed in backticks or $() to be run (or if there are double-quotes in the filename). Jul 10, 2012 at 13:49
4

If this directory is in your home folder then type:

cd "Milano, Torino (Jan)-Compressed"

else give absolute path:

cd "/…/…/Milano, Torino (Jan)-Compressed"

if there is a double quote in file name then escape that with \"

3
  • If you start a path with a leading forward slash, it goes from root. You might want to remove that.
    – isaaclw
    Feb 5, 2012 at 21:52
  • @isaaclw That is why he filed it as an absolute path :P
    – user13091
    Feb 6, 2012 at 1:16
  • 1
    Ah, that's three dots, indicating a "variable" folder. I assumed it was two dots, indicating "parent folder". Apologies.
    – isaaclw
    Feb 6, 2012 at 3:43
3

Another option although not the best in this case is to use wildcards. You can try:

cd *Torino*

It is best to use this method when there is a distinct word or phrase in the name of a directory not shared by others. For example I have mount points /media/DataSSD and /media/DataHDD. Autocompletion doesn't work until I type nearly half of the name so to get to my HDD partition I just type

cd /media/*HD*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .