Fix “zsh: permissions denied” Error in Linux or macOS

Zsh permission denied is an error that occurs when you try to execute a command on Linux or Mac to which you don’t have execution permissions. The command could be as straightforward as performing an ‘ls’ on a directory for which you don’t have write permissions or executing a script that performs actions your user doesn’t have permission to do. By permissions, we refer to read or write permissions.

Let’s take a look at the main cause of this error, then we can try each solution to see which one works the best.

Mainly this error occurs when you don’t have permissions to execute, write or read the file.

Let’s assume we are inside a Zsh shell in a Linux operating system and you are trying to list the contents of a file using ‘cat’, but you receive a ‘permission denied’ error.

Now to fix this error, first we need to list the permissions of the file. We execute “ls -l” to see the permissions.

As you can see, the permissions are such that only root user and root group can read the file, while the root user can only write on the file.

1. Use sudo

To fix this, the simplest solution would be to use ‘sudo’ with the command we want to run. This will execute the command as the root user. In your case as well, you can use ‘sudo’ when you are confident that the command you want to execute or the task you want to run won’t harm your computer.

We can thus run:

sudo cat file

2. Change permissions of the file or files

The next thing we can do is to change permissions of the file. Since permission denied error is usually caused by read or write permissions, we can change the permissions of that particular file or files. 

By executing ‘ls -l’ inside the directory where the file is located, we can see that the owner and group of the file are root.

We can change the owner to our user and then do what we want to do with the file. 

We can run:

sudo chown [user] [file-name]

In our example, as v3n0m is the user we are logged in as, we will issue the command as:

sudo chown v3n0m file

v3n0m here is our user. In your case it would be your username. 

Now, we can run cat on the file as it would no longer give us a permission denied error

3. Enable Execute permissions

You can use ./ with a filename in zsh or other shells to execute a file as a program. But if the execute permissions are not set on a file, it will give you zsh : permission denied error.

Let’s say we want to execute a script called script1. We type ./script1 in our terminal and it gives us this error: 

We do ls -la on the file to see the permissions. As you can see the execute permissions on the file are not set. 

The solution here is simple, add the execute permissions on the file.
We issue a simple command 

chmod +x script1

chmod +x would add the execute bit on the file’s permission for owner, group as well as others. 

Now we can run the file as program by typing ./script1 without any error.

This should fix the error if you are facing it on Linux, however, if you are facing this error of MacOS or Gradle keep following the article.

Edit your zshrc File (macOS)

Some users have reported in their MacOS using zsh when they are not inside the directory from where they want to execute a command. 

If you face a similar issue, then the fix for it is really simple. 

You need to edit your .zshrc file in your home/username directory and add this to the end of the file. 

setopt auto_cd

This will enable the option of auto_cd in zsh.

In order to edit the .zshrc file, you can use any graphical text editor or a command line text editor like vim or vi.

Use CHMOD Command to Set Permissions (Gradle)

Zsh permission denied error can occur if you are working on gradle and the gradlew file doesn’t have the correct permissions. 

In order to fix such an error, you need to set the correct permissions on gradlew. 

You can type this command in your terminal,

chmod 755 gradlew 

This will make the gradlew file executable and the error will be fixed.

Or you can simple type this command chmod +x gradlew 

This will also make the gradlew file executable.

If none of these solutions worked for you, feel free to comment down below and our technician will be happy to help you shortly

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.