Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

294 votes
8 answers
255061 views
Find out current working directory of a running process?
What command(s) can one use to find out the current working directory (CWD) of a running process? These would be commands you could use externally from the process.
What command(s) can one use to find out the current working directory (CWD) of a running process? These would be commands you could use externally from the process.
slm (378965 rep)
Oct 10, 2013, 12:15 AM • Last activity: Mar 13, 2025, 04:30 PM
1 votes
3 answers
3652 views
chroot with working directory specified
Say I want to write wrapper for `chroot` command with the possibility to set working directory otherwise preserving `chroot` semantics. Thus semantics should be: chroot.sh [arg]... My naive attempt is: #!/bin/sh chroot_dir=$1 working_dir=$2 shift 2 chroot "$chroot_dir" sh -c "cd $working_dir; $*" Bu...
Say I want to write wrapper for chroot command with the possibility to set working directory otherwise preserving chroot semantics. Thus semantics should be: chroot.sh [arg]... My naive attempt is: #!/bin/sh chroot_dir=$1 working_dir=$2 shift 2 chroot "$chroot_dir" sh -c "cd $working_dir; $*" But this fails to correctly handle: chroot.sh /path/to/chroot /tmp touch 'filename with space' I cannot figure out how it should be implemented correctly. Is it possible using bash only? On my CentOS 6 system chroot command doesn't support setting working directory. Probably this is not true on other systems.
reddot (296 rep)
Nov 2, 2017, 04:39 PM • Last activity: May 21, 2023, 04:18 PM
11 votes
1 answers
19718 views
What is the difference between cwd and pwd?
What is the difference between cwd and pwd? I've tried googling it, and one of the answers mentioned that depending on some factor (which I sadly do not remember), the implementation (the code I'm assuming) is not the same? I don't suppose this is like the difference between `print('x')` vs `return...
What is the difference between cwd and pwd? I've tried googling it, and one of the answers mentioned that depending on some factor (which I sadly do not remember), the implementation (the code I'm assuming) is not the same? I don't suppose this is like the difference between print('x') vs return str(x) (to use a Python analogy)?
account (113 rep)
Jul 14, 2022, 09:35 PM • Last activity: Mar 30, 2023, 12:57 PM
2 votes
2 answers
2468 views
Set alias to run python venv setup based on current directory name
Can't seem to figure out what the issue is. This is on a Fedora 37 with default setup (bash, gnome, etc...). What I want to achieve is to create an alias that creates a python virtual environment, where the folder name for the virtual environment is the name of the current working directory (somethi...
Can't seem to figure out what the issue is. This is on a Fedora 37 with default setup (bash, gnome, etc...). What I want to achieve is to create an alias that creates a python virtual environment, where the folder name for the virtual environment is the name of the current working directory (something akin to python3 -m venv $cwd).
This is helpful especially when activating the venv (and later switching directories between project) you can easily identify which venv is enabled. But for some odd reason this is not working as I expected. To note that I don't have much experience in creating complex aliases but still. I have created the following vars in .bashrc: - export cwd="pwd | rev | cut -d '/' -f1 | rev" - If I run this in termnial it displays the directory name test as I need it, but when running $cwd in the terminal I get home//Projects/VueJS/test - export cwd2="${PWD##*/}" - This one just displays my username for some reason. I've searched here on stackoverflow and this was one of the suggested methods to get the current directory name Some indication on how to get started or where to look for info are appreciated. I've tried to search on multiple sources but I can't seem to figure it. I read somewhere that double and single quotes make a difference, but I couldn't understand why and if it would affect this behaviour. EDIT: To clarify, what I want my alias to run is i.e: - python3 -m venv && source /bin/activate && [ ! -f ./requirements.txt ] || pip install -r requirements.txt - This creates a virtual for python projects - python3 -m venv && source /bin/activate && pip install nodeenv && nodeenv -p - This creates virtual environments for projects that depend on nodejs, which in itself depend on python venv
calexandru (210 rep)
Feb 8, 2023, 11:09 PM • Last activity: Feb 11, 2023, 12:29 AM
1 votes
0 answers
210 views
How to get the final current working directory of a child process?
If I spawn off a child process, I can access its working directory (in a not-very-cross-platform way) any time while it's alive by examining `/proc/ /cwd`. But what are my options if I want to know its final location on exit? I've tried watching `/proc/ /` after death, but the directory vanishes as...
If I spawn off a child process, I can access its working directory (in a not-very-cross-platform way) any time while it's alive by examining /proc//cwd. But what are my options if I want to know its final location on exit? I've tried watching /proc// after death, but the directory vanishes as soon as the child does, even if the parent's camped out there. And this may not even be a concern, but that seems a bit fragile if my child manages to exit before I can grab its PID and read the symlink.
Different55 (111 rep)
Feb 1, 2023, 01:05 AM
1 votes
1 answers
1741 views
new tab in bash on Arch Linux starts at home directory
On my Arch box, when I hit Ctrl+Alt+T `bash` opens a new tab in the Terminal's window. The `pwd` of this new tab is the current user's home directory. On my Ubuntu box at work however, the `pwd` of the new tab is the same `pwd` of the old tab. How can I tell `bash` on my Arch box to behave like `bas...
On my Arch box, when I hit Ctrl+Alt+T bash opens a new tab in the Terminal's window. The pwd of this new tab is the current user's home directory. On my Ubuntu box at work however, the pwd of the new tab is the same pwd of the old tab. How can I tell bash on my Arch box to behave like bash on my Ubuntu box? Is it even a bash thing, or does Ubuntu include a customized variant of bash?
Dohn Joe (215 rep)
Sep 18, 2016, 12:30 PM • Last activity: Dec 17, 2021, 10:01 AM
1 votes
1 answers
208 views
Get the unresolved pwd of a shell from another process
I'm [hitting an issue](https://github.com/microsoft/vscode/issues/129023) where I need to get the unresolved symlink of a shell process. For example given a symlink `~/link` -> `~/actual`, if bash is launched with a `$PWD` of `~/link`, I need to fetch that from outside the bash process. Getting the...
I'm [hitting an issue](https://github.com/microsoft/vscode/issues/129023) where I need to get the unresolved symlink of a shell process. For example given a symlink ~/link -> ~/actual, if bash is launched with a $PWD of ~/link, I need to fetch that from outside the bash process. Getting the _resolved_ cwd is possible using lsof or /proc as called out in https://unix.stackexchange.com/a/94359/115410 but I'm beginning to think it's not possible to get the _unresolved_ path. I have tried to use lsof -b to not use readlink but the logging says that path never tries to use readlink anyway. It does appear possible to read the environment via /proc/.../environ and parse out PWD but this is slow, /proc may not exist on the system and I believe there are some security implications to trying to read a processes' environment. Here is the code in question I'm trying to fix: [lsof on macOS](https://github.com/microsoft/vscode/blob/723ce3f4be196696c195f6c05511b853b05c9e84/src/vs/platform/terminal/node/terminalProcess.ts#L489) :
lang-ts
exec('lsof -OPln -p ' + this._ptyProcess.pid + ' | grep cwd', (error, stdout, stderr) => {
  ...
});
[/proc on Linux](https://github.com/microsoft/vscode/blob/723ce3f4be196696c195f6c05511b853b05c9e84/src/vs/platform/terminal/node/terminalProcess.ts#L507) :
lang-ts
Promises.readlink(/proc/${this._ptyProcess.pid}/cwd);
Daniel Imms (113 rep)
Jul 22, 2021, 12:26 PM • Last activity: Jul 22, 2021, 06:50 PM
1 votes
2 answers
1024 views
Terminal has changed its default folder to start at
By default, every Linux distributions' Terminal I have used so far starts at `/home/[username]`. For some reason, without noticing, the default folder is now at root in Sabayon. How do I change the default starting folder in Terminal to be my username folder again?
By default, every Linux distributions' Terminal I have used so far starts at /home/[username]. For some reason, without noticing, the default folder is now at root in Sabayon. How do I change the default starting folder in Terminal to be my username folder again?
user120282
Feb 20, 2016, 02:06 AM • Last activity: May 22, 2021, 01:30 AM
13 votes
2 answers
3409 views
Why do all users start in /root instead of their home directories after su as root?
I feel like this should be straightforward but I've never seen anyone ask this that I can tell. The situation is pretty straight forward. Whenever I become a user, ie `su user` it always starts in /root directory instead of it's home directory. Let me show you. [root@st-test2 ~]# grep "postgres" /et...
I feel like this should be straightforward but I've never seen anyone ask this that I can tell. The situation is pretty straight forward. Whenever I become a user, ie su user it always starts in /root directory instead of it's home directory. Let me show you. [root@st-test2 ~]# grep "postgres" /etc/passwd postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql/:/bin/bash [root@st-test2 ~]# su postgres bash-4.2$ pwd /root [root@st-test2 ~]# ls -lhart /var/lib |grep postgres drwx------. 4 postgres postgres 86 May 5 16:07 pgsql So, you can see that the postgres user's home directory exists and that its set in /etc/passwd...but for some reason, they start in the root directory. This happens with every user that I have created and I have no idea why. I can't say that I've ever seen this happen before either.
Chris Jones (233 rep)
May 5, 2021, 08:55 PM • Last activity: May 10, 2021, 03:08 PM
0 votes
1 answers
191 views
Why cwd in /proc/nautilus_pid/cwd always points to /home/username?
I wanted to check the current directory opened in nautilus window. I check the pid of nautilus, and symbolic link `proc/pid/cwd`, always points to home directory. Is this expected behaviour? If so thenhow to check the opened directory of nautilus from command line?
I wanted to check the current directory opened in nautilus window. I check the pid of nautilus, and symbolic link proc/pid/cwd, always points to home directory. Is this expected behaviour? If so thenhow to check the opened directory of nautilus from command line?
Prvt_Yadav (6032 rep)
Feb 4, 2021, 02:21 AM • Last activity: Feb 5, 2021, 03:04 PM
6 votes
3 answers
5085 views
"ls $PWD" and "ls ." get different files, strange caching perhaps?
I have a strange problem on my Linux Mint machine (with solid-state drive if that matters). Somehow the machine (on multiple occasions) gets *different* files, and different content for the same file, if I do "ls $PWD" instead of "ls ." or just "ls". That means I can write the file into the present...
I have a strange problem on my Linux Mint machine (with solid-state drive if that matters). Somehow the machine (on multiple occasions) gets *different* files, and different content for the same file, if I do "ls $PWD" instead of "ls ." or just "ls". That means I can write the file into the present working directory and pick up something different copying it from another directory. It's not $PWD set to the wrong thing, I can hand-type the directory name. I've checked this pretty carefully, but it does come and go. That makes it hard to make and to test production scripts and code. adam@RADIUM:/home/adam/cd2/adam_dev/rsim ==> ls . ClearPrice.cme Makefile.win data gfiles rsim5.tmp src zlib ClearPrice.src ReadMe.txt err include rsimdone.txt tools Makefile build g2f out scripts vs2013 adam@RADIUM:/home/adam/cd2/adam_dev/rsim ==> adam@RADIUM:/home/adam/cd2/adam_dev/rsim ==> echo $PWD /home/adam/cd2/adam_dev/rsim adam@RADIUM:/home/adam/cd2/adam_dev/rsim ==> ls $PWD ClearPrice.cmd Makefile.win err include scripts vs2013 ClearPrice.src ReadMe.txt g2f out src zlib Makefile build gfiles rsimdone.txt tools adam@RADIUM:/home/adam/cd2/adam_dev/rsim ==>
Adam N. Rosenberg (61 rep)
Jul 12, 2016, 09:17 PM • Last activity: Dec 7, 2020, 11:51 AM
1 votes
1 answers
3895 views
handling $1 in shell script directory path
I have this part of the script(bash) that worked absolutely fine until recently but it throws me an error with file not found when using $1 to give directory path. Only difference I know is it stopped working after we upgraded to rhel 7. here is the part of the script: MOUNTINST=/opt/mqm80_Inst1 $1/...
I have this part of the script(bash) that worked absolutely fine until recently but it throws me an error with file not found when using $1 to give directory path. Only difference I know is it stopped working after we upgraded to rhel 7. here is the part of the script: MOUNTINST=/opt/mqm80_Inst1 $1/crtMQdir.ksh $MOUNTINST/bin 755 mqm $1/crtMQdir.ksh $MOUNTINST/logs 755 mqm $1/crtMQdir.ksh $MOUNTINST/java/lib/soap 755 mqm I am in the right directory path and also made sure the file exists: /var/mqm/trace/WMQUNX8000.02] $ ll *crt* -rwxr-xr-x. 1 mqm mqm 826 Apr 10 2014 crtMQdir.ksh Here is the error I get: ./test5.sh: line 4: /crtMQdir.ksh: No such file or directory ./test5.sh: line 6: /crtMQdir.ksh: No such file or directory ./test5.sh: line 8: /crtMQdir.ksh: No such file or directory Am I missing something ?
MO12 (409 rep)
Jun 29, 2017, 08:41 PM • Last activity: Oct 31, 2019, 04:01 PM
8 votes
1 answers
1511 views
Can I create a one way symlink?
I want to create a one way symlink i.e. I can use it to go to the destination directory but I cannot go back. Let's say there is a directory called `D` with two subdirectories `S1` and `S2`. I want to create a link in `S1` that points to `S2` (let's say `ls2 -> ../S2/`). If I do `cd ls2` and then `c...
I want to create a one way symlink i.e. I can use it to go to the destination directory but I cannot go back. Let's say there is a directory called D with two subdirectories S1 and S2. I want to create a link in S1 that points to S2 (let's say ls2 -> ../S2/). If I do cd ls2 and then cd .. then I want to go to D and not S1. Is it possible?
WYSIWYG (363 rep)
Oct 16, 2019, 01:16 PM • Last activity: Oct 16, 2019, 01:27 PM
2 votes
1 answers
1753 views
The current path of a process
Suppose I write a program, which outputs a file under a relative path (as opposed to a full/absolute path) - let's say, the current path. Then I compile it and store the executable under some dir `path1`. 1. Now I run the executable, while I am under a different dir `path2`. The executable will outp...
Suppose I write a program, which outputs a file under a relative path (as opposed to a full/absolute path) - let's say, the current path. Then I compile it and store the executable under some dir path1. 1. Now I run the executable, while I am under a different dir path2. The executable will output a file under path2 instead of path1. I wonder why the executable doesn't write a file under path1 instead? In other words, why is the "current path" path2 not path1? 2. If during run-time, the executable opens a file stored in path3, why does the current path become path3, although I run the executable from path2 and the executable was stored in path1? How does the OS assign and change the current path for a process, during it's run-time?
Tim (106420 rep)
Jul 10, 2014, 05:22 AM • Last activity: Jun 23, 2019, 01:17 PM
3 votes
0 answers
52 views
Shell: Mounting to CWD does not update in shell session
When mounting a partition into my CWD (`mount /path/do/devxy $(pwd)`), the current shell session does not recognize it, e.g. `find` does not show any contents of the partition and `umount $(pwd)` succeeds despite me being in the directory (keeping it busy). Only after explicitly entering into the di...
When mounting a partition into my CWD (mount /path/do/devxy $(pwd)), the current shell session does not recognize it, e.g. find does not show any contents of the partition and umount $(pwd) succeeds despite me being in the directory (keeping it busy). Only after explicitly entering into the directory via cd . I can browse the files and unmounting fails. So obviously the information on the changes in my CWD is not passed on. This behaviour is very different from e.g. entering a directory in one session and creating a file into that directory from another one. In this case find's results are up to date. So how does the shell determine where it is and why is the mounting not visible immediately? Using GNU bash, version 5.0.3(1) on debian 10. ----- Considered solved: please check comments.
FelixJN (14014 rep)
May 31, 2019, 07:50 AM • Last activity: May 31, 2019, 09:32 AM
7 votes
1 answers
1297 views
Making 'pushd' directory stack persistent
Ok this is a short question. I just happened to know that with `pushd` command, we can add more working directories into our list, which is handy. But is there a way to make this list permanent, so it can survive reboots or logoffs?
Ok this is a short question. I just happened to know that with pushd command, we can add more working directories into our list, which is handy. But is there a way to make this list permanent, so it can survive reboots or logoffs?
Sollosa (1993 rep)
May 16, 2017, 08:48 AM • Last activity: May 26, 2019, 08:45 AM
1 votes
1 answers
1179 views
Customize zsh prompt to separately show tail of current working dir
So my current prompt looks like this: `~/path/to/dir %` If possible, I'd like it to show the topmost dir separately, like this: `~/path/to (dir) %` I've gotten close! In my .zshrc, I currently have: `PROMPT="%~ (%1d) %#"` which outputs: `~/path/to/dir (dir) %` I just need to remove the last folder "...
So my current prompt looks like this: ~/path/to/dir % If possible, I'd like it to show the topmost dir separately, like this: ~/path/to (dir) % I've gotten close! In my .zshrc, I currently have: PROMPT="%~ (%1d) %#" which outputs: ~/path/to/dir (dir) % I just need to remove the last folder "dir" from the first path. Any ideas? (Note: I asked this question in Stack Overflow and got redirected here.)
user11319 (11 rep)
Nov 28, 2018, 07:09 PM • Last activity: Nov 28, 2018, 08:31 PM
23 votes
4 answers
3547 views
Is there a POSIX (or at least a popular) utility to set the current working directory when invoking a program?
We have [env(1)][1] to modify the environment of the command we want to run (for example `env MANPAGER=more man dtrace`). Is there something similar but for modifying the directory that the command is going to be started in? Ideally, I would like it to look like this: theMagicCommand /new/cwd myProg...
We have env(1) to modify the environment of the command we want to run (for example env MANPAGER=more man dtrace). Is there something similar but for modifying the directory that the command is going to be started in? Ideally, I would like it to look like this: theMagicCommand /new/cwd myProgram This way it could be "chained" with other env(1)-like commands, e.g., daemon -p /tmp/pid env VAR=value theMagicCommand /new/cwd myProgram ---- So far I can think of the following solution, which unfortunately does not have the same *interface* as env(1): cd /new/cwd && myProgram Also, I can just create a simple shell script like this: #! /bin/sh - cd "${1:?Missing the new working directory}" || exit 1 shift exec "${@:?Missing the command to run}" but I am looking for something that already exists (at least on macOS and FreeBSD). myProgram is not necessarily a desktop application (in which case I could just use the *Path* key in a .desktop file ).
Mateusz Piotrowski (4983 rep)
Nov 9, 2018, 03:01 PM • Last activity: Nov 11, 2018, 12:36 PM
5 votes
2 answers
564 views
Meaning of statement that 'getcwd functions work correctly' in FreeBSD man page for mount_nullfs?
In FreeBSD, [`man mount_nullfs`](https://www.freebsd.org/cgi/man.cgi?query=mount_nullfs&apropos=0&sektion=0&manpath=FreeBSD+11.1-RELEASE+and+Ports&arch=default&format=html) states that: > The primary differences between a virtual copy of the file system and a symbolic link are that **the [`getcwd(3)...
In FreeBSD, [man mount_nullfs](https://www.freebsd.org/cgi/man.cgi?query=mount_nullfs&apropos=0&sektion=0&manpath=FreeBSD+11.1-RELEASE+and+Ports&arch=default&format=html) states that: > The primary differences between a virtual copy of the file system and a symbolic link are that **the getcwd(3) functions work correctly** in the virtual copy, and that other file systems may be mounted on the virtual copy without affecting the original. A different device number for the virtual copy is returned by stat(2) , but in other respects it is indistinguishable from the original. What is the full meaning/implication of this paragraph?
Stilez (1311 rep)
Feb 26, 2018, 08:03 AM • Last activity: Feb 26, 2018, 11:17 AM
-4 votes
1 answers
282 views
Change directory to execute a script
In my script I create a directory and need to execute subsequent commands within the directory. The below script creates a directory, but the next script it invokes (`repo init`) does not get executed within that directory. mkcdir () { echo "creating directory" $1 mkdir -p -- ~/"$1" && cd -P --...
In my script I create a directory and need to execute subsequent commands within the directory. The below script creates a directory, but the next script it invokes (repo init) does not get executed within that directory. mkcdir () { echo "creating directory" $1 mkdir -p -- ~/"$1" && cd -P -- ~/"$1" } mkcdir $1 repo init -u git@github.com:P0/manifest.git -b refs/tags/$1 repo sync
user1050619 (745 rep)
Aug 5, 2017, 12:44 PM • Last activity: Aug 6, 2017, 05:11 AM
Showing page 1 of 20 total questions