12

When I want to navigate into directories having special characters in their names I get an error message.

For example:

aman@desktop:~/Aman$ cd !)e$!gn&(reate
bash: !: event not found
aman@desktop:~/Aman$ 

Here !)e$!gn&(reate is the directory name where I want to navigate.

How to rectify this error?

How to use cd command to navigate directories in Ubuntu having directory name with special characters?

1
  • 1
    You can turn off history expansion: set +H -- turn it back on with set -H – glenn jackman Apr 23 '14 at 16:24
19

That error is happening because the ! (bang) is a shortcut that allows you re-run a command from your history. Ex: This command re-runs command #1504 from my history.

$ !1504

To avoid that issue, try encapsulating your directory name with single quotes.

$ cd '!)e$!gn&(reate'

That being said, while Ubuntu (Linux) will let you name directories in this way, I would highly recommend against it. Using special characters in directory names can make them difficult to read, and if you are using a reserved character you will always need to escape it or encapsulate the directory name in quotes.

4
  • 1
    +1 especially for the last paragraph... and if they can avoid spaces too, will be grateful in the future. Just MHO. – Rmano Apr 23 '14 at 16:41
  • @Rmano Yes, I'm with you on that. Spaces in directory/file names drive me crazy. – Aaron Apr 23 '14 at 16:54
  • 2
    set +H or set +o histexpand to disable the special treatment of !. Add that to your ~/.bash_profile or even some system-wide config file and you'll have one less thing to worry about. Unless you are actually using this feature, that is. – MvG Apr 23 '14 at 22:28
  • I'm not sure what this is called, but I call it "passing the file name as a string" since that is what it would be called in programming. – JFA Apr 24 '14 at 1:02
10

You use \ before special characters

cd \!\)e\$\!gn\&\(reate

You can also put entire foldername in single quotes

cd '!)e$!gn&(reate'

You can encapsulate entire path within single quotes

cd 'Desktop/!)e$!gn&(reate'

if you want to refer a path inside /home/username directory: just add ~/ before path in single quotes

cd ~/'Desktop/!)e$!gn&(reate'
5

Interactively, type cd ! then hit Tab and bash will fill in the rest, properly escaped.

2
  • Will that work even if the first character is a special character, or will you have to type cd \! then TAB? – IQAndreas Apr 24 '14 at 4:19
  • works for me without escaping. – glenn jackman Apr 24 '14 at 10:28
2

Not sure if this applies to special characters, but perhaps put the path inside double quotes?

1
  • 2
    Double quotes will help with special characters like spaces that aren't interpolated, but won't help with $, ! and the like, which are interpolated. There are two types of strings in bash: double quotes, which allow for interpolation, and single quotes, which don't. (This is why you should always put your regex for grep, sed, etc. in single quotes.) – Livius Apr 23 '14 at 22:03
2

Confirming @glenn jackman's answer works in practise, I typed cd ! and then pressed [tab]

ianh@abe:~/tmp$ mkdir '!)e$!gn&(reate' ianh@abe:~/tmp$ cd \!\)e\$\!gn\&\(reate/ ianh@abe:~/tmp/!)e$!gn&(reate$

Even making a directory with '!1997' and typing cd !1 and then pressing [tab] worked.

ianh@abe:~/tmp/!)e$!gn&(reate$ mkdir '!1997' ianh@abe:~/tmp/!)e$!gn&(reate$ cd \!1997/ ianh@abe:.../!)e$!gn&(reate/!1997$

(i dont have enough rep points to add it as a comment)

Not the answer you're looking for? Browse other questions tagged or ask your own question.