346 votes

Combined `mkdir` and `cd`?

I think creating a function is the most appropriate way to do this, but just for listing all alternative ways, you could write: mkdir foo && cd "$_" $_is a special parameter that holds the ...
user avatar
  • 3,561
247 votes

How do I use pushd and popd commands?

There is a really useful use case for pushd and popdcommands for working with several folders simultaneously. You can navigate the stack very easily, since it is enumerated. Meaning, you can have ...
user avatar
  • 2,571
164 votes
Accepted

Combined `mkdir` and `cd`?

Function? mkcdir () { mkdir -p -- "$1" && cd -P -- "$1" } Put the above code in the ~/.bashrc, ~/.zshrc or another file sourced by your shell. Then source it by ...
user avatar
  • 5,464
157 votes

Does cd . have use?

I think this is overthinking the problem. cd . may not be something that one would manually run in the usual course of things, but it definitely is something that can come up in programmatic execution ...
user avatar
  • 4,476
127 votes

Does cd . have use?

The path of the directory could have changed since the last command was executed, and without cd . the bash and ksh93 shells will rely on the logical working directory described in the post linked in ...
user avatar
109 votes
Accepted

Difference between "cd -" and "cd ~-"

There are two things at play here. First, the - alone is expanded to your previous directory. This is explained in the cd section of man bash (emphasis mine): An argument of - is converted to $...
user avatar
  • 217k
80 votes

Do we have more history for cd?

You didn't specify which shell you are using, so let this be excuse to advertise zsh. Yes, we do have more history for cd, namely cd -2, cd -4 etc. Very convenient is cd -TAB, especially with ...
user avatar
  • 43.4k
57 votes
Accepted

Why do I need to use cd "$@" instead of cd "$1" when writing a wrapper for cd?

Because, according to bash(1), cd takes arguments cd [-L|[-P [-e]] [-@]] [dir] Change the current directory to dir. if dir is not supplied, ... so therefore the directory ...
user avatar
  • 30.2k
56 votes
Accepted

How to tell if I'm actually in a symlink location from command line?

Depending on how your pwd command is configured, it may default to showing the logical working directory (output by pwd -L) which would show the symlink location, or the physical working directory (...
user avatar
  • 3,401
55 votes

How do I use pushd and popd commands?

One simple use case for using dirs stack what you cannot do by just cd is: pushd . adds current directory XX to dirs stack. Afterwards, you can move around using cd, and to return to XX you just do ...
user avatar
55 votes

Does cd . have use?

Another use case of cd . would be when the directory you currently are in has been deleted and then made again. Consider trying the following - Create a directory temp cd temp and then do an ls Open ...
user avatar
54 votes
Accepted

cd .. on root folder

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 ...
user avatar
  • 774
51 votes
Accepted

Is it possible to define a command in bash?

just type: alias gb='cd /media/Dan/evolution' To make this setting permanent (so that it sticks after you restart or open another console) add this line to the file ~/.bashrc (assuming you use the ...
user avatar
  • 698
51 votes

Why current directory doesn't change in makefile?

You're using a makefile. Makefiles aren't scripts, each line is executed in a new shell. Meaning when you change the environment in line (such as cd), that change is not propagated to the next line. ...
user avatar
  • 67.6k
48 votes
Accepted

What is the difference between the commands builtin cd and cd?

The cd command is a built-in, so normally builtin cd will do the same thing as cd. But there is a difference if cd is redefined as a function or alias, in which case cd will call the function/alias ...
user avatar
  • 19.6k
46 votes

Is `-` used only with cd?

- is defined in POSIX Utility Syntax Guidelines as standard input: Guideline 13: For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should ...
user avatar
  • 144k
42 votes

Is it possible to define a command in bash?

Alternative to aliasing gb() { cd /media/Dan/evolution; } This defines shell function gb, which takes no arguments, and performs cd /media/Dan/evolution. As with other suggeststions, this can be ...
user avatar
39 votes
Accepted

pushd, popd vs cd, cd- in bash and zsh

It depends. In zsh you can configure cd to push the old directory on the directory stack automatically, but it is not the default setting. As far as I can tell zsh with default settings behaves very ...
user avatar
  • 4,016
38 votes

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

If your cronjob is a bash script, the following will CD to the location of your script (assuming that you're using absolute path in your cron definition): cd "$(dirname "$0")";
user avatar
  • 481
37 votes
Accepted

Everything is a file?

The "Everything is a file" phrase defines the architecture of the operating system. It means that everything in the system from processes, files, directories, sockets, pipes, ... is represented by a ...
user avatar
  • 45.6k
37 votes
Accepted

What exactly is dot dot (..)? Why is its behavior different with symlinks?

.. is a hard link to the parent directory which is created as part of the directory entry. If you issue ls -ail in each of these directories, you should see that the following entries all have the ...
user avatar
  • 7,868
36 votes

Script to change current directory (cd, pwd)

Use exec bash at the end A bash script operates on its current environment or on that of its children, but never on its parent environment. However, this question often gets asked because one wants ...
user avatar
36 votes

Is `-` used only with cd?

cd - is actually shorthand for cd "$OLDPWD" && pwd, where $OLDPWD is set each time you change directories to the directory you were just in. Handling of - depends on the application. Some ...
user avatar
  • 2,182
36 votes
Accepted

Do we have more history for cd?

The command you are looking for is pushd and popd. You could view a practical working example of pushd and popd from here. mkdir /tmp/dir1 mkdir /tmp/dir2 mkdir /tmp/dir3 mkdir /tmp/dir4 cd /tmp/...
user avatar
  • 36.8k
36 votes

Alias to CD in a directory and call a command

To execute a command with a specific working directory, one usually does ( cd directory && utility ) The parentheses around the cd ... means that the command(s) therein runs in a subshell. ...
user avatar
  • 271k
36 votes

Does cd . have use?

You can clear $OLDPWD with a quick cd ., if there should be a case where you don't want it to point anywhere "interesting". It'll also affect cd -.
user avatar
  • 1,479
34 votes
Accepted

How does a shell know home(s)?

In the case of csh and tcsh, it records the value of the $HOME variable at the time the shell was started (in its $home variable as noted by @JdeBP). If you unset it before starting csh, you'll see ...
user avatar
33 votes

is there a way to cd into a directory based on the last characters?

With Bash, yes, you can use wildcards: cd /path/to/*9/ (replace 9 with however many digits you need; you can drop /path/to/ if you’re in the directory containing all the 164... directories). You need ...
user avatar
31 votes
Accepted

Can the command completion for `cd` be modified to only show directories and ignore files?

Just add complete -d cd in your ~/.bashrc (or other bash configuration file).
user avatar
  • 43.4k
29 votes

cd .. on root folder

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 .: $ ...
user avatar

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