1. Introduction

Browsing data is a trivial but critical activity when using any system. A major part of that activity is switching directories, commonly with cd.

In this tutorial, we tackle the scenario in which we’re unable to enter a given directory. First, we delve into invalid paths in Linux. Next, we check other reasons for the inability to access a directory. After that, the focus is on the permissions we need to change into a directory and how to assess and correct them. Finally, we provide a comprehensive example.

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments.

2. Invalid Path

Since it’s perhaps one of the most common problems when using cd, let’s get invalid paths out of the way first.

To confirm our current location at different points, we use the pwd command. As usual, ls helps when listing directory contents.

2.1. Typos in the Path

While a human may detect simple typos like /dirs/ubdir instead of /dir/subdir or incorrect spelling, machines are rarely equipped to do so by default. The problem is tougher when we get similar-looking characters wrong:

$ cd /dirl
$ pwd
/dirl
$ cd /dir1
-bash: cd: /dir1: No such file or directory

Depending on the font, we can have trouble distinguishing a one (1) from a lowercase “L” (l). Of course, this is easy to correct by listing / in this example, but the problem may be at any point in a long path.

2.2. Incorrect Syntax

Linux has a strict standard for paths, which includes predefined tokens like the / forward slash separator.

Using a \ backslash instead of a forward slash can lead to incorrect results. For example, /dir/file is a file within /dir, but /dir\subdir is the dirsubdir file in root:

$ cd /dir/subdir
$ cd /dir\subdir
-bash: cd: /dirsubdir: No such file or directory

On the other hand, shells often treat multiple forward slashes in paths as one, so that’s acceptable.

Many UNIX tools also have switches that force input interpretation, output generation, or both to use NULL (\0) as the separator. The reason for this is that only NULL and forward slash are forbidden as parts of names in Linux, but the latter is the path separator.

2.3. Whitespace Not Escaped

Whitespaces are allowed in Linux paths. Even newlines can be a part of object names. This can be very confusing, especially when we list directory contents.

In fact, when a path contains any whitespace, we need to either wrap it in quotes or escape the whitespace:

$ cd /dir\ 1
$ pwd
/dir 1
$ cd /dir 1
-bash: cd: too many arguments

Here, we enter /dir 1 by escaping the single space and confirm we’re inside the correct directory. However, if we try to avoid escaping the space, cd complains about the number of arguments since 1 is superfluous.

2.4. Incorrect Capitalization

Finally, unlike Microsoft Windows, the Linux operating system is case-sensitive. This means that /dir/subDir is not the same as /dir/subdir.

To make things more problematic, we can have both at the same time:

$ ls /dir
subdir subDir

The situation is even worse when both are directories. Naturally, this is also an organizational problem, but still a valid scenario.

After looking at the many ways seemingly valid paths can actually be invalid, let’s explore what else can prevent us from entering a valid path.

3. Reasons for Directory Entry Problems

Of course, even with a valid path, different issues can arise when attempting to enter a directory. Let’s look at some of them.

3.1. Not a Directory

Supplying a path that’s not an actual directory is the first trivial reason for the inability to enter. For example, we might be attempting to access a nonexistent directory that was moved, deleted, or renamed if it even existed in the first place.

In other cases, the object we expect to be a directory can turn out to be a file.

3.2. Permissions

If we have a valid path that’s a directory, but we’re still unable to get in with cd, it’s most commonly due to the following error:

$ cd /dir/subdir
bash: cd: /dir/subdir: Permission denied

Here, the error text tells us that we don’t have the necessary permissions. Let’s dive further into this scenario and explore how to remedy it.

4. Checking and Changing Directory Permissions

Linux file and directory permissions dictate which actors can perform what actions over which objects.

An easy way to check the permissions for an object is the ls command:

$ ls -l /dir
[...]
drwxr-xr-x 2 baeldung baeldung 4096 Oct 28 16:50 subdir

In its long listing (-l) format, ls provides the following information about each item from the second row onwards, column by column:

  • Permissions
  • Number of links to the file
  • Owning user
  • Owning group
  • Size
  • Last modification date and time
  • Name

