I have an external program that generates directories with strings that are negative integers. I am trying to use basic shell commands including cd and ls with this directory, for example:

cd -78059735

Understandably, this fails because this looks like command line options starting with -7.

-bash: cd: -7: invalid option

Similarly, these variations also fail:

cd "-78059735"
cd "\-78059735"
cd '-78059735'
cd '\-78059735'
cd \-78059735

How do I interact with this troublesome directory through the shell?

marked as duplicate by JdeBP, Michael Homer, muru, Romeo Ninov, user88036 Oct 18 '18 at 5:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


It is because the command cd treats the character followed by - as valid option flag for it to work. Since 7 is not a valid one for cd it is failing with the error you are seeing.

In such cases you can specify end of command line options by doing a double dash before the command name -- as below. The below command implies that the command line options for cd are complete and there are no other flags expected after --

cd -- -78059735/

You can even have other flags provided before -- which would work just fine. The below command for mkdir which takes an option -p to create a directory if it does not exist before works just fine with a string having -, provided you give an end of command line options flag right after -p

mkdir -p -- /tmp/-78059735
ls -d /tmp/*

rm command to delete a directory also works just fine as below

rm -vrf -- /tmp/-78059735
removed directory '/tmp/-78059735'
cd ./-78059735
ls ./-78059735

It works for me.

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