Cd is a command-line utility to change the working directory.

learn more… | top users | synonyms

79
votes
6answers
7k views

Why is cd not a program?

I've always wondered why cd isn't a program, but never managed to find the answer. Anyone know why this is the case?
62
votes
25answers
10k views

Quick directory navigation in the terminal

I would like to frequently switch between directories that are in totally unrelated paths, for example /Project/Warnest/docs/ and ~/Dropbox/Projects/ds/test/. But I don't want to type cd ...
44
votes
6answers
3k views

Is there a way to cd back multiple times in bash?

I often use cd - to go back to where I was. How can I do this multiple times in bash? Or would zsh or some other tool support this?
44
votes
5answers
2k views

cd by just typing the directory's name?

I either had this somewhere 20 years ago or I dreamed about it. Basically: If if type blobblob I get blobblob: command not found Fair enough. I would like it so that when my shell gets those ...
40
votes
7answers
3k views

cd .. on root folder

Why cd .., typed at root folder, does not warn or fails with an error? I would expect: /$ cd .. -bash: cd: ..: No such file or directory Instead, I'm left at /. Of course, this is since .. does ...
34
votes
7answers
3k views

Is `-` used only with cd?

cd - can switch between current dir and previous dir. It seems that I have seen - used as arguments to other commands before, though I don't remember if - means the same as with cd. I found that - ...
33
votes
4answers
27k views

What is the 'working directory' when cron executes a job

I have a script that works when I run it from the command line, but when I schedule it with cron I get errors that it cannot find files or commands. My questions is twofold: When I schedule a cron ...
33
votes
2answers
1k views

What is the point of the `cd` external command?

As referenced in this fine answer, POSIX systems have an external binary cd in addition to the shell builtin. On OS X 10.8 it's /usr/bin/cd. You can't use it like the builtin cd since it exits ...
32
votes
2answers
22k views

How do I use pushd and popd commands?

What are the practical uses of both pushd and popd when there is an advantage of using these two commands over cd and cd -? EDIT: I'm looking for some practical examples of uses for both of these ...
27
votes
4answers
3k views

How to go to the previous working directory in terminal?

In terminal, how can I define a key to go to the previous directory which I was in when changing directory with the cd command? For example, I'm in /opt/soft/bin and I cd into /etc/squid3 and I want ...
24
votes
6answers
2k views

Why can't I redirect a path name output from one command to “cd”?

I am trying to get cd to accept a directory name redirected to it from another command. Neither of these methods work: $ echo $HOME | cd $ echo $HOME | xargs cd This does work: $ cd $(echo $HOME) ...
20
votes
6answers
4k views

where is `cd` located? [duplicate]

In a bash sub shell I get the following error when running cd sudo: cd: command not found This is expected because I don't have a path. Usually to work around this I just provide the full path like ...
20
votes
3answers
2k views

How do you enter a directory that's name is only a minus?

I downloaded lessn to my webserver and unzipped it. It contains a folder named -. I assumed I know how to deal with that, but I don't. I tried cd -- -, but that doesn't have the desired effect. ...
20
votes
4answers
2k views

Automatically detect when I typed “vi” but meant “cd”?

Title says it all. About 5 times a day, I type "vi" when I meant "cd", and end up opening a directory in vi. It's making me NUTS. It seems like there should be a way to detect when I type in "vi + ...
20
votes
1answer
3k views

Execute bash scripts on entering a directory

What is the best way to execute a script when entering into a directory? When I move into a new directory I would like bash to execute the projectSettings.bash script much like RVM does.
19
votes
2answers
3k views

What is the bash shortcut to change to the previous directory?

Sadly, I only learned about this last year by stumbling upon it randomly on the internet. I use it so infrequently that I always forget what it is by the time I need it again. How do you change to ...
17
votes
6answers
2k views

Why do I have to cd out of a deleted directory?

On my server I have a directory structure looking something like this: /myproject/code I normally have an ssh connection to the server and 'stand' in that directory: root@machine:/myproject/code# ...
16
votes
4answers
5k views

Why does `cd` command not work via SSH?

I was trying to backup some files via SSH but instead of tar'ing the ones I wanted I got my home folder. I did some further testing and it boils down to this: ssh root@server /bin/sh -c "cd /boot ...
16
votes
4answers
1k views

Do we have more history for cd?

cd - can move to the last visited directory. Can we visit more history other than the last one?
15
votes
1answer
5k views

Make cd follow symbolic links

I have my code mounted as an sshfs in my home directory, but the hierarchy is difficult to remember, so I created a symlink in my home directory leading to that directory. Is there a way so that when ...
14
votes
6answers
11k views

Execute a specific command in a given directory without cd'ing to it?

Is there a way to execute a command in a different directory without having to cd to it? I know that I could simply cd in and cd out, but I'm just interested in the possibilities of forgoing the extra ...
14
votes
3answers
2k views

Changing parent directory (../) with symlinks

I have a symlink ~/link -> ~/a/really/long/path When I do cd ~/link cd .. it takes me to ~ but I want to go to ~/a/really/long Is there a way to do this? I am using bash.
12
votes
6answers
1k views

Why does “ls” require a separate process for executing?

Why does ls require a separate process for its execution? I know the reason why commands like cd can't be executed by forking mechanism but is there any harm if ls is executed without forking?
11
votes
4answers
11k views

Script to change current directory (cd, pwd)

I want to run a script to simply change the current working directory: #!/bin/bash cd web/www/project But, after I run it, the current pwd remains unchanged! How can I do that?
10
votes
4answers
5k views

symbolic link to a directory and relative path

I've created symlink with absolute path to the directory (Blink) and have for example following tree: $ ls -l /tmp/A total 0 lrwxrwxrwx 1 root root 6 Apr 3 12:27 Blink -> /tmp/B -rw-r--r-- 1 root ...
9
votes
2answers
849 views

Change directory without typing cd?

Is it possible to make bash change directory in command line simply by typing that directory without any commands like cd? For example is it possible instead of writing this: $ cd /tmp I just want ...
9
votes
1answer
684 views

How do I redirect output to cd?

Is it possible to redirect a command's output to cd? For example, I searched for a directory using locate and got the path to it. Now, instead of writing a cd path, can I redirect the locate output to ...
9
votes
5answers
4k views

How can I open a new terminal in the same directory of the last used one from a window manager keybind?

I'm using a tiling window manager and I switched from gnome-terminal with multiple tabs to multiple urxvt instances managed by the window manager. One of the features I miss is the ability to open a ...
8
votes
3answers
9k views

~ is $HOME, but sometimes?

This is kind of a trivial and dumb question. cd ~ does the same thing as cd $HOME which is also the same as cd /home/tandu However, cd ~not-tandu changes to /home/not-tandu Is this ...
8
votes
3answers
247 views

Going into a directory linked by a link

When I cd a link, my current path is prefixed with the link's path, rather than the path of the directory the link links to. E.g. ~/dirlinks/maths$ ls -l logic lrwxrwxrwx 1 tim tim 71 Jul 27 10:24 ...
8
votes
3answers
1k views

How can I cd to the previous/next sibling directory?

I often have a project directory layout like this project `-- component-a | `-- files... `-- component-b | `-- files... `-- component-c `-- files... I'll usually be working in one of the ...
7
votes
4answers
4k views

Make cd automatically ls

I find that I often do the following: %> cd bla/bla %> ls I would like it that whenever I cd into a directory it automatically does an ls. I fiddled with my .bashrc for a while, but couldn't ...
7
votes
4answers
1k views

Combined `mkdir` and `cd`? [duplicate]

is there any way (what is the easiest way in bash) to combine the following: mkdir foo cd foo The manpage for mkdir does not describe anything like that, maybe there is a fancy version of mkdir? I ...
7
votes
1answer
591 views

Show message when cd into specific directory

How can I display a message when I cd into a specific directory? This directory is a local one, and I just need a reminder when I go into it from the terminal.
7
votes
2answers
2k views

What does “cd &” mean?

I know how to change directory from terminal by typing cd <directory> but here's the scenario. I was using terminal to go on my git repository's folder. glennRepositories ...
6
votes
3answers
2k views

is it possibe to change parent shell's working directory programmatically?

I want to write some code to allow me switching to some directories that I usually go to. Say this program is mycd, and /a/very/long/path/name is the directory that I want to go to. So I can simply ...
6
votes
4answers
680 views

How to make `cd dir/filename` take me to dir/?

I would find it very convenient to be able to use cd with a file argument. cd myDirectory/anyname.anyExtension would be equivalent to cd myDirectory/ What would be the best alias or function to ...
6
votes
2answers
464 views

Why `.` is displayed by `ls`?

After reading the question When you type “ls -a”, what is the significance of “.” and “..”?, I have a similar one. Why to display .? I mean, is there any directory where cd . would not work? How is ...
6
votes
2answers
225 views

Output of which command used for input to cd

I would like to take the output of a which command, and cd to the parent directory. For example, say I have the following: which someprogram With output: /home/me/somedirectory/someprogram And I ...
6
votes
1answer
2k views

gnome-terminal: keep track of directory in new tab

I'm on Arch linux, and when I open a new terminal tab, it always goes to $HOME. How can I make it so that when I open a new tab, it opens the shell in the directory I was in previously?
6
votes
2answers
589 views

Run a script automatically as I switch to a directory [duplicate]

Possible Duplicate: Execute bash scripts on entering a directory I am running an environment which requires some very specific tool versions. The commands to set up the environment are in a ...
6
votes
1answer
138 views

cd and ls behave differently when inside a softlink to a dir

When we cd into a dir via its softlink, i observe: ls .. will show the content of the parent dir of the linked dir, while cd .. will go under the parent dir of the softlink. Why are they different? ...
5
votes
2answers
780 views

Why doesn't 'sudo bash && cd desktop' cd in to the desktop folder? [duplicate]

I have a folder with chmod 000 permissions on it with a lot of different stuff in, the way I get in is a start bash in sudo by running sudo bash. Why can't I do &&? I want to cd into the ...
5
votes
4answers
982 views

chdir(<directory>) doesn't change directory after exiting to shell

I wanted to make a simple C program to change to my project directory. Its content is following: #include <unistd.h> int main(){ chdir("/home/enedil/projects/algo"); return 0; } I ...
5
votes
2answers
2k views

How can I cd into a directory as root?

Let's say there's a directory that I don't have the privileges to access. Obviously sudo cd foo won't work, because cd is a shell builtin in every shell ever. So far, I've just been using sudo bash ...
5
votes
2answers
185 views

How does bash continue to work correctly when you move its working directory?

On Linux, I moved a directory to another directory using mv, and in another shell, my working directory happened to be the directory that was moved. I ran hg pull in that shell, and to my surprise, ...
5
votes
3answers
327 views

cd to directory of a symbolically linked file

Before I write a script, anyone know an easy way to do the following: $ pwd /foo/bar $ ls -l lrwxr-xr-x 1 username admin 48 Apr 17 2012 foo.sh -> /bar/foo.sh $ cd /bar $ ls foo.sh i.e., in ...
5
votes
2answers
3k views

How do I escape a sub-directory name with an ampersand in it?

Running a kornshell and trying to traverse a directory tree. Want to cd to a sub-directory named as follows: -3ab_&_-3dc.img My question is HOW do I need to escape the ampersand in this name? ...
5
votes
2answers
922 views

How to go back n levels of directories from the command line?

For example, I do the following cds in succession- cd /tmp cd /home/admin cd /root/ cd /some_other/directory Now I am in /some_other/directory. Now, is there anyway to go back to /tmp, the ...
5
votes
2answers
1k views

Behavior of stdin/stdout in conjunction with subshells and cd command

I have the following series of commands: cd / && ls | ( cd /tmp && cat >dumpfile) This series of commands does the following: it creates a file named /tmp/dumpfile. This file ...