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 ...
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 ...
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 $...
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 (...
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 ...
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 ...
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 ...
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 ...
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. ...
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")";
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 ...
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 -.
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 ...
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 ...
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
~/...
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:...
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 ...
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 ../.. (...
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 ...
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 ...
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() {
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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
...
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:...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cd-command × 419bash × 161
shell × 105
directory × 61
shell-script × 45
command-line × 38
zsh × 35
symlink × 33
linux × 26
alias × 25
ls × 16
autocomplete × 13
permissions × 12
sudo × 11
pwd × 11
find × 9
command × 9
pushd × 9
scripting × 7
filesystems × 7
environment-variables × 7
path × 7
home × 7
shell-builtin × 7
mkdir × 7