DEV Community

Cover image for Linux's chmod vs. PATH: A Rant with Useful Explanations
Jabulani Meki
Jabulani Meki

Posted on

Linux's chmod vs. PATH: A Rant with Useful Explanations

Picture this: You're following a tutorial, typing commands like a wizard, when suddenly...

./install.sh: Permission denied
Enter fullscreen mode Exit fullscreen mode

(based on a true story)

Or worse:

command not found
Enter fullscreen mode Exit fullscreen mode

Congratulations! You've met Linux's gatekeepers: chmod and PATH. Let's dissect them without the usual jargon overdose.

Part 1: chmod (Or, "Why Can't I Run My Own Damn Script?")

What It Actually Means

Every file in Linux has permissions like a nightclub bouncer:

  • Read (r): "You can look at this file."

  • Write (w): "You can edit this file."

  • Execute (x): "You can run this file (if it's a script/program)."

The chmod Command

It changes permissions. Syntax:

chmod [who][+/-][permissions] file
Enter fullscreen mode Exit fullscreen mode

Examples:
Give yourself execute permission:

chmod u+x script.sh  # u = user (you), +x = add execute
Enter fullscreen mode Exit fullscreen mode

Lock down a file (read-only for everyone):

chmod 444 file.txt  # 4 = read, for user/group/others
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Octal Notation

Nerds love numbers. Here's the cheat sheet:

Number Permission
4 Read
2 Write
1 Execute

Add them up:

  • 7 = 4+2+1 (read+write+execute)
  • 6 = 4+2 (read+write)
  • 5 = 4+1 (read+execute)
chmod 755 script.sh  # You get everything, others get read/execute
Enter fullscreen mode Exit fullscreen mode

Part 2: PATH (Or, "Why Can't Linux Find My Program??")

What Is PATH?

A list of directories Linux checks when you type a command. Run this to see yours:

echo $PATH
# Output: /usr/local/bin:/usr/bin:/bin:/snap/bin
Enter fullscreen mode Exit fullscreen mode

The "Command Not Found" Crisis

If you install a program (like rustc or npm) and get this error, it means:

  • The program isn't in any PATH directory.
  • Linux is being deliberately obtuse.

Fixes:

  1. Add a Directory to PATH (Temporarily)
export PATH=$PATH:/path/to/your/program
Enter fullscreen mode Exit fullscreen mode

(Dies when you close the terminal.)

  1. Permanently Add to PATH

Edit your shell config file:

Bash: ~/.bashrc

Zsh: ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Add this line:

export PATH=$PATH:/path/to/your/program
Enter fullscreen mode Exit fullscreen mode

Then reload:

source ~/.bashrc  # or ~/.zshrc
Enter fullscreen mode Exit fullscreen mode
  1. Nuclear Option (Not Recommended)
sudo cp my_script /usr/local/bin  # Now it's in PATH... but pray you don't break anything.
Enter fullscreen mode Exit fullscreen mode

Real-World Example: Installing a Python Script

Permission Denied?

chmod +x cool_script.py
Enter fullscreen mode Exit fullscreen mode

"Command Not Found" After Moving It?

export PATH=$PATH:/home/yourname/scripts
Enter fullscreen mode Exit fullscreen mode

TL;DR

chmod +x file.sh = "Linux, let me run this!"

export PATH=$PATH:/new/path = "Linux, LOOK HARDER."
Enter fullscreen mode Exit fullscreen mode

Now go fix those errors before I rm -rf my patience.

Top comments (0)