Permission denied
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.
- 1Trying to read a file you do not have read access to.
- 2Trying to write to a file or directory you do not have write access to.
- 3Trying to list the contents of a directory you do not have read/execute access to.
Attempting to create a file in a directory owned by root.
#!/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
# 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
sudo your_command
Why this works
`sudo` executes the command as the root user, who typically has full permissions.
✕ 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