1
BashERRORCriticalFilesystemHIGH confidence

Permission denied

What this means

The 'permission denied' error message, often accompanied by exit code 1, occurs when a command attempts to read, write, or access a file or directory without having the necessary permissions.

Why it happens
  1. 1Trying to read a file you do not have read access to.
  2. 2Trying to write to a file or directory you do not have write access to.
  3. 3Trying to list the contents of a directory you do not have read/execute access to.
How to reproduce

Attempting to create a file in a directory owned by root.

trigger — this will error
trigger — this will error
#!/bin/bash
touch /etc/new-file
echo "Exit: $?"

expected output

touch: cannot touch '/etc/new-file': Permission denied
Exit: 1

Fix 1

Correct file/directory permissions

WHEN You own the file or have sudo access

Correct file/directory permissions
# Grant write access to the group
chmod g+w /path/to/directory

Why this works

Use `chmod` to change permissions or `chown` to change ownership so that the user running the script has the required access.

Fix 2

Run with sufficient privileges

WHEN The action legitimately requires root privileges

Run with sufficient privileges
sudo your_command

Why this works

`sudo` executes the command as the root user, who typically has full permissions.

What not to do

Recursively run `chmod 777` on system directories

This makes files and directories world-writable, creating a massive security vulnerability.

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Bash errors