22

In this compilation of sysadmin horrors, one of the authors writes, as a rule of thumb:

Always do a cd . before doing anything.

Why would you want to do that?

3 Answers 3

32

You don't.

At least not just like that. The preceding line in the quoted document is of importance:

  • Set up your prompt to do a pwd everytime you cd.
  • Always do a cd . before doing anything.

This way, you as the operator verify your current working dir before doing anything of importance, as it's printed out with each change. cd . doesn't make any sense otherwise.

This "verification" is a good thing, and you should adapt a form of it. A more (IMHO) common variation of this theme is to always print out the working dir at the prompt.

5
  • 11
    It is probably better to think of this as "always type 'pwd' before doing anything important to make sure you are in the directory you think you are" Jun 30, 2013 at 16:03
  • 4
    Why not just do a pwd before doing anything? Jun 30, 2013 at 18:30
  • 15
    Actually, it does have a use. If you've gone into a directory via a symbolic link, and the link then gets deleted and re-created elsewhere, such as during a server deployment where versions are controlled with a symlink to "current_release", then doing cd . would change the actual underlying directory that you are in.
    – Stewart
    Jun 30, 2013 at 20:05
  • 1
    +1 for "always print out the working dir at the prompt." That is what I do. Jul 1, 2013 at 11:04
  • 4
    @Stewart Unfortunately this won't help if the symbolic link changes after cd . but before doing anything else. It just reduces the likelihood of being in the wrong directory, but doesn't solve the general problem. Unless you switch to the real directory using something like cd $(readlink -f .) given that you actually want to stay there.
    – scai
    Jul 1, 2013 at 12:42
18

If the current working directory of your shell is removed, it is possible to lose data.

For example,

$ pwd
/home/user/test
$ rmdir /home/user/test
$ pwd
/home/user/test
$ some_command | tee command.log
tee: command.log: No such file or directory
<long output>

The output of some_command was not written to the disk.

Typing cd . before running a command would reveal the problem.

$ pwd
/home/user/test
$ rmdir /home/user/test
$ cd .
$ pwd
.

If the current working directory directory was removed and re-created, typing cd . would "refresh" the reference to that directory.

$ ls
foo bar ljz
$ pwd
/home/user/test
$ rmdir /home/user/test
$ mkdir /home/user/test
$ pwd
/home/user/test
$ ls
$ cd .
$ ls 
foo bar ljz
13

I think it's more important to show your current directory.

On every linux server, I modify the prompt in /etc/bashrc by changing "W" to "w".

 [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

to

 [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \w]\\$ "

The effect of this is:

[root@xt include]#

versus

[root@xt /usr/src/spl-0.6.1/include]# 
4
  • is PS1 bash-specific?
    – qdii
    Jun 30, 2013 at 14:04
  • I think so... It's all I use.
    – ewwhite
    Jun 30, 2013 at 14:17
  • 1
    Your changes might get lost with the next system upgrade, tho.
    – ott--
    Jun 30, 2013 at 19:08
  • 2
    @ott-- The stick it your ~/.profile or ~/.bash_profile as well as updating /etc/skel/.profile
    – jscott
    Jun 30, 2013 at 19:14

You must log in to answer this question.

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