Intermediate · 10 min · practise every command in the built-in terminal

chmod — Change File Mode

chmod changes file permissions.

Numeric mode

chmod 755 script.sh      # rwxr-xr-x
chmod 644 file.txt       # rw-r--r--
chmod 600 private.txt    # rw-------
chmod 777 public/        # rwxrwxrwx (dangerous!)

Symbolic mode

Format: [who][operator][permission]

Who: u (user/owner), g (group), o (others), a (all) Operator: + (add), - (remove), = (set exactly)

chmod +x script.sh          # Add execute for all
chmod u+x script.sh         # Add execute for owner only
chmod go-w file.txt         # Remove write from group and others
chmod a=r file.txt          # Set read-only for everyone
chmod u+x,go-x script.sh   # Multiple changes

Recursive

chmod -R 755 directory/    # Apply to all files inside

Make a script executable

This is the most common use:

chmod +x myscript.sh
./myscript.sh               # Now you can run it!

Special permissions

chmod 4755 file    # Setuid bit
chmod 2755 file    # Setgid bit
chmod 1755 dir     # Sticky bit

Practice tasks

  1. Create a file myscript.sh and make it executable
  2. Create private.txt and set permissions to 600 (owner read/write only)
  3. Verify the permissions with ls -la
  4. Remove write permission for group and others on private.txt
  5. Create a directory and recursively set its permissions to 755

Commands in this lesson

Permissions