coderholic

Howto cd to a dir starting with -

I've been using Linux for over 10 years now, but I ran into a seemingly simple problem today that had me stumped. I wanted to change directory whose name started with a "-" character (a hypen, or dash). Here's what happened:

bmd /dir: ls -folder bmd /dir: cd -folder -bash: cd: -f: invalid option cd: usage: cd [-L|-P] [dir] bmd /dir: cd \-folder -bash: cd: -f: invalid option cd: usage: cd [-L|-P] [dir] bmd /dir: cd "-folder" -bash: cd: -f: invalid option cd: usage: cd [-L|-P] [dir]

I wasn't having much luck! It turns out solution is to pass "--" (two hypens) as the first argument to cd:

bmd /dir: cd -- -folder /home/bmd/dir/-folder bmd /-folder:

I think I remember having to use this trick in the past, but it's such an infrequent problem that I obviously forgot about it. Hopefully by posting about it I'll have a better change of remembering it for next time! :)

Posted on 24 Sep 2008
If you enjoyed reading this post you might want to follow @coderholic on twitter or browse though the full blog archive.