1

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?

mattm
  • 181

2 Answers2

5

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/*
/tmp/-78059735

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

rm -vrf -- /tmp/-78059735
removed directory '/tmp/-78059735'
Inian
  • 12,807
3
cd ./-78059735
ls ./-78059735

It works for me.

Gounou
  • 549
  • 3
  • 5