Platform: CentOS 6.2 Shell:tcsh

I'm playing around with cd for a BASH script, and noticed the wondrous cd - option, but was left with many questions...

  1. Why the cd -? Isn't this redundant with cd ..?

    [As FatalError points out, these two commands don't do the same things... so the answer is "no"]

  2. Can you delve farther back into your history with - flag, a la in a browser?

    e.g. When I type cd -, it takes me to my previous directory, but then if I enter that command again, it takes me to the directory I just came from, creating a sort of loop.

    Is a shorthand for going back multiple levels supported?

    I realize I can go back with cd .., but was hoping this could be a gateway to a less verbose deep back, e.g. cd -3 vs. cd ../../../ ... hopefully that clarifies what I'm asking....

    As to the current feedback, while .. is a special directory, I don't see a reason why the built-in cd to the terminal couldn't use a shorthand for ../../ ... ../ e.g. cd ..5 or why the built-in also couldn't have a history (a la auto pushd/popd) that could be turned on and used like cd -3. I get that this could be somewhat of security/privacy risk, but I don't see how it's any worst than storing a command history, which most shells/terminals do.

  3. The manpage for cd, accessible via man cd and help cd (it's the same for either command), only lists -L and -P flags.

    However when I type in cd --help it outputs Usage: cd [-plvn][-|<dir>]..

    Am I right in assuming the other flags and the - (back) option are nonstandard?

  4. What are the -n and -v flags for?

    Both seem to take me back to my home directory, that's all I've been able to figure out via experimentation.

A quick read on web resources [1][2] offered just the same sort of info that the man page did and didn't answer my questions.

Note: The second Linux-centric resource above claimed cd only had two options (obviously not true in current CentOS) hence my assumption that this functionality could be non-standard.

  • You might be using a builtin provided by tcsh called cd and not invoking /bin/cd (unless you explicitly specify /bin/cd on the command line) - this probably accounts for the difference in behavior. I'm not knowledgeable enough about tcsh to help you further, though. Try typing help cd (that's for bash, don't know how it will work on tcsh...)
    – LawrenceC
    Apr 25, 2012 at 20:21
  • Just to add, executing /bin/cd would be pretty pointless (cd pretty much has to be a builtin to be useful). I'm pretty sure it only exists to satisfy some obscure POSIX requirement.
    – FatalError
    Apr 25, 2012 at 20:30
  • Ah interesting, you're right, this is tcsh specific... that's kind of odd. I still am curious what the extra flags do. help cd prints the same info as man cd, like I mentioned... no clue as to what the extra flags in the tcsh built-in are for. Apr 25, 2012 at 20:31
  • Regarding 3, multilevel "cd ..": note that command history enables you to do cd .. <Enter> <Up> <Enter> <Up> and so on to quickly traverse backwards through the file tree. Perhaps I mostly find this more convenient because of my specific keyboard locale where / is behind the shift modifier, though. Apr 25, 2012 at 20:56
  • true, but then you have like 3-5 cd .. littering your command history, which is less than optimal. a cd ..5 built in would not only be less typing and easy to implement, it'd also leave your history cleaner. It's amazing to me that I'm possibly the first to have thought of it. :P Apr 25, 2012 at 21:37

1 Answer 1

  1. cd - is not the same as cd ... The cd - command goes to the previous directory you were in. For example, if you're in /home/bob and then you run cd /var/log, then running cd - will take you back to /home/bob. cd .. on the other hand, always goes to the parent directory.
  2. If you want to keep a history like that, I suggest checking out pushd and popd which will let you use a "directory stack":

    user@host:/etc/init.d$ pushd /var/log
    /var/log /etc/init.d
    user@host:/var/log$ pushd /tmp
    /tmp /var/log /etc/init.d
    user@host:/tmp$ popd
    /var/log /etc/init.d
    user@host:/var/log$ popd

    As for stepping back multiple levels in the directory tree, there's no common way that I know of. .. isn't a feature of the shell, it's actually a special directory link in *nix that refers to the parent dir.

  3. cd is a built in command, so it's flags and usage may vary by shell. But, in general the options to cd in bash on one system ought to be the same as on another system.

  4. My bash shell doesn't list either of these flags... are you experimenting using bash or tcsh? If you're using tcsh you're essentially using a totally different command, so you probably want to run bash and mess with cd there.


According to the tcsh man page:

With -p, prints the final directory stack, just like dirs. The -l, -n and -v flags have the same effect on cd as on dirs, and they imply -p.

(You can check the section about dirs if you want to read the specifics). Looks like it's basically making it print the directory stack after the command runs:

host:43> pushd /etc/init.d
/etc/init.d ~ 
host:44> pushd /var/log
/var/log /etc/init.d ~ 
host:45> pushd /tmp
/tmp /var/log /etc/init.d ~ 
host:46> cd -p
~ /var/log /etc/init.d ~ 
host:47> cd -v
0       ~
1       /var/log
2       /etc/init.d
3       ~
host:48> popd
/var/log /etc/init.d ~ 
host:49> cd -v
0       ~
1       /etc/init.d
2       ~

both show the directory stack, but -v seems a little easier to read. The top of the stack (element 0) is the current directory, and 1 is where you'd go if you execute popd once, and so on.

  • 1 -- yup, good point. 2 -- good thought, forgot that about .... I see how that would make shorthand problematic for a true history, which could be in non-connected dirs. 3. Interesting 4. As I mention in the comment, I'm using tcsh... I guess I confused the issue a bit by saying I was looking to write a bash-script, so fixing that now... Apr 25, 2012 at 20:42
  • P.S. I voted you up, but you didn't explain what those non-standard flags do in tcsh's cd built-in, so I'm still waiting for a more complete solution. Also it seems like cd ..5 (go back 5 directories) and cd -5 (go back in a directory history 5 directories) could be useful additions to the built-in shell cd, as I explain in my second edit. Apr 25, 2012 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .