Chmod Linux
Chmod Linux
Linux file permissions FAQ: Can you share some examples of the
Unix/Linux chmod command? (Also written as, "How do I change
permissions on Unix files and directories?")
The chmod command name stands for "change mode", and as that name implies, the chmod command
is used to change the mode of Unix/Linux files.
I'll start with some simple examples, then add some more details as we go along.
The chmod command is commonly used to make a file "executable", like this:
chmod +x myShellScript.sh
Typically you create a new Unix shell script, then make the script executable with that command. Once your new shell script is
executable, you can run your script like this:
myShellScript.sh
or this:
./myShellScript.sh
Another use of the chmod command is to make a file "readable". If a file doesn't have read permission, you can add read permission to that file like this:
chmod +r myfile.txt
This example assumes (a) you own this file or (b) you are the root Unix user (in which case you can change the permission on any file in the system).
In the same way, you can also give a file write permission, like this:
chmod +w myfile.txt
This example makes the same assumptions as the previous command, you either own the file, or you're logged in as the root user.
To really understand the details of the chmod command, you need to understand the output of the ls command. The relationship here is that the ls
command shows file permissions, and the chmod command lets you change file permissions. I explain this in a lot of detail in my Unix ls command
examples tutorial, so I'll just share the short story about file and directory permissions here.
In short, when you issue an ls command with the "long" output option (-l), like this, you'll see detailed output, as shown here:
$ ls -l *doc
-rw------- 1 stu1 class 4892 May 7 12:18 sales.doc
-rw-r--r-- 1 stu1 class 10874 May 7 11:27 mktg.doc
The permissions on your Linux files are shown in the first ten characters of those two lines. The first column doesn't matter for this discussion -- it
shows whether the file is a real file, directory, pipe, or something else -- but the next nine characters show the file permissions.
Those nine characters can be broken up into three groups of three characters each:
Fields 2-4 show the permissions for the owner of the file (the "user").
Fields 5-7 show the permissions for the group the user belongs to.
Fields 8-10 show the permissions for all other users on the system.
-rw-------
This means that the owner of the file has read (r) and write (w) permissions on the file, and no other permissions are set, meaning they are denied.
-rw-r--r--
This means that the owner of the file has read (r) and write (w) permissions on the file, users in the group have read permission (the 'r' in the 5th
column), and all other users on the system also have read permission (the 'r' in the 8th column).
The Linux `chmod` command | alvinalexander.com https://alvinalexander.com/linux-unix/linux-chmod-command-permissions-file-directories/
Given that extremely brief background on Unix/Linux file permissions, here is a collection of Unix chmod commands. In each example, assume we start
with a file named foo.txt that has no assigned permissions, like this:
----------
I'll then show the file permissions after the command I issue. First, in this example I'll add read permission to the file for all users:
chmod +r foo.txt
-r--r--r--
chmod +w foo.txt
--w--w--w-
Here I add both read and write permission to the file for all users:
Finally, here I add read, write, and execute permissions for all users:
4 of 13 28/07/2022 02:42
It's important to know that you can also control file permissions for the user (u), the user's group (g), and all others (o). Here's a command where I add
read permission for the current user:
As you can see, only the user has read permission on the file now. Similarly I can add write permission like this:
You can also add read, write, and execute permissions for the user with one command like this:
So far I've just shown "add" permissions with the plus sign (+) operator; this adds permissions to whatever permissions the file already has. You can
also use the equal sign operator (=) to set permissions exactly, instead of adding to file permissions as shown so far. Here's a typical chmod equal
permission assignment:
Whenever you need to subtract permissions from the permissions a file already has, you use the equal sign operator.
In the old days that's the way I learned to assign file permissions. These days you can do the same thing with numbers. When using numerical chmod
commands you use a sequence of three numbers, and the numbers again correspond to the user, group, and owner of the file.
Add read permission to the owner and the group for the file foo.txt:
and read permission for the user, group, and all others:
You can do the same thing to add write permission using the number six, like this:
And you can add execute permission to the file, like this:
7 of 13 28/07/2022 02:42
or this:
Finally, if you want to set and read and execute permissions -- typically done on a directory -- you use this command:
I hope these Unix/Linux chmod command examples have been helpful. There's much more to say about the chmod command, but I hope all of these
examples are enough to help explain most things you'll see on a daily basis.
related
Links:
front page photo gallery scala linux/unix alvin on twitter privacy terms & conditions
alvinalexander.com
is owned and operated by
Valley Programming, LLC
11 of 13 28/07/2022 02:42
The Linux `chmod` command | alvinalexander.com https://alvinalexander.com/linux-unix/linux-chmod-command-permissions-file-directories/
java
java tutorials
android tutorials
java applets
java faqs
java source code
intellij idea
eclipse ide
jdbc
jsp’s
java servlets
jfc-swing
kotlin
perl
perl tutorials
perl faqs
perl recipes
perl tutorials
Unix
linux tutorials
unix by example
tutorials
man (help) pages
programming
scala tutorials
drupal
flutter/dart
git
haskell
oop
php
mysql
postgresql
ruby
sencha
best practices
programming
software dev
testing
uml
12 of 13 28/07/2022 02:42
The Linux `chmod` command | alvinalexander.com https://alvinalexander.com/linux-unix/linux-chmod-command-permissions-file-directories/
misc
privacy policy
terms & conditions
apple/mac/iphone
career
design
funny/humor
function point
analysis (fpa)
fpa tutorial
gadgets
technology
LaTeX
alaska
colorado
cvs tutorials
wincvs tutorial
other
contact me
rss feed
my photos
inspirational quotes
investing
'personal' posts
science
zen, mindfulness
life in alaska
living in talkeetna, alaska
13 of 13 28/07/2022 02:42