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
  • 67,791
  • 32
  • 179
  • 269
Aman
  • 221
  • 2
  • 5

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,714
  • 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 01: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'
Back.Slash
  • 2,166
  • 2
  • 17
  • 22
5

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

glenn jackman
  • 17,900
2

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

bross
  • 591
  • 2
  • 5
  • 7
  • 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)

iheggie
  • 159
  • 4