Howto cd to a dir starting with -
September 24, 2008
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!
Alternatively use:
cd “\-folder”
just my 2 cents..
Comment by Adam — February 10, 2009 @ 1:05 am
cd ./-folder
Comment by consumer — July 13, 2009 @ 10:17 am