You tried to run a command and got this:

zsh: permission denied: somefile

This means you're trying to do something your user account isn't allowed to do. Your Mac is protecting itself - or you're just missing one small step.

Here's how to fix it.

The Two Main Causes

1. You're Trying to Run a Script That Isn't Executable

This is the most common cause. You downloaded or created a script file, but your Mac doesn't know it's supposed to be runnable.

Check the file's permissions:

ls -l yourscript.sh

You'll see something like:

-rw-r--r--  1 you  staff  1234 Dec 23 10:00 yourscript.sh

See those letters at the start? If there's no x in there, the file isn't executable.

The fix:

chmod +x yourscript.sh

Now try running it again:

./yourscript.sh

2. You Need Admin Privileges

Some commands require administrator access. You're trying to modify system files, install software globally, or change protected settings.

The fix:

Add sudo before your command:

sudo your-command-here

You'll be asked for your password. Type it (you won't see any characters appear - that's normal) and press Enter.

For example, if you're trying to edit a system file:

sudo nano /etc/hosts

How to Tell Which Fix You Need

Use chmod +x if:

  • You're trying to run a script you created or downloaded
  • The error mentions a specific file with an extension like .sh
  • You're using ./ before the filename

Use sudo if:

  • You're trying to install something
  • You're trying to modify files outside your home folder
  • You're trying to change system settings
  • The error mentions a path starting with /usr, /etc, /System, or /Library

Common Specific Fixes

Running a shell script you just created:

chmod +x myscript.sh
./myscript.sh

Installing something with a command that failed:

sudo your-install-command

Editing a file in /etc:

sudo nano /etc/hosts

Changing permissions on a folder:

sudo chmod -R 755 /path/to/folder

What the Permission Letters Mean

When you run ls -l, you see something like -rwxr-xr-x. Here's what that means:

Position Meaning
1 File type (- for file, d for directory)
2-4 Owner permissions (read, write, execute)
5-7 Group permissions
8-10 Everyone else's permissions

The letters:

  • r = read (can view the file)
  • w = write (can modify the file)
  • x = execute (can run the file as a program)
  • - = permission not granted

So -rwxr-xr-x means: the owner can read, write, and execute; everyone else can read and execute but not write.

When NOT to Use sudo

Don't use sudo for everything. It gives you root access, which can break things if you're not careful.

Don't use sudo for:

  • Normal file operations in your home folder
  • Running applications
  • Commands that don't specifically require it

If a command works without sudo, don't add it. Only use it when you get a permission error that specifically requires admin access.

"Operation Not Permitted" Is Different

If you see "Operation not permitted" instead of "permission denied," that's a different issue. It usually means macOS System Integrity Protection (SIP) is blocking you. This protects core system files and can't be bypassed with sudo.

For normal Terminal use, you shouldn't need to disable SIP. If something requires it, reconsider whether you really need to do it.

Still Not Working?

If chmod +x didn't help and sudo didn't help:

  1. Check if the file exists: ls -la yourfile
  2. Check who owns it: Look at the owner column in ls -l output
  3. Check the full path: Make sure you're in the right directory

If a file is owned by root and you need to modify it, use:

sudo chown \$(whoami) yourfile

This changes ownership to your user account.


Keep Learning

Permission issues trip up everyone at first. Once you understand the difference between "make it executable" and "run as admin," you'll fix these in seconds.

For a complete foundation in Terminal, check out the free course at Mac Terminal for Humans.