Skip to main content
156 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 ...
Olorin's user avatar
  • 4,666
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 ...
Sergiy Kolodyazhnyy's user avatar
117 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 $...
terdon's user avatar
  • 245k
59 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 (...
Zanna's user avatar
  • 3,611
58 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 ...
thrig's user avatar
  • 35.1k
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 ...
Sahil Agarwal's user avatar
50 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 ...
filbranden's user avatar
  • 21.9k
45 votes

How to get the cd shell-builtin to stop guessing?

The behavior of the cd command is affected by shopt. See man bash, search: shopt. See also the The Shopt Builtin in the Bash Reference Manual. In particular, the behavior illustrated in the question ...
Ana Nimbus's user avatar
42 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. ...
Kusalananda's user avatar
  • 339k
41 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")";
Hugo's user avatar
  • 511
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 ...
user4556274's user avatar
  • 9,045
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 -.
mike3996's user avatar
  • 1,569
33 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 ...
Stéphane Chazelas's 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 ...
Stephen Kitt's user avatar
30 votes
Accepted

How to `cd` with word in the middle of a folder?

I can't speak for others (e.g., zsh) but if you are using bash, wildcards do work to an extent.  Example: ~ $ ls Documents Desktop Downloads If you use an asterisk (*), you get: ~ $ cd *ments ~/...
Stewart's user avatar
  • 14.2k
26 votes
Accepted

Why did my folder names end up like this, and how can I fix this using a script?

You can use the perl rename utility (aka prename or file-rename) to rename the directories. NOTE: This is not to be confused with rename from util-linux, or any other version. rename -n 's/([[:cntrl:...
cas's user avatar
  • 79.2k
25 votes
Accepted

What is an "alternative directory name" in CDPATH for the cd command?

The variable is not set by default (at least in the systems I am familiar with) but can be set to use a different directory to search for the target dir you gave cd. This is probably easier to ...
terdon's user avatar
  • 245k
24 votes
Accepted

Can you explain these three things in this bash code for me?

d=$d/.. adds /.. to the current contents of the d variable. d starts off empty, then the first iteration makes it /.., the second /../.. etc. sed 's/^\///' drops the first /, so /../.. becomes ../.. (...
Stephen Kitt's user avatar
23 votes
Accepted

Different behaviour of cd with multiple arguments in bash releases

See the Relase Notes of bash 4.4, especially qq. There is a new option settable in config-top.h that makes multiple directory arguments to cd a fatal error. BTW, I didn't know. I just ...
choroba's user avatar
  • 47.7k
23 votes
Accepted

-bash: /bin/cd: No such file or directory - automatically execute ls after cd

Your system (like many Unix systems) does not have an external cd command (at least not at that path). Even if it had one, the ls would give you the directory listing of the original directory. An ...
Kusalananda's user avatar
  • 339k
21 votes

Script to change current directory (cd, pwd)

Depends on what you're going to do, another solution can be creating a function instead of a script. Example: Create a function in a file, let's say /home/aidin/my-cd-script: function my-cd() { ...
Aidin's user avatar
  • 404
21 votes
Accepted

Can I create a one way symlink?

All symbolic links are one-way. As far as the kernel is concerned, after going into /D/S1 and running chdir("ls2"), you're in /D/S2, so if you run chdir(".."), you end up in /D. If you do this in a ...
Gilles 'SO- stop being evil''s user avatar
20 votes
Accepted

"cd" into /sys/kernel/debug/tracing causes permission change

/sys /sys is sysfs, an entirely virtual view into kernel structures in memory that reflects the current system kernel and hardware configuration, and does not consume any real disk space. New files ...
telcoM's user avatar
  • 101k
19 votes

How do I use pushd and popd commands?

The pushd/popd is such a simple concept which took me awhile to comprehend since people tend to teach it by defining these commands as commands that 'manipulate the directory stack' which in my ...
Mercury's user avatar
  • 291
19 votes

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

Using "$@" will pass all arguments to cd where as $1 will only pass the first argument. In your examples $ . cdtest.sh "r st" always works as you only pass in one argument, but if you were to pass ...
Michael Daffin's user avatar
18 votes
Accepted

How to run one command with a directory as argument, then cd to the same? I get "no such file or directory"

Instead of defining a function, you can use the variable $_, which is expanded to the last argument of the previous command by bash. So use: cd "$_" after mv command. You can use history expansion ...
heemayl's user avatar
  • 56.7k
18 votes

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

~- is subject to tilde expansion (see man bash), so what cd sees is the previous directory name directly. - is not expanded by the shell, cd sees it directly, and behaves as documented: An argument ...
choroba's user avatar
  • 47.7k
18 votes

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

If they are actually distinct except for the last few digits, you can use a wildcard in the cd command, e.g., cd 164*8 (and if they are not actually distinct, the shell will remind you of this by ...
Thomas Dickey's user avatar
17 votes

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

Note that pwd is actually a shell built-in. Depending on your shell and its configuration, results may change. For a more portable solution, you should use /bin/pwd. Snippet from manual page: NAME ...
Gowtham's user avatar
  • 2,089
16 votes

How do I use pushd and popd commands?

For bash, basically: instead of using cd one can use pushd to change directories. With practical usage: the history of visited directories is saved (correctly: stacked) and one can switch between them:...
MacMartin's user avatar
  • 2,964

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