cd Command Linux and Mystery Flags

Posted by Jason R. Mick on Super User See other posts from Super User or by Jason R. Mick
Published on 2012-04-25T19:57:07Z Indexed on 2014/06/11 15:31 UTC
Read the original article Hit count: 223

Filed under:
|
|
|

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 ..?

    EDIT
    [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?

    EDIT
    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....

    EDIT2
    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.

© Super User or respective owner

Related posts about linux

Related posts about command-line