Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Shell says getcwd() failed: No such file or directory

Errorgetcwd() failed: No such file or directory

Short 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.

  • Applies to Linux, glibc 2.39, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 .
  2. 2Match what you see, then jump to its fix

    1. 1A deploy, git checkout, rm -rf or unmount ran while the shell sat in that folderThe working directory was deleted or replacedFix
    2. 2The failing program runs in a chroot or container tool, and the current directory was never changed to a place inside itThe process is outside its own root (chroot)Fix

What does the error look like?

  • getcwd() failed: No such file or directory
  • sh: 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/project

3Verify

pwd prints a path and the script runs.

pwd

Confirmed by man7.org

Did this fix it?

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 1

2Fix

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.

pwd

Confirmed by man7.org

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.