Tag Info

Hot answers tagged

125

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


72

If you're just switching between two directories, you can use cd - to go back and forth.


67

Bash: shopt -s autocd Zsh: setopt autocd tcsh: set implicitcd Also, 'autojump' is a useful tool. Once installed it remembers directories so that you can type j abc and if you've visited abc before, say x/d/f/g/t/abc then it will cd to there! https://github.com/joelthelion/autojump


59

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


52

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


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


39

You can use cd - or you could use cd $OLDPWD


38

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


38

With the assumption that you call vi with the directory as the last argument: vi() { if [[ -d ${!#} ]]; then cd "$@" else command vi "$@" fi }


31

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


31

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

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

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

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

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


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


28

It looks to me like the Project directory has a space at the start of it, so try: cd " Project" If it's something other than a space (say a tab) then cd *Project should do the trick.


28

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


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


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


23

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


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

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


21

In bash there is also autocd option. You can enable it by using shopt -s autocd: pbm@tauri ~ $ shopt -s autocd pbm@tauri ~ $ django # Now just type this cd ./django <- it's done automatically pbm@tauri ~/django $


21

Add cd /home/xxxx/Documents/Scripts/ if you want your job to run in that directory. There's no reason why cron would change to that particular directory. Cron runs your commands in your home directory. As for ssmtp, it might not be in your default PATH. Cron's default path is implementation-dependent, so check your man page, but in all likelihood ssmtp is ...


21

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.


21

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


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. :)


19

I don't know if this counts, but you can make a subshell: $ (cd /var/log; cp *.log ~/Desktop) The directory is only changed for that subshell, so you avoid the work of needing to cd - afterwards



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