Intro To 'chmod' Command In Linux
2023-05-05 - By Robert Elder
I use the 'chmod' command to change the permission attributes of files and directories:
chmod ugo+rwx file1.txt
Adding Execute Permission With 'chmod'
For example, here's an executable script called 'do-stuff.sh' that I want to run:
#!/bin/bash
echo 'Hello World!'
But when I try to run this script, it says permission denied:
bash: ./do-stuff.sh: Permission denied
This is because the execute permission is not enabled on this file:
ls -l do-stuff.sh
-rw-rw-r-- 1 robert robert 32 May 6 16:18 do-stuff.sh
I can give the file's user the permission to execute the file using this command:
chmod u+x do-stuff.sh
And now, you can verify that the execute permission is now set for the user (indicated by the 'x' character below):
ls -l do-stuff.sh
-rwxrw-r-- 1 robert robert 32 May 6 16:18 do-stuff.sh
And now, I can run the script successfully:
./do-stuff.sh
outputs the following:
Hello World!
If I want to, I can also grant the execute permission to the user, group, and other using this command:
chmod ugo+x do-stuff.sh
And now, you can see that the 'x' character shows up in two more places for the 'group' and 'other':
ls -l do-stuff.sh
-rwxrwxr-x 1 robert robert 32 May 6 16:18 do-stuff.sh
Selectively Granting Permissions With 'chmod'
With the chmod command, you can selectively grant read, write, and execute permissions to the 'user', 'group', and 'other' by including only the 'u', 'g', 'o' and 'r', 'w', 'x' in this command:
chmod ugo+rwx file1.txt
Granting Permissions With Octal Format 'chmod'
Some people like to use octal notation to set the permissions. Let's create 6 examples files with different permissions to illustrate this:
touch file2.txt file3.txt file4.txt file5.txt file6.txt
chmod 777 file2.txt
chmod 000 file3.txt
chmod 700 file4.txt
chmod 123 file5.txt
chmod 456 file6.txt
And here are the permissions associated with these files:
ls -l file*.txt
-rwxrwxrwx 1 robert robert 0 May 6 16:33 file1.txt
-rwxrwxrwx 1 robert robert 0 May 6 16:33 file2.txt
---------- 1 robert robert 0 May 6 16:33 file3.txt
-rwx------ 1 robert robert 0 May 6 16:33 file4.txt
---x-w--wx 1 robert robert 0 May 6 16:33 file5.txt
-r--r-xrw- 1 robert robert 0 May 6 16:33 file6.txt
Each bit in the octal number corresponds to an individual permission:
And that's why the 'chmod' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|