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?
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:
Or you put your folder name or path into quotes:
A little tip: tab completion ;-)
cd Mi
(or more letters if needed) and press Tab. Terminal will help you by completing the rest words.Another way: drag and drop
cd
first and then drag and drop the directory on the terminal and hit enter.Write it as:
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: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 with the escape character
\
So to enter a directory or a file with a special character, escape the latter with
\
, e.g.:bash
's Programmable Completion (aka Tab ↹ Completion) automatically escapes special characters with the escape character\
.Quoting with double quotes
" "
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.:
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
' '
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.:
You can find more about Quoting in
man bash
/QUOTING, on wiki.bash-hackers.org and on tldp.org.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: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:
You can make use of character hex values, such as:
printf
Same idea as before - take advantage of escape characters:
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: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:Note the use of
./
- a safeguard against filenames which may contain leading-
in them.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
.If this directory is in your home folder then type:
else give absolute path:
if there is a double quote in file name then escape that with
\"
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*