Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
21
votes
3
answers
5307
views
When I cd through a symlink, why does pwd show the symlink instead of the real path?
I created a soft link (`ln -s 1 2`) to a directory which is inside the `test` directory and opened the soft link (`cd 2`) and displayed the current path using `pwd`. The displayed path was `~/test/2` and not `~/test/1`. It's different in an OS like Windows, the shortcut brings us to the real directo...
I created a soft link (
ln -s 1 2
) to a directory which is inside the test
directory and opened the soft link (cd 2
) and displayed the current path using pwd
. The displayed path was ~/test/2
and not ~/test/1
.
It's different in an OS like Windows, the shortcut brings us to the real directory. I'm little bit confused how this soft link works in Linux. Is it not a shortcut like in Windows
? Why is the path not ~/test/1
?
$ mkdir test
$ cd test
$ mkdir 1
$ ln -s 1 2
$ cd 2
$ pwd
/home/dazz/test/2
DScript
(1083 rep)
Apr 16, 2015, 05:03 PM
• Last activity: Jul 2, 2025, 03:14 PM
0
votes
2
answers
90
views
Error trying to set directory in shell script
I am trying to set the current directory in a shell script like this: cd /home/sshuser/xyz and get this response when I run it: $'/home/sshuser/xyz\r': No such file or directory* The cd command works when run from the command line. I intend to run the script in a cronjob.
I am trying to set the current directory in a shell script like this:
cd /home/sshuser/xyz
and get this response when I run it:
$'/home/sshuser/xyz\r': No such file or directory*
The cd command works when run from the command line. I intend to run the script in a cronjob.
geezer
(1 rep)
Jun 10, 2025, 07:21 PM
• Last activity: Jun 12, 2025, 04:00 PM
36
votes
5
answers
8242
views
How to tell if I'm actually in a symlink location from command line?
Suppose I have a folder: cd /home/cpm135/public_html and make a symbolic link ln -s /var/lib/class . Later, I'm in that directory: cd /home/cpm135/public_html/class The `pwd` is going to tell me I'm in `/home/cpm135/public_html/class` Is there any way to know that I'm "really" in `/var/lib/class` ?...
Suppose I have a folder:
cd /home/cpm135/public_html
and make a symbolic link
ln -s /var/lib/class .
Later, I'm in that directory:
cd /home/cpm135/public_html/class
The
pwd
is going to tell me I'm in /home/cpm135/public_html/class
Is there any way to know that I'm "really" in /var/lib/class
? Thanks
Oliver Williams
(1425 rep)
Dec 18, 2016, 12:16 PM
• Last activity: Jun 11, 2025, 09:27 PM
152
votes
11
answers
425052
views
Script to change current directory (cd, pwd)
I want to run a script to simply change the current working directory: #!/bin/bash cd web/www/project But, after I run it, the current pwd remains unchanged! How can I do that?
I want to run a script to simply change the current working directory:
#!/bin/bash
cd web/www/project
But, after I run it, the current pwd remains unchanged! How can I do that?
Sony Santos
(1623 rep)
Dec 19, 2011, 07:55 AM
• Last activity: May 22, 2025, 02:54 AM
14
votes
1
answers
2677
views
Why does cd '' succeed in bash?
Maybe I'm missing it, but I don't find it documented that `cd ''` should succeed. Since there is no directory with the name `''`, it seems obvious that it should fail. For example, mydir= cd -- "$mydir" || exit 1 # succeeds!! echo wrong Seems like a bug in about a million scripts, just waiting to ha...
Maybe I'm missing it, but I don't find it documented that
cd ''
should succeed. Since there is no directory with the name ''
, it seems obvious that it should fail. For example,
mydir=
cd -- "$mydir" || exit 1 # succeeds!!
echo wrong
Seems like a bug in about a million scripts, just waiting to happen. Is this some sort of backwards-compatible-bug kind of thing? Do I really have to do a separate check for an empty variable name every time I cd
? FWIW, shellcheck
doesn't catch this, either.
jrw32982
(1089 rep)
Apr 24, 2025, 01:08 AM
• Last activity: Apr 27, 2025, 06:09 AM
-2
votes
2
answers
244
views
Dangerous behavior of the `cd` built-in
I was plodding along, working on a shell script, and I've just learned something that I found *surprising*. I'm presenting it here as a Question because I'd like to learn if there's some way to avoid this. Here's the *scenario*: `cd` to a folder that is defined as a variable; except the variable nam...
I was plodding along, working on a shell script, and I've just learned something that I found *surprising*. I'm presenting it here as a Question because I'd like to learn if there's some way to avoid this.
Here's the *scenario*:
cd
to a folder that is defined as a variable; except the variable name is spelled incorrectly in the cd
command:
$ TGT_FLDR=/home/seamus/logs/
$ cd $TGT_FLDF
$ echo $?
0
$ pwd
/home/seamus
$
I learned of this while testing a script:
TGT_FLDR=/home/seamus/logs/
...
cd $TGT_FLDF
if [ -n "$(ls -A)" ]; then
rm *
fi
### OUCH!!!
If I had quoted "$TGT_FLDF" (as I probably should have) cd
still fails. If the pwd
happens to be somewhere other than ~
; cd
"fails" in a **different way**, depending upon whether or not the variable is quoted. Note that in neither case does cd
throw an error
; this makes the error [*pernicious*](https://dictionary.cambridge.org/dictionary/english/pernicious) in that it cannot be trapped in a test
([]
).:
$ pwd
/home/seamus/logs/testing
$ TGT_FLDR=/home/seamus/logs/
$ cd "$TGT_FLDF" # var quoted
$ echo $?
0
$ pwd
/home/seamus/logs/testing
$ cd $TGT_FLDF # var unquoted
$ echo $?
0
$ pwd
/home/seamus
$
This *struck me* as rather dangerous behavior (primarily b/c $?
is 0
). Since cd
is a bash
*built-in* (at least on my system), I checked cd --help
. One item stood out:
> If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
This statement makes little sense to me, but it *suggests* that there is a *"shell option"* cdable_vars
defined somewhere, that it's defined, and may be defined as $HOME
.
I think I'd like to change the value of cdable_vars
, but where is this shell option defined?
Seamus
(3772 rep)
Jan 13, 2025, 05:20 AM
• Last activity: Jan 31, 2025, 12:05 AM
15
votes
4
answers
18913
views
How to make cd arguments case INsensitive?
Sometimes while accessing the various directories it happens most of the times that I remember the names or at least part of the names of a directory under our Linux system. But some of the directories are named starting with first character caps or one of the characters in the middle of the name Up...
Sometimes while accessing the various directories it happens most of the times that I remember the names or at least part of the names of a directory under our Linux system. But some of the directories are named starting with first character caps or one of the characters in the middle of the name Upper case.
Can anyone suggest how can I make the arguments following
cd
command case INSENSITIVE, such that if I perform cd BackupDirectory
or cd backupdirectory
it could enter the directory name BackupDirectory.
Of course I don't want to screw the things for other users so if the above is possible, is that possible that the change could be applied just to the session I am using and do not effect other users?
Ok, I tried set completion-ignore-case
on but this just doesn't work. It just helps in that if I type cd b
and Tab or Esc Esc it fills the directory name ignoring the case. But, what I need is if I do a cd backupdirectory
, it just ignores the case and enters BackupDirectory
on its own.
Ankit Vashistha
(3973 rep)
Jan 3, 2013, 09:55 AM
• Last activity: Jan 27, 2025, 12:26 PM
2
votes
1
answers
1918
views
Change to a directory containing a space
I'm using a MacBook with OS X installed. I'm on Terminal trying to go to a directory. So I'm in Home and I use the command `ls` and there is a `VirtualBox VMs` directory. But when I try to do `cd VirtualBox VMs` it says "No such file or directory" Why? Is it because of the space bar in the word? How...
I'm using a MacBook with OS X installed.
I'm on Terminal trying to go to a directory. So I'm in Home and I use the command
ls
and there is a VirtualBox VMs
directory.
But when I try to do cd VirtualBox VMs
it says "No such file or directory"
Why?
Is it because of the space bar in the word? How can I fix this?
GrangerObliviate
(21 rep)
Apr 18, 2016, 12:20 AM
• Last activity: Nov 14, 2024, 10:14 PM
13
votes
5
answers
4208
views
How to make `cd dir/filename` take me to dir/?
I would find it very convenient to be able to use `cd` with a **file** argument. `cd myDirectory/anyname.anyExtension` would be equivalent to `cd myDirectory/` What would be the best alias or function to achieve this behavior ? **EDIT**: Sorry I didn't mention it in the 1st place: I use `zsh`
I would find it very convenient to be able to use
cd
with a **file** argument.
cd myDirectory/anyname.anyExtension
would be equivalent to
cd myDirectory/
What would be the best alias or function to achieve this behavior ?
**EDIT**:
Sorry I didn't mention it in the 1st place: I use zsh
Sébastien
(977 rep)
May 17, 2013, 04:39 PM
• Last activity: Aug 30, 2024, 08:02 AM
16
votes
1
answers
2221
views
Different behaviour of cd with multiple arguments in bash releases
Posting it here something that is puzzling me; upgrading an application server from Jessie to Stretch broke a `bash` script. Upon investigation, we narrowed it to a change of behaviour of the `cd` command. I am not discussing here if what the script is doing is a good idea, or if it could be improve...
Posting it here something that is puzzling me; upgrading an application server from Jessie to Stretch broke a
bash
script.
Upon investigation, we narrowed it to a change of behaviour of the cd
command. I am not discussing here if what the script is doing is a good idea, or if it could be improved, I am just focusing in a behaviour change /awareness of changes mindset.
Jessie
$echo *-*
xxxx-1.0b xxxx-run
$cd *-*
cd works and cds to the first directory ; actually *-run is a symbolic link to the same directory.
Stretch
$echo *-*
xxxx-1.0b xxxx-run
$cd *-*
bash: cd: too many arguments
bash
is 4.3.30(1) in Jessie, and 4.4.12(1) in Stretch.
Why the change in behaviour between Jessie and Stretch?
Rui F Ribeiro
(57882 rep)
Jun 27, 2017, 08:00 AM
• Last activity: Aug 23, 2024, 09:54 AM
6
votes
2
answers
1120
views
Terminal autocomplete (tab) not completing when changing directory up one level (cd ../)
I'm a relative Linux noob: I've worked with the bash shell for quite a while via an Ubuntu install in WSL2 on a Windows 11 machine and recently took the plunge to switch over to Linux 100%. My question is about changing directories (specifically navigating up a directory) using the terminal and usin...
I'm a relative Linux noob: I've worked with the bash shell for quite a while via an Ubuntu install in WSL2 on a Windows 11 machine and recently took the plunge to switch over to Linux 100%. My question is about changing directories (specifically navigating up a directory) using the terminal and using the tab autocomplete feature.
Previously when I've been navigating on Ubuntu I was able to type
For reference sake, here's the output of neofetch on my system:
OS: Kali GNU/Linux Rolling x86_64
Host: NH50_70RA
Kernel: 6.8.11-amd64
Uptime: 1 day, 1 hour, 16 mins
Packages: 3900 (dpkg)
Shell: zsh 5.9
Resolution: 1080x1920, 1920x1080
DE: Plasma 5.27.10
WM: KWin
Theme: [Plasma], Breeze [GTK2/3]
Icons: [Plasma], Vivid-Dark-Icons [GTK2/3]
Terminal: gnome-terminal
CPU: Intel i7-9750H (12) @ 4.500GHz
GPU: Intel CoffeeLake-H GT2 [UHD Graphics 630]
GPU: NVIDIA GeForce GTX 1650 Mobile / Max-Q
Memory: 6228MiB / 31813MiB
I feel like the different behavior is due to using bash on Ubuntu and zsh on my current setup. I found this documentation on different configuration options within the .zshrc and looked through it but I wasn't able to find anything that stood out as far as what I could do to get my desired behavior. I think this is due to me not fully understanding the documentation rather than the solution not being in there.
Sorry in advance if I didn't post the right info needed or if I didn't post enough to effectively troubleshoot this. This is my first time posting a question about Linux (well, second actually. I posted in this question over in the Superuser StackExchange but have not received any insight over there so I thought I'd post over here).
If anyone would help me figure out what the best solution is to get this desired behavior I would greatly appreciate it. Optimally I'd like to be able to type
cd ..
on the command line and then press tab which would then complete the command to be cd ../
. I could have sworn that when I spun up my new install (Kali Linux) that I was able to do the same thing. But now, when I try to do this (type cd ..
and then press tab to get the auto complete to change it to cd ../
) this is what I see instead:

cd ..
at the command prompt and be able to press tab
to have it autocomplete to cd ../
, then if I immediately type ..
and then press tab
again it would autocomplete to ../
resulting with cd ../../
at the command prompt and so on, which in this example if I then pressed enter
would take me up two directories from the current directory with the single command of cd ../../
having been formed and executed.
I've seen answers where others have created something like a function to be stored in the .zshrc
(or .bashrc
for bash) file that could be called to navigate up a certain amount of directories at once by passing a number to the function when called, IE: the function is named something like cdu
for Change Directory Up and entering cdu 2
at the command prompt and pressing enter
would then automatically navigate up two directories from the current directory. This is a clever way to accomplish the same thing, but I'd really like to be able to manually "chain" on however many instances of ../
by pressing ..
and then tab
immediately after to dynamically see the command grow until I press enter
to execute the command and navigate up that many directories. I could probably cobble together a function of my own that would let me do what I want to do, but I feel there has to be a simpler way like a setting that I need to configure in the right way that will make this happen.
Thanks in advance!
Gharbad The Weak
(193 rep)
Aug 16, 2024, 05:33 AM
• Last activity: Aug 20, 2024, 05:49 AM
2
votes
2
answers
304
views
cd 'old' 'new' with multiple "whole directory-name" substitutions
- To move from `~/aaa/foo/bbb` to `~/aaa/bar/bbb`, one can use `cd foo bar` - and from `~/foobar/foo/www` to `~/foobar/bar/www`: `cd "/foo/" "/bar/"` But how is it possbile, using the same technique, - to move from `~/aaa/foo/foo/bbb` to `~/aaa/bar/bar/bbb` - or from `~/foobar/foo/foo/www` to `~/foo...
- To move from
~/aaa/foo/bbb
to ~/aaa/bar/bbb
, one can use cd foo bar
- and from ~/foobar/foo/www
to ~/foobar/bar/www
: cd "/foo/" "/bar/"
But how is it possbile, using the same technique,
- to move from ~/aaa/foo/foo/bbb
to ~/aaa/bar/bar/bbb
- or from ~/foobar/foo/foo/www
to ~/foobar/bar/bar/www
?
jsx97
(1347 rep)
Aug 8, 2024, 07:47 PM
• Last activity: Aug 9, 2024, 08:42 AM
0
votes
1
answers
48
views
To 'cd' by altering only the middle part of the path
In Zsh, to `cd` from `~/foo/bar/aaa/bbb/ccc` to `~/foo/bar/zzz/bbb/ccc`, I can type `cd aaa zzz`, and that's it. I have carefully read the Fish `cd` man page, but I still cannot figure out: is the same thing possible there?
In Zsh, to
cd
from ~/foo/bar/aaa/bbb/ccc
to ~/foo/bar/zzz/bbb/ccc
, I can type cd aaa zzz
, and that's it. I have carefully read the Fish cd
man page, but I still cannot figure out: is the same thing possible there?
jsx97
(1347 rep)
Jul 28, 2024, 10:33 AM
• Last activity: Jul 28, 2024, 10:51 AM
5
votes
4
answers
79036
views
How to change the working directory for a shell script
I have a Python script that looks files up in a relative directory. For example: the Python script is in `/home/username/projectname/`. I have a file that is being called within the Python script that is in `/home/username/projectname/subfolder`. If I run the script from the shell as `python scriptn...
I have a Python script that looks files up in a relative directory. For example: the Python script is in
/home/username/projectname/
. I have a file that is being called within the Python script that is in /home/username/projectname/subfolder
.
If I run the script from the shell as python scriptname.py
it runs perfectly fine.
However, I'm trying to run the script as a startup service. I'm setting it up in webmin, and I believe its creating a shell script to call it. Through the startup script, I'm doing something like this to call the script:
execute python home/username/projectname/scriptname.py
The script is starting up fine, but it can't access the files in the relative directory.
I am guessing that there is a better way to call the Python program from within the startup script so that its aware of the relative path.
Randy
(153 rep)
Jul 8, 2012, 11:02 PM
• Last activity: Jul 27, 2024, 12:12 PM
1
votes
1
answers
118
views
Bash `cd -L` vs. `cd -P` vs. Bash Reference manual description
**Premise** I've read what's listed in Bibliography regarding `cd`, `pwd`, `set -P`. > By default, or when the `-L` option is supplied, symbolic links in *directory* are resolved after `cd` processes an instance of ‘`..`’ in directory. (ref. Bash reference manual - sec. "[4 Shell Builtin Commands" -...
**Premise**
I've read what's listed in Bibliography regarding
cd
, pwd
, set -P
.
> By default, or when the -L
option is supplied, symbolic links in *directory* are resolved after cd
processes an instance of ‘..
’ in directory. (ref. Bash reference manual - sec. "[4 Shell Builtin Commands" - cd
paragraph])
> If ‘..
’ appears in *directory*, it is processed by removing the immediately preceding pathname component, back to a slash or the beginning of *directory*. (ibid)
My version of Bash is
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
**Problem**
To test things out I have the following file scheme in my current working directory that is the root of my experiment (dir111
is a symbolic link to dir010
).
dir010/
dir020/
dir110/dir111 -> ../dir010
my_dir="$PWD"
cd dir110/dir111 # 1
cd .. # 2
cd dir111 # 3
cd ../dir020 # 4
cd "$my_dir" # 5
cd dir110/dir111/../dir020 # 6
cd - # 7
cd dir110/dir111/../../dir020 # 8
1) sets the current working directory to "$my_dir"/dir110/dir111
, as expected[^1].
2) sets the current working directory to "$my_dir"/dir110
, as expected[^1].
3) back to 1.
4) sets the current working directory to "$my_dir"/dir020
, **unexpected**. I would expect cd
to fail at "$my_dir"/dir110/dir020
(see below for explanation).
5) back to the root of my experiment.
6) **same as 4**.
7) back to the root of my experiment.
8) sets the current working directory to "$my_dir"/dir020
, as expected[^1], but **in contrast with 6.**
Line 4 should fail for the reason in the following paragraph.
The reference manual and the man page (see quote in the Premise) both tell that ..
make cd
only remove the preceding pathname component, instead of looking at the ..
of the inode entry. So the parent directory of "$my_dir/dir110/dir111
as seen by cd
(in default behaviour) is "$my_dir/dir110
, which does not contain dir020
.
I thought that this behaviour may be an exception caused by ..
being the prefix of the «dirname»
provided to cd «dirname»
. However, using ..
as an infix (line 6) contrast with this.
cd
is behaving as if cd -P
has been executed (or set -P
(or similar) command issued before), that is by not following symbolic links. With cd -P
, at 1., the current working directory would be set to $my_dir/dir010
.
set -o
tells me
[omissis]
physical off
pipefail off
posix off
[omissis]
Is my (and the ones like me in other posted questions) interpretation of the reference manual correct? What am I missing? How can the behaviour of cd
be described?
**Bibliography**
+ [Bash man page] sec. "SHELL BUILTIN COMMANDS"
+ [Bash reference manual] sec. "4 Shell Builtin Commands"
+ [Changing parent directory (../) with symlinks]
+ [Path resolution depending on pwd (symlinked dirs)]
+ [How do I cd up and down again with symlinks in bash?]
the_eraser
(185 rep)
Jul 24, 2024, 04:27 PM
• Last activity: Jul 24, 2024, 05:21 PM
2
votes
5
answers
3847
views
Faster way to cd into a directory after listing the directory in a previous command?
I was looking for a file in a directory and so I used the find command which then showed the directory path of where the file is. ex. $find . -name file.txt /folder/path/file-contents/file.txt Now I was wondering is there a way to cd into this directory a quicker way than copying the path directory...
I was looking for a file in a directory and so I used the find command which then showed the directory path of where the file is.
ex.
$find . -name file.txt
/folder/path/file-contents/file.txt
Now I was wondering is there a way to cd into this directory a quicker way than copying the path directory and then using the cd command and pasting the directory?
Also if there are two paths that get listed after using the find command, is there a way to just to cd to the second path given without copying and pasting that path as well?
Just want to know to help me speed up working in terminal a little bit.
AMVPlusPlus
(195 rep)
Dec 4, 2019, 02:54 PM
• Last activity: Jul 18, 2024, 05:37 AM
3
votes
2
answers
179
views
cd's '-e' option
According to the Bash manual page the `-e` option of `cd` leads to an unsuccessful return status **after a successful directory change**. >If the -e option is supplied with -P and the current working directory cannot be successfully determined after a successful directory change, cd will return an u...
According to the Bash manual page the
-e
option of cd
leads to an unsuccessful return status **after a successful directory change**.
>If the -e option is supplied with -P and the current working directory cannot be successfully determined after a successful directory change, cd will return an unsuccessful status.
$ cd
$ mkdir -p test/a/b
$ cd test/a/b
$ rmdir ../b ../../a
$ cd -Pe ..; echo $?
cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
1
Why is the above returning 1
? How could cd -Pe ..
possibly have made a successful directory change if the current as well as the parent directories do not exist anymore?
Also, if I want cd
to return an unsuccessful return status each and every time it cannot change to the desired directory, what are the situations where I have to add -Pe
? From what I understand it is only necessary if .
or ..
are used in the argument for cd
.
user279450
Mar 7, 2018, 04:40 PM
• Last activity: May 30, 2024, 07:16 PM
20
votes
3
answers
102991
views
~ is $HOME, but sometimes?
cd ~ does the same thing as cd $HOME which is also the same as cd /home/tandu However, cd ~not-tandu changes to `/home/not-tandu` Is this purely a syntactic choice? How is this handled by the kernel (or the `cd` execuable?) Is there a special case for `~` to add the slash if everything else is omitt...
cd ~
does the same thing as
cd $HOME
which is also the same as
cd /home/tandu
However,
cd ~not-tandu
changes to
/home/not-tandu
Is this purely a syntactic choice? How is this handled by the kernel (or the cd
execuable?) Is there a special case for ~
to add the slash if everything else is omitted? That is to say, ~/
and ~
change to the same directory, but ~a
is one directory up. The same cannot be said for any other directory you change to.
Explosion Pills
(333 rep)
Mar 17, 2012, 05:17 AM
• Last activity: May 2, 2024, 01:46 AM
0
votes
0
answers
141
views
How to show path after cd command with CDPATH set?
I came across [this question][1] and showing the current path after `cd` is exactly what I'm looking for. However, I can't manage to get this behavior. What I've tried is this: ```shell export CDPATH='.:/home/alexzeitler' ``` and ```shell export CDPATH=$HOME ``` But after `cd`, the current path is n...
I came across this question and showing the current path after
cd
is exactly what I'm looking for.
However, I can't manage to get this behavior.
What I've tried is this:
export CDPATH='.:/home/alexzeitler'
and
export CDPATH=$HOME
But after cd
, the current path is not shown.
I want to use it using bash
and zsh
and a solution without alias or function is preferred (those are known).
Alexander Zeitler
(175 rep)
Apr 25, 2024, 01:44 PM
• Last activity: Apr 25, 2024, 01:57 PM
134
votes
36
answers
74924
views
Quick directory navigation in the bash shell
I would like to frequently switch between directories that are in totally unrelated paths, for example `/Project/Warnest/docs/` and `~/Dropbox/Projects/ds/test/`. But I don't want to type `cd /[full-path]/` all the time. Are there any shortcut commands to switch to previously worked directories? One...
I would like to frequently switch between directories that are in totally unrelated paths, for example
/Project/Warnest/docs/
and ~/Dropbox/Projects/ds/test/
.
But I don't want to type cd /[full-path]/
all the time. Are there any shortcut commands to switch to previously worked directories?
One solution I could think of is to add environment variables to my bash
.profile
for the frequently used directories and cd
to them using those variables.
But is there any other solution to this?
saiy2k
(1603 rep)
Feb 8, 2012, 07:33 AM
• Last activity: Apr 11, 2024, 05:50 PM
Showing page 1 of 20 total questions