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?

Braiam
  • 69,112
Aman
  • 221

5 Answers5

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.

Aaron
  • 6,764
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'
Back.Slash
  • 2,176
5

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

glenn jackman
  • 18,218
2

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

bross
  • 621
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)

iheggie
  • 157