From these, the permissions and owning user and group are of interest to us. By default, each user is the sole member of a group with the user’s name.

Indeed, the permissions are represented by a string of letters. The first letter identifies the object type (d means directory). Following are three 3-letter clusters of rwx, responsible for the [r]ead, [w]rite, and e[x]excute permissions of the owning user, owning group, and everyone else, respectively. If a position has a dash instead of a letter, it means that the permission is unavailable for the given actor.

Importantly, to change into a directory, a user must have the x execute permission over it. This can happen if our user is:

  • the owning user, while there is an x in the first 3-letter cluster
  • part of the owning group, while there is an x in the second 3-letter cluster
  • not the owning user or part of the owning group, while there is an x in the third 3-letter cluster

Finally, we change the owning user and group via chown, while chmod is the command for changing all permissions.

Armed with the knowledge above, we can combine all commands in an example.

5. Example Scenario

Let’s create a path structure as user1:

$ whoami
user1
$ mkdir --parents /dir/subdir
$ ls -l /dir/
[...]
drwxr-xr-x 2 user1 user1 4096 Oct 28 16:56 subdir

After confirming the current user as user1, we create a new directory /dir with a subdirectory /dir/subdir via mkdir –parents. Its owner is user1 of the single-user user1 group.

5.1. Default Behavior

Let’s describe the default permissions of the subdirectory in this case:

  • user user1 has full permissions
  • group user1 does not have the write permission
  • everyone else also does not have the write permissions

Of course, that means we can freely cd into the directory as any user:

$ whoami
user2
$ cd /dir/subdir
$

How do we change that?

5.2. Deny Execute

Naturally, we can use chmod to strip the x permissions for all actors:

$ chmod -x /dir/subdir
$ ls -l /dir/
[...]
drw-r--r-- 2 user1 user1 4096 Oct 28 16:56 subdir
$ cd /dir/subdir
bash: cd: /dir/subdir: Permission denied

Now, we’re locked out. Still, we can change permissions as the owner or a superuser.

5.3. Superusers

As usual, even if we don’t have a particular permission as a given user, superusers can access and change most objects:

$ whoami
root
$ cd /dir/subdir
$

Of course, the same can be achieved with sudo and similar tools as long as our user is configured correctly:

$ sudo cd /dir/subdir
$

Even the owner does not always have all permissions.

5.4. Allow Execute for Others

Let’s add the execute permission only for non-owner users that are not part of the owner group:

$ chmod o+x /dir/subdir
$ ls -l /dir/
[...]
drw-r--r-x 2 user1 user1 4096 Oct 28 16:56 subdir

At this point, we can cd with user2 as long as it’s not part of the user1 group as returned by getent:

$ getent group user1
user1:user1:1001:
$ whoami
user2
$ cd /dir/subdir
$

However, we still can’t change to /dir/subdir with user1, as it’s the owner and doesn’t have the necessary permissions:

$ whoami
user1
$ cd /dir/subdir
bash: cd: /dir/subdir: Permission denied

To work around this, we can also change the owner.

5.5. Changing the Owner

We can use chown to modify the current owning user of /dir/subdir:

$ chown user2 /dir/subdir
$ ls -l /dir/
[...]
drw-r--r-x 2 user1 user1 4096 Oct 28 16:56 subdir

After this, we can’t access the subdirectory with user2 anymore since that user, as the owner, does not have the x permission:

$ whoami
user2
$ cd /dir/subdir
bash: cd: /dir/subdir: Permission denied

Note that the owner group has not changed, so now the permissions applicable to user1 come from the second 3-letter cluster.

5.6. Defaults

Finally, we can restore the defaults:

$ chown user1 /dir/subdir
$ chmod 0755 /dir/subdir
$ ls -l /dir/
[...]
drwxr-xr-x 2 user1 user1 4096 Oct 28 16:56 subdir

At this point, we’re back to the original permissions.

6. Summary

In this article, we explored why we might not be able to change into a given directory. Specifically, we paid attention to permissions problems and how to fix them.

In conclusion, while there are many reasons a directory can be inaccessible, we can typically remedy each one as long as we can identify it correctly.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.