1. Overview

When we compile or run some commands on a Linux command line interface, we may encounter the error message “sh: 0: getcwd() failed: No such file or directory“. Even though it’s an error, the command may still execute successfully, but the error message itself will be displayed during the process.

In this tutorial, we’ll discuss what this error means, what causes it, and how to fix it.

We tested the solutions in this tutorial on Ubuntu 22.04 LTS. It should work in most POSIX-compliant environments.

2. What Does the Error Mean?

The error “sh: 0: getcwd() failed: No such file or directory” is a message from the shell that indicates that it can’t determine the current working directory. The current working directory (CWD) is the location in the file system where the shell is operating. It’s usually displayed in the prompt before the command input:

alex@ubuntu:~/Documents$

In this example, the CWD is ~/Documents which is a shorthand for /home/alex/Documents.

The system call getcwd() is a function that asks the operating system to return the CWD as a string. The operating system (OS) keeps track of the CWD for each process by using a data structure called an inode. An inode is a unique identifier for each file or directory in the file system.

When we change directories using commands like cd, we’re actually changing the inode number associated with our process. For example, when we type cd /home/alex/Documents, we’re telling the OS to change our process’s inode number to match that of /home/alex/Documents. The OS then updates our process’s CWD accordingly.

However, if another process deleted or moved our CWD while we are using the shell, then our process’s inode number will no longer match any existing file or directory in the file system. In that case, commands like sudo apt upgrade will run but will include this error message with our original output.

3. What Causes the Error?

The most common cause of this error is deleting or renaming the CWD from another terminal or file manager. For example, suppose we have a directory called foo in our tmp directory on our local machine. If we have two terminals open, and one of them is in /tmp/foo. In the other terminal, we delete or rename /tmp/foo directory. Running a command like sudo apt upgrade from the first terminal that has its CWD as /tmp/foo will give this error.

Another cause of this error is unmounting a removable device that contains the CWD. For example, if we have a USB flash drive mounted at /media/alex/USB and our CWD is /media/alex/USB/Documents. Then, running a command that requires the CWD after unmounting the USB flash drive will cause this error.

Another cause of this error is deleting or renaming the target directory that a symbolic link points to. For example, if we have a symbolic link in our CWD that points to a directory called foo in our tmp directory. Then, running a command like sudo apt upgrade from CWD after deleting the target will cause this error.

A less common but still possible cause of this error is that the directory we ran the command has a different signature because it was recreated with the same name but different contents. Let’s consider our example of the /tmp/foo directory. Suppose we have two terminals open and one of them is in /tmp/foo. In the other terminal, we delete /tmp/foo and create a new one with the same name. Now, if we try to run a command from the first terminal, we will get this error.

4. How Do We Fix the Error?

The scenarios discussed in the previous section are not exhaustive and there may be other ways to trigger the error. However, they all share a common pattern: something external to our shell process modifies our CWD without our knowledge or consent.

Generally, we can fix the getcwd() error by changing the CWD to a valid location before running our command. We can do this by using an absolute path to change to an existing directory, such as cd /. An absolute path doesn’t depend on our CWD and always points to the same location regardless of where we are.

We can also use a relative path to change to an existing parent directory, such as cd .. until we stop getting errors.

Also, if we noticed that the CWD is still present, it could mean the directory has been deleted and recreated, thereby having a different signature. To solve this, we can refresh our CWD to match the new signature. We can do this by typing cd ., i.e., cd followed by a period. This tells the shell to change our CWD to itself, which should update its information. Then we can try the command again.

5. Conclusion

In this article, we’ve looked at the error “sh: 0: getcwd() failed: No such file or directory” that occurs when using the shell. We’ve learned what this error means, what causes it, and how we can fix it.

This error isn’t a fatal error and can be easily fixed by changing the CWD to a valid location. We can also fix it by refreshing the CWD if we notice a change in the signature of the CWD.