& (or almost any other character, you have two simple possibilities.
- Put single quotes around the whole ting:
- Put a backslash before each troublesome character:
There are two exceptions:
- The single quote method doesn't work for a single quote. Backslash isn't special within single quotes, so you can't directly use that either. What you can do is end the single-quoted string, immediately use backslash plus single quote, and restart the single quote. So for example, if the name of the directory is
cd 'foo'\''bar\qux'. You can remember it that way: inside single quotes,
'\'' gets you a single quote.
- The backslash method doesn't work for newlines: backslash-newline is just ignored. You need to put single quotes around a newline.
You can use double quotes too, but then you need to put a backslash before some characters. Single quotes are more straightforward.
There's an additional difficulty here, which is that the name of the directory begins with a dash. That character tells the
cd command (like almost every command) that an option follows. The dash is not special to the shell, only to the command, so quotes won't affect it. You have two ways of passing an argument that begins with a dash to a command without having it interpreted as an option:
- Find another way to express this argument. For a file name, adding
./ in front still designates the same file.
- Put the argument
-- before. That tells the command to stop looking for options.
So here are some ways you can change into this subdirectory:
cd -- '-3ab_&_-3dc.img'
cd -- -3ab_\&_-3dc.img