Shell says getcwd() failed: No such file or directory
getcwd() failed: No such file or directoryShort answer
getcwd() fails when the process's current directory no longer exists. Usually the folder was deleted, moved or unmounted while a terminal or script was still inside it. Change to a directory that exists, then rerun. A process inside a chroot can hit the same error on glibc 2.27 or newer.
Fix it fast
1Find out which cause you have
Check whether the current directory still exists. If pwd errors or prints a path that no longer exists, the directory was removed under you.
pwd ls -ld .2Match what you see, then jump to its fix
What does the error look like?
getcwd() failed: No such file or directorysh: 0: getcwd() failed: No such file or directory
Cause 1 of 2
The working directory was deleted or replaced
1Check
The manual page for getcwd says ENOENT means the current working directory has been unlinked. The shell still holds the old directory, which no longer has a path.
ls -ld "$PWD"2Fix
Change to a directory that exists. If a deploy replaced the folder with a new one of the same name, cd to its path to enter the new copy.
cd ~
cd /path/to/project3Verify
pwd prints a path and the script runs.
pwdConfirmed by man7.org
Cause 2 of 2
The process is outside its own root (chroot)
1Check
The manual page says a process whose current directory lies outside its filesystem root, for example after chroot without changing directory, gets ENOENT from getcwd on glibc 2.27 and newer.
ldd --version | head -n 12Fix
chroot does not move the current directory, so change it to a path inside the new root right after the chroot call.
chroot(new_root);
chdir("/");3Verify
getcwd now returns a path.
pwdConfirmed by man7.org
How do I stop it coming back?
- Make cron jobs and deploy scripts start with cd to a stable absolute path.
- Deploy by switching a symlink, so a running shell is not left inside a folder that was deleted.
- Leave a folder before you remove or unmount it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
dnf cannot install MySQL on Amazon Linux 2023
dnf cannot find a mysql-server package on Amazon Linux 2023
Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.
3 causes, each with a fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix