Tag Info

Hot answers tagged

123

The cd command modifies the "current working directory", right? "current working directory" is a property that is unique to each process. So, if cd was a program it would work like this: cd foo the cd process starts the cd process changes the directory for the cd process the cd process exits your shell still has the same state, including current working ...


58

cd in addition to being a shell builtin, is actually a program too on POSIX compliant OSes. They must provide independent executables for regular utilities, like cd. This is for example the case with Solaris, AIX, HP-UX and OS X. A builtin cd is mandatory as of course the external implementation doesn't change the current shell directory. However, the ...


41

In zsh, there's an auto_pushd option. This option makes cd behave like pushd. Then you can just use popd to go back to previous directories. ~ $ setopt auto_pushd ~ $ cd / / $ cd /var /var $ cd /usr /usr $ dirs /usr /var / ~ /usr $ popd /var $ popd / $ popd ~ $ In Bash, you can alias cd to pushd. alias cd=pushd The one downside of this is that you ...


37

From the Bash introduction (What is a shell?): Shells also provide a small set of built-in commands (builtins) implementing functionality impossible or inconvenient to obtain via separate utilities. For example, cd, break, continue, and exec) cannot be implemented outside of the shell because they directly manipulate the shell itself. The ...


35

According to the Open Group (responsible for the POSIX standard): Each directory has exactly one parent directory which is represented by the name dot-dot in the first directory. [...] What the filename dot-dot refers to relative to the root directory is implementation-defined. In Version 7 it refers to the root directory itself; this is the behavior ...


30

If it's a small number of directories, you can use pushd to rotate between them: # starting point $ pwd /Project/Warnest/docs # add second dir and change to it $ pushd ~/Dropbox/Projects/ds/test ~/Dropbox/Projects/ds/test /Project/Warnest/docs # prove we're in the right place $ pwd ~/Dropbox/Projects/ds/test # swap directories $ pushd /Project/Warnest/docs ...


30

cd is not an external command - it is a shell builtin function. It runs in the context of the current shell, and not, as external commands do, in a fork/exec'd context as a separate process. Your third example works, because the shell expands the variable and the command substitution before calling the cd builtin, so that cd receives the value of ${HOME} as ...


30

What cd am I using? If you're in Bash cd is a builtin. The type command even bears this out: $ type -a cd cd is a shell builtin cd is /usr/bin/cd cd is /bin/cd The system will use the first thing in this list, so the builtin will be the preferred option, and the only one that works (see the section below on What is /bin/cd). What's a builtin? I like ...


29

The shortcut is - Try cd - If you want to use this in your prompt, you have to refer to it with ~-. See the example: [echox@kaffeesatz ~]$ cd /tmp [echox@kaffeesatz tmp]$ ls cron.iddS32 serverauth.CfIgeXuvka [echox@kaffeesatz tmp]$ cd - /home/echox [echox@kaffeesatz ~]$ ls ~- cron.iddS32 serverauth.CfIgeXuvka


29

There is a shell variable CDPATH in bash and ksh and cdpath in zsh: CDPATH The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. So you can set in your ~/.bashrc: export CDPATH=/Project/Warnest:~/Dropbox/Projects/ds ...


29

The other answers are definitely complete in the direct answer sense. cd - and cd $OLDPWD are definitely the main choices for this. However, I often find that getting into a workflow with pushd and popd works better. Long story short, if you are moving into a directory with the ultimate intent of coming back to where you started, use pushd/popd. Extended ...


27

It serves primarily as making sure the POSIX tool-chest is available both inside and outside a shell. Also, the cd command changes directories but has other side effects: it returns an exit status that helps determine whether you're able to chdir() to that directory or not, and outputs a useful error message explaining why you can't chdir() when you can't. ...


26

İt's a quoting issue. Ssh already runs the command you pass it in a shell. When you pass multiple parameters, they are concatenated with a space in between to build a string. So the remote command that you are running remotely is /bin/sh -c cd /boot && ls -l (no quotes, because the quotes in your command were interpreted by the local shell). /bin/sh ...


24

You want to avoid it from being a parameter, thus we try to prepend something to it. The current directory can be accessed with ., thus the subfolder - can be accessed alternatively with ./-. cd ./- The reason that cd -- - doesn't work is because this is implemented differently if you compare rm (see man rm) to cd (see man bash or man cd), cd has a ...


22

