155
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 ...
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 ...
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 -.
32
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
~/...
30
votes
Accepted
Why does cd '' succeed in bash?
The most recent POSIX specification for the cd utility (Issue 8, from 2024) has this in the OPERANDS section:
[...] If directory is an empty string, cd shall write a diagnostic message to standard ...
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
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() {
...
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
-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
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
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 ...
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
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
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
Accepted
Changing directory by changing one early word in a pathname
In some shells, e.g. ksh and zsh, doing cd word1 word2 would change to a directory given by changing the first occurrence of word1 in the pathname of the current directory to word2.
For example, in ...
16
votes
Execute a specific command in a given directory without cd'ing to it?
The env program from GNU coreutils-8.28 (released 2017-09-01) and newer has --chdir:
env: add --chdir option
This is useful when chaining with other commands that run commands in
a different context,...
16
votes
Does cd . have use?
Programmatically it's useful as a no-op. Consider a path provided from external input.
read -p "Path to file: " p
dirn=$(dirname "$p")
file=$(basename "$p")
echo "dirn=$dirn, file=$file"
cd "$dirn"
...
15
votes
Accepted
$ find -exec cd => gives error: => find: ‘cd’: No such file or directory
The -exec option to find executes an external utility, possibly with some command line option and other arguments.
Your Unix does not provide cd as an external utility, only as a shell built-in, so ...
15
votes
What is the difference between the commands builtin cd and cd?
In most instances, there is no difference (but see below). The cd command is a built-in command in all shells. It needs to be built-in1 as an external command can not change the environment of the ...
14
votes
Execute bash scripts on entering a directory
direnv might be what you are looking for.
direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current ...
14
votes
Why do I need to use cd "$@" instead of cd "$1" when writing a wrapper for cd?
There's also the case when there are no arguments:
$ cd /tmp; cd; pwd
/home/muru
$ cd_func() { builtin cd "$1"; }
$ cd_func /tmp; cd_func; pwd
/tmp
cd without any arguments changes to the home ...
14
votes
Does cd . have use?
This is common if you had to work with a bad USB cable. After a device get disconnected and connected again, and automounted to the same directory, you have to use cd . to get it work again.
13
votes
Accepted
libtool error cd: ../..: Not a directory
The problem was that I'm on an NFS mount on an HPC system (on which I have administrative privileges). But the error occurred when running sudo make install.
The NFS server appears to be using the ...
12
votes
Accepted
Has "cd .. .. .." ever worked for going up 3 directories?
This is an error in the book which the publisher addresses in the "Updates" section on the book's "homepage" (https://nostarch.com/linuxbasicsforhackers#updates):
Updates
Page 7
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cd-command × 425bash × 162
shell × 104
directory × 62
shell-script × 48
command-line × 38
zsh × 37
symlink × 34
linux × 26
alias × 25
ls × 16
autocomplete × 14
permissions × 12
pwd × 12
sudo × 11
find × 9
command × 9
pushd × 9
shell-builtin × 8
scripting × 7
filesystems × 7
environment-variables × 7
path × 7
home × 7
mkdir × 7