cd does not read standard input. That is why your first example does not work. xargs needs a command name, that is, a name of an independant executable. cd needs to be a shell built-in command and would have no effect (other than verifying that you can change to that directory and the potential side effects it may have like for automountable directories) if ...


22

You don't get an error because even the / directory actually has a valid directory entry for .., but unlike with other directories it points to the directory itself and thus behaves identical to .: $ ls -lid / /. /.. 128 drwxr-xr-x 22 root root 4096 Apr 15 11:26 / 128 drwxr-xr-x 22 root root 4096 Apr 15 11:26 /. 128 drwxr-xr-x 22 root root 4096 Apr 15 11:26 ...


21

Something else you might try is a tool called autojump. It keeps a database of calls to it's alias (j by default) and attempts to make intelligent decisions about where you want to go. For example if you frequently type: j ~/Pictures You can use the following to get there in a pinch: j Pic It's installed system-wide, but included on a per-user basis ...


21

You can make cd a function, and make it detect if you enter that particular directory. cd () { builtin cd "$@" case $PWD in /some/directory) . ./projectSettings.bash;; esac } Do not do this in directories that you haven't whitelisted, because it would make it very easy for someone to trick you into running arbitrary code — send you an archive, ...


21

This is of course a subjective question with many plausible answers. A peculiarity of cd is that it has to be implemented by the shell itself: it cannot be an external command (one that runs in a separate program). cd changes the working directory of the shell process, and only the shell itself can do that. This is a poor question because cd isn't the only ...


20

For April Fool's this year, I wrote a standalone version of cd. No one got the joke. Sigh. Anyone who isn't sure that cd must be built into the shell should download it, build it, and try it. Read its man page, too. :)


20

Here's a quick and dirty way to conveniently bookmark directories and jump back to them: $ a() { alias $1="cd $PWD"; } Go somewhere and type: $ a 1 You now have a command called 1 which does cd the directory that is current at this time. Later just type: $ 1 And presto, back to that directory. I find this very useful.


20

To me the "cd ../code" is a noop. I'm very interested into hearing why it isn't. Because files and directories are fundamentally filesystem inodes, not names -- this is perhaps an implementation detail specific to the filesystem type, but it is true for all the ext systems, so I'll stick to it here. When a new directory code is created, it is ...


19

make mycd a function so the cd command executes in your current shell. Save it in your ~/.bashrc file. function mycd { if (( $# == 0 )); then echo "usage: $FUNCNAME [1|2|3|...]" return fi case $1 in 1) cd /tmp ;; 2) cd /a/very/long/path/name ;; 3) cd /some/where/else ;; *) echo "unknown parameter" ...


18

Bash (as well as ksh, zsh, and even ash) track directory changes so that cd /foo/bar && cd .. always takes you to /foo even if bar is a symlink. Pass the -P option to cd to ignore the tracked change and follow the “physical” directory structure: cd -P .. See help cd or man builtins for documentation about the bash builtin cd. If you really dislike ...


18

If I were you, I would toy around with something like that in my shell configuration file (e.g. ~/.bashrc): reminder_cd() { builtin cd "$@" && { [ ! -f .cd-reminder ] || cat .cd-reminder 1>&2; } } alias cd=reminder_cd This way, you can add a .cd-reminder file in each directory you want to get a reminder for. The content of the file ...


17

You can do this with a function: $ cdls() { cd "$@" && ls; } The && means 'cd to a directory. If successful (e.g. the directory exists), then run ls'. $ cdls /var/log CDIS.custom fsck_hfs.log monthly.out system.log $ pwd /var/log In general, it is a bad practice to rename a command which already exists, especially for a ...


17

It is an expected behavior, and already discussed several times. The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes. To change directory permanently you should source the script, as in . ./script


17

The answer is more or less that ls is an external executable. You can see its location by running type -p ls. Why isn't ls built into the shell, then? Well, why should it be? The job of a shell is not to encompass every available command, but to provide an environment capable of running them. Some modern shells have echo, printf, and their ilk as builtins, ...


16

By saying the & on the end of command line you are instructing your shell to take set of commands and run them in sub-shell on background (not waiting for a result). cd is the bash builtin but it is not important here. It will not affect your "parent" shell in any way, so your working directory will not be changed. [1] 7519 Is reporting that there is ...


16

It is a builtin. See man bash for the details of cd and the Bash Manual for a description of builtins: Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command (see Simple Commands), the shell executes the command directly, without invoking another program. Builtin commands are ...



Only top voted, non community-wiki answers of a minimum length are eligible