Linux Basic Commands

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

Linux Shell or “Terminal”

So, basically, a shell is a program that receives commands from the user
and gives it to the OS to process, and it shows the output. Linux's shell is
its main part. Its distros come in GUI (graphical user interface), but
basically, Linux has a CLI (command line interface). In this tutorial, we
are going to cover the basic commands that we use in the shell of Linux.

To open the terminal, press Ctrl+Alt+T in Ubuntu, or press Alt+F2, type


in gnome-terminal, and press enter. In Raspberry Pi, type in lxterminal.
There is also a GUI way of taking it, but this is better!

Linux Commands

Basic Commands

1. pwd — When you first open the terminal, you are in the home
directory of your user. To know which directory you are in, you can use
the “pwd” command. It gives us the absolute path, which means the
path that starts from the root. The root is the base of the Linux file
system. It is denoted by a forward slash( / ). The user directory is usually
something like "/home/username".

2. ls — Use the "ls" command to know what files are in the directory
you are in. You can see all the hidden files by using the command “ls -
a”.

3. cd — Use the "cd" command to go to a directory. For example, if


you are in the home folder, and you want to go to the downloads folder,
then you can type in“cd Downloads”. Remember, this command is case
sensitive, and you have to type in the name of the folder exactly as it is.
But there is a problem with these commands. Imagine you have a folder
named “Raspberry Pi”. In this case, when you type in “cd Raspberry
Pi”, the shell will take the second argument of the command as a
different one, so you will get an error saying that the directory does not
exist. Here, you can use a backward slash. That is, you can use “cd
Raspberry\ Pi” in this case. Spaces are denoted like this: If you just
type “cd”and press enter, it takes you to the home directory. To go back
from a folder to the folder before that, you can type “cd ..” . The two
dots represent back.
4. mkdir & rmdir — Use the mkdir command when you need to create
a folder or a directory. For example, if you want to make a directory
called “DIY”, then you can type “mkdir DIY”. Remember, as told
before, if you want to create a directory named “DIY Hacking”, then
you can type “mkdir DIY\ Hacking”. Use rmdir to delete a directory.
But rmdir can only be used to delete an empty directory. To delete a
directory containing files, use rm.

5. rm - Use the rm command to delete files and directories. Use "rm -


r" to delete just the directory. It deletes both the folder and the files it
contains when using only the rm command.

6. touch — The touch command is used to create a file. It can be


anything, from an empty txt file to an empty zip file. For example,
“touch new.txt”.
7. man & --help — To know more about a command and how to use it,
use the man command. It shows the manual pages of the command. For
example, “man cd” shows the manual pages of the cd command. Typing
in the command name and the argument helps it show which ways the
command can be used (e.g., cd –help).

8. cp — Use the cp command to copy files through the command line. It


takes two arguments: The first is the location of the file to be copied, the
second is where to copy.
9. mv — Use the mv command to move files through the command line.
We can also use the mv command to rename a file. For example, if we
want to rename the file “text” to “new”, we can use “mv text new”. It
takes the two arguments, just like the cp command.

10. locate — The locate command is used to locate a file in a Linux


system, just like the search command in Windows. This command is
useful when you don't know where a file is saved or the actual name of
the file. Using the -i argument with the command helps to ignore the
case (it doesn't matter if it is uppercase or lowercase). So, if you want a
file that has the word “hello”, it gives the list of all the files in your
Linux system containing the word "hello" when you type in “locate -i
hello”. If you remember two words, you can separate them using an
asterisk (*). For example, to locate a file containing the words "hello"
and "this", you can use the command “locate -i *hello*this”.

Intermediate Commands

1. echo — The "echo" command helps us move some data, usually text
into a file. For example, if you want to create a new text file or add to an
already made text file, you just need to type in, “echo hello, my name is
alok >> new.txt”. You do not need to separate the spaces by using the
backward slash here, because we put in two triangular brackets when we
finish what we need to write.

2. cat — Use the cat command to display the contents of a file. It is


usually used to easily view programs.

3. nano, vi, jed — nano and vi are already installed text editors in the
Linux command line. The nano command is a good text editor that
denotes keywords with color and can recognize most languages.
And vi is simpler than nano. You can create a new file or modify a file
using this editor. For example, if you need to make a new file
named "check.txt", you can create it by using the command “nano
check.txt”. You can save your files after editing by using the sequence
Ctrl+X, then Y (or N for no). In my experience, using nano for HTML
editing doesn't seem as good, because of its color, so I
recommend jed text editor. We will come to installing packages soon.
4. sudo — A widely used command in the Linux command
line, sudo stands for "SuperUser Do". So, if you want any command to
be done with administrative or root privileges, you can use
the sudo command. For example, if you want to edit a file like viz. alsa-
base.conf, which needs root permissions, you can use the command –
sudo nano alsa-base.conf. You can enter the root command line using
the command “sudo bash”, then type in your user password. You can
also use the command “su” to do this, but you need to set a root
password before that. For that, you can use the command “sudo
passwd”(not misspelled, it is passwd). Then type in the new root
password.
5. df — Use the df command to see the available disk space in each of
the partitions in your system. You can just type in df in the command
line and you can see each mounted partition and their used/available
space in % and in KBs. If you want it shown in megabytes, you can use
the command “df -m”.

6. du — Use du to know the disk usage of a file in your system. If you


want to know the disk usage for a particular folder or file in Linux, you
can type in the command df and the name of the folder or file. For
example, if you want to know the disk space used by the documents
folder in Linux, you can use the command “du Documents”. You can
also use the command “ls -lah” to view the file sizes of all the files in a
folder.

7. tar — Use tar to work with tarballs (or files compressed in a tarball
archive) in the Linux command line. It has a long list of uses. It can be
used to compress and uncompress different types of tar archives
like .tar, .tar.gz, .tar.bz2,etc. It works on the basis of the arguments
given to it. For example, "tar -cvf" for creating a .tar archive, -xvf to
untar a tar archive, -tvf to list the contents of the archive, etc. Since it is
a wide topic, here are some examples of tar commands.

8. zip, unzip — Use zip to compress files into a zip archive,


and unzip to extract files from a zip archive.

9. uname — Use uname to show the information about the system your
Linux distro is running. Using the command “uname -a” prints most of
the information about the system. This prints the kernel release date,
version, processor type, etc.

10. apt-get — Use apt to work with packages in the Linux command
line. Use apt-get to install packages. This requires root privileges, so use
the sudocommand with it. For example, if you want to install the text
editor jed (as I mentioned earlier), we can type in the command “sudo
apt-get install jed”. Similarly, any packages can be installed like this. It
is good to update your repository each time you try to install a new
package. You can do that by typing “sudo apt-get update”. You can
upgrade the system by typing “sudo apt-get upgrade”. We can also
upgrade the distro by typing “sudo apt-get dist-upgrade”. The
command “apt-cache search” is used to search for a package. If you
want to search for one, you can type in “apt-cache search jed”(this
doesn't require root).
11. chmod — Use chmod to make a file executable and to change the
permissions granted to it in Linux. Imagine you have a python code
namednumbers.py in your computer. You'll need to run “python
numbers.py” every time you need to run it. Instead of that, when you
make it executable, you'll just need to run “numbers.py” in the terminal
to run the file. To make a file executable, you can use the command
“chmod +x numbers.py” in this case. You can use “chmod 755
numbers.py” to give it root permissions or “sudo chmod +x
numbers.py” for root executable. Here is some more information about
the chmod command.

12. hostname — Use hostname to know your name in your host or


network. Basically, it displays your hostname and IP address. Just typing
“hostname” gives the output. Typing in “hostname -I” gives you your
IP address in your network.
13. ping — Use ping to check your connection to a server. Wikipedia
says, "Pingis a computer network administration software utility used to
test the reachability of a host on an Internet Protocol (IP) network".
Simply, when you type in, for example, “ping google.com”, it checks if
it can connect to the server and come back. It measures this round-trip
time and gives you the details about it. The use of this command for
simple users like us is to check your internet connection. If it pings the
Google server (in this case), you can confirm that your internet
connection is active!

Tips and Tricks for Using Linux Command Line

 You can use the clear command to clear the


terminal if it gets filled up with too many
commands.
 TAB can be used to fill up in terminal. For
example, You just need to type “cd Doc” and
then TAB and the terminal fills the rest up and
makes it “cd Documents”.
 Ctrl+C can be used to stop any command in
terminal safely. If it doesn't stop with that,
then Ctrl+Z can be used to force stop it.
 You can exit from the terminal by using
the exit command.
 You can power off or reboot the computer by
using the command sudo haltand sudo reboot.

18 Tar Command Examples in Linux


by Ravi Saive | Published: September 15, 2012 | Last Updated: September 18, 2017
Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators | 4 Free Shell Scripting
eBooks

The Linux “tar” stands for tape archive, which is used by large number
of Linux/Unix system administrators to deal with tape drives backup. The tar
command used to rip a collection of files and directories into highly
compressed archive file commonly called tarball or tar, gzip and bzip in Linux.
The tar is most widely used command to create compressed archive files and
that can be moved easily from one disk to another disk or machine to
machine.

Linux Tar Command Examples


In this article we will be going to review and discuss various tar command
examples including how to create archive files using (tar, tar.gz and tar.bz2)
compression, how to extract archive file, extract a single file, view content of
file, verify a file, add files or directories to archive file, estimate the size of tar
archive file, etc.
The main purpose of this guide is to provide various tar command examples that
might be helpful for you to understand and become expert in tar archive
manipulation.

1. Create tar Archive File


The below example command will create a tar archive file tecmint-14-09-
12.tar for a directory /home/tecmint in current working directory. See the
example command in action.
# tar -cvf tecmint-14-09-12.tar /home/tecmint/

/home/tecmint/
/home/tecmint/cleanfiles.sh
/home/tecmint/openvpn-2.1.4.tar.gz
/home/tecmint/tecmint-14-09-12.tar
/home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
/home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

Let’s discuss each option that we have used in the above command for
creating a tar archive file.

 c – Creates a new .tar archive file.


 v – Verbosely show the .tar file progress.
 f – File name type of the archive file.
2. Create tar.gz Archive File
To create a compressed gzip archive file we use the option as z. For example
the below command will create a compressed MyImages-14-09-12.tar.gz file for
the directory /home/MyImages. (Note : tar.gz and tgz both are similar).
# tar cvzf MyImages-14-09-12.tar.gz /home/MyImages
OR
# tar cvzf MyImages-14-09-12.tgz /home/MyImages

/home/MyImages/
/home/MyImages/Sara-Khan-and-model-Priyanka-Shah.jpg
/home/MyImages/RobertKristenviolent101201.jpg
/home/MyImages/Justintimerlake101125.jpg
/home/MyImages/Mileyphoto101203.jpg
/home/MyImages/JenniferRobert101130.jpg
/home/MyImages/katrinabarbiedoll231110.jpg
/home/MyImages/the-japanese-wife-press-conference.jpg
/home/MyImages/ReesewitherspoonCIA101202.jpg
/home/MyImages/yanaguptabaresf231110.jpg

3. Create tar.bz2 Archive File


The bz2 feature compress and create archive file less than the size of the gzip.
The bz2 compression takes more time to compress and decompress files as
compared to gzip which takes less time. To create highly compressed tar file
we use option as j. The following example command will create a Phpfiles-
org.tar.bz2 file for a directory /home/php. (Note: tar.bz2 and tbz is similar as tb2).
# tar cvfj Phpfiles-org.tar.bz2 /home/php
OR
# tar cvfj Phpfiles-org.tar.tbz /home/php
OR
# tar cvfj Phpfiles-org.tar.tb2 /home/php

/home/php/
/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
/home/php/video_title.php
/home/php/report.php
/home/php/object.html
/home/php/video.php
4. Untar tar Archive File
To untar or extract a tar file, just issue following command using
option x (extract). For example the below command will untar the
file public_html-14-09-12.tar in present working directory. If you want to untar in
a different directory then use option as -C (specified directory).
## Untar files in Current Directory ##
# tar -xvf public_html-14-09-12.tar

## Untar files in specified Directory ##


# tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/

/home/public_html/videos/
/home/public_html/videos/views.php
/home/public_html/videos/index.php
/home/public_html/videos/logout.php
/home/public_html/videos/all_categories.php
/home/public_html/videos/feeds.xml

5. Uncompress tar.gz Archive File


To Uncompress tar.gz archive file, just run following command. If would like to
untar in different directory just use option -C and the path of the directory, like
we shown in the above example.
# tar -xvf thumbnails-14-09-12.tar.gz

/home/public_html/videos/thumbnails/
/home/public_html/videos/thumbnails/katdeepika231110.jpg
/home/public_html/videos/thumbnails/katrinabarbiedoll2311
10.jpg
/home/public_html/videos/thumbnails/onceuponatime101125.j
pg
/home/public_html/videos/thumbnails/playbutton.png
/home/public_html/videos/thumbnails/ReesewitherspoonCIA10
1202.jpg
/home/public_html/videos/thumbnails/snagItNarration.jpg
/home/public_html/videos/thumbnails/Minissha-Lamba.jpg
/home/public_html/videos/thumbnails/Lindsaydance101201.jp
g
/home/public_html/videos/thumbnails/Mileyphoto101203.jpg

6. Uncompress tar.bz2 Archive File


To Uncompress highly compressed tar.bz2 file, just use the following
command. The below example command will untar all the .flv files from the
archive file.

# tar -xvf videos-14-09-12.tar.bz2

/home/public_html/videos/flv/katrinabarbiedoll231110.flv

/home/public_html/videos/flv/BrookmuellerCIA101125.flv

/home/public_html/videos/flv/dollybackinbb4101125.flv

/home/public_html/videos/flv/JenniferRobert101130.flv

/home/public_html/videos/flv/JustinAwardmovie101125.flv

/home/public_html/videos/flv/Lakme-Fashion-Week.flv

/home/public_html/videos/flv/Mileyphoto101203.flv

/home/public_html/videos/flv/Minissha-Lamba.flv

7. List Content of tar Archive File


To list the contents of tar archive file, just run the following command with
option t (list content). The below command will list the content
of uploadprogress.tar file.
# tar -tvf uploadprogress.tar

-rw-r--r-- chregu/staff 2276 2011-08-15 18:51:10


package2.xml
-rw-r--r-- chregu/staff 7877 2011-08-15 18:51:10
uploadprogress/examples/index.php
-rw-r--r-- chregu/staff 1685 2011-08-15 18:51:10
uploadprogress/examples/server.php
-rw-r--r-- chregu/staff 1697 2011-08-15 18:51:10
uploadprogress/examples/info.php
-rw-r--r-- chregu/staff 367 2011-08-15 18:51:10
uploadprogress/config.m4
-rw-r--r-- chregu/staff 303 2011-08-15 18:51:10
uploadprogress/config.w32
-rw-r--r-- chregu/staff 3563 2011-08-15 18:51:10
uploadprogress/php_uploadprogress.h
-rw-r--r-- chregu/staff 15433 2011-08-15 18:51:10
uploadprogress/uploadprogress.c
-rw-r--r-- chregu/staff 1433 2011-08-15 18:51:10
package.xml

8. List Content tar.gz Archive File


Use the following command to list the content of tar.gz file.
# tar -tvf staging.tecmint.com.tar.gz

-rw-r--r-- root/root 0 2012-08-30 04:03:57


staging.tecmint.com-access_log
-rw-r--r-- root/root 587 2012-08-29 18:35:12
staging.tecmint.com-access_log.1
-rw-r--r-- root/root 156 2012-01-21 07:17:56
staging.tecmint.com-access_log.2
-rw-r--r-- root/root 156 2011-12-21 11:30:56
staging.tecmint.com-access_log.3
-rw-r--r-- root/root 156 2011-11-20 17:28:24
staging.tecmint.com-access_log.4
-rw-r--r-- root/root 0 2012-08-30 04:03:57
staging.tecmint.com-error_log
-rw-r--r-- root/root 3981 2012-08-29 18:35:12
staging.tecmint.com-error_log.1
-rw-r--r-- root/root 211 2012-01-21 07:17:56
staging.tecmint.com-error_log.2
-rw-r--r-- root/root 211 2011-12-21 11:30:56
staging.tecmint.com-error_log.3
-rw-r--r-- root/root 211 2011-11-20 17:28:24
staging.tecmint.com-error_log.4

9. List Content tar.bz2 Archive File


To list the content of tar.bz2 file, issue the following command.
# tar -tvf Phpfiles-org.tar.bz2

drwxr-xr-x root/root 0 2012-09-15 03:06:08


/home/php/
-rw-r--r-- root/root 1751 2012-09-15 03:06:08
/home/php/iframe_ew.php
-rw-r--r-- root/root 11220 2012-09-15 03:06:08
/home/php/videos_all.php
-rw-r--r-- root/root 2152 2012-09-15 03:06:08
/home/php/rss.php
-rw-r--r-- root/root 3021 2012-09-15 03:06:08
/home/php/index.php
-rw-r--r-- root/root 2554 2012-09-15 03:06:08
/home/php/vendor.php
-rw-r--r-- root/root 406 2012-09-15 03:06:08
/home/php/video_title.php
-rw-r--r-- root/root 4116 2012-09-15 03:06:08
/home/php/report.php
-rw-r--r-- root/root 1273 2012-09-15 03:06:08
/home/php/object.html

10. Untar Single file from tar File


To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the following
command.
# tar -xvf cleanfiles.sh.tar cleanfiles.sh
OR
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh
cleanfiles.sh

11. Untar Single file from tar.gz File


To extract a single file tecmintbackup.xml from tecmintbackup.tar.gz archive file,
use the command as follows.
# tar -zxvf tecmintbackup.tar.gz tecmintbackup.xml
OR
# tar --extract --file=tecmintbackup.tar.gz tecmintbackup.xml

tecmintbackup.xml

12. Untar Single file from tar.bz2 File


To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the
following option.
# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php
OR
# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php

/home/php/index.php

13. Untar Multiple files from tar, tar.gz and tar.bz2 File
To extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For
example the below command will extract “file 1” “file 2” from the archive files.
# tar -xvf tecmint-14-09-12.tar "file 1" "file 2"

# tar -zxvf MyImages-14-09-12.tar.gz "file 1" "file 2"

# tar -jxvf Phpfiles-org.tar.bz2 "file 1" "file 2"

14. Extract Group of Files using Wildcard


To extract a group of files we use wildcard based extracting. For example, to
extract a group of all files whose pattern begins with .php from a tar, tar.gz and
tar.bz2 archive file.
# tar -xvf Phpfiles-org.tar --wildcards '*.php'

# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'

# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'

/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
/home/php/video_title.php
/home/php/report.php
/home/php/video.php

15. Add Files or Directories to tar Archive File


To add files or directories to existing tar archived file we use the
option r (append). For example we add file xyz.txt and directory php to
existing tecmint-14-09-12.tar archive file.
# tar -rvf tecmint-14-09-12.tar xyz.txt

# tar -rvf tecmint-14-09-12.tar php

drwxr-xr-x root/root 0 2012-09-15 02:24:21


home/tecmint/
-rw-r--r-- root/root 15740615 2012-09-15 02:23:42
home/tecmint/cleanfiles.sh
-rw-r--r-- root/root 863726 2012-09-15 02:23:41
home/tecmint/openvpn-2.1.4.tar.gz
-rw-r--r-- root/root 21063680 2012-09-15 02:24:21
home/tecmint/tecmint-14-09-12.tar
-rw-r--r-- root/root 4437600 2012-09-15 02:23:41
home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
-rw-r--r-- root/root 12680 2012-09-15 02:23:41
home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
-rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt
drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/
-rw-r--r-- root/root 1751 2012-09-15 03:06:08 php/iframe_ew.php
-rw-r--r-- root/root 11220 2012-09-15 03:06:08 php/videos_all.php
-rw-r--r-- root/root 2152 2012-09-15 03:06:08 php/rss.php
-rw-r--r-- root/root 3021 2012-09-15 03:06:08 php/index.php
-rw-r--r-- root/root 2554 2012-09-15 03:06:08 php/vendor.php
-rw-r--r-- root/root 406 2012-09-15 03:06:08 php/video_title.php

16. Add Files or Directories to tar.gz and tar.bz2 files


The tar command don’t have a option to add files or directories to an existing
compressed tar.gz and tar.bz2archive file. If we do try will get the following
error.
# tar -rvf MyImages-14-09-12.tar.gz xyz.txt

# tar -rvf Phpfiles-org.tar.bz2 xyz.txt

tar: This does not look like a tar archive


tar: Skipping to next header
xyz.txt
tar: Error exit delayed from previous errors

17. How To Verify tar, tar.gz and tar.bz2 Archive File


To verfify any tar or compressed archived file we use option as W (verify). To
do, just use the following examples of command. (Note : You cannot do
verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).
# tar tvfW tecmint-14-09-12.tar

tar: This does not look like a tar archive


tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: VERIFY FAILURE: 30740 invalid headers detected
Verify -rw-r--r-- root/root 863726 2012-09-15 02:23:41
/home/tecmint/openvpn-2.1.4.tar.gz
Verify -rw-r--r-- root/root 21063680 2012-09-15 02:24:21
/home/tecmint/tecmint-14-09-12.tar
tar: /home/tecmint/tecmint-14-09-12.tar: Warning: Cannot
stat: No such file or directory
Verify -rw-r--r-- root/root 4437600 2012-09-15 02:23:41
home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
tar: /home/tecmint/phpmyadmin-2.11.11.3-
1.el5.rf.noarch.rpm: Warning: Cannot stat: No such file
or directory
Verify -rw-r--r-- root/root 12680 2012-09-15 02:23:41
home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
tar: /home/tecmint/rpmforge-release-0.5.2-
2.el5.rf.i386.rpm: Warning: Cannot stat: No such file or
directory
Verify -rw-r--r-- root/root 0 2012-08-18 19:11:04
xyz.txt
Verify drwxr-xr-x root/root 0 2012-09-15 03:06:08
php/

18. Check the Size of the tar, tar.gz and tar.bz2 Archive File
To check the size of any tar, tar.gz and tar.bz2 archive file, use the following
command. For example the below command will display the size of archive
file in Kilobytes (KB).
# tar -czf - tecmint-14-09-12.tar | wc -c
12820480

# tar -czf - MyImages-14-09-12.tar.gz | wc -c


112640

# tar -czf - Phpfiles-org.tar.bz2 | wc -c


20480

Tar Usage and Options


 c – create a archive file.
 x – extract a archive file.
 v – show the progress of archive file.
 f – filename of archive file.
 t – viewing content of archive file.
 j – filter archive through bzip2.
 z – filter archive through gzip.
 r – append or update files or directories to existing archive file.
 W – Verify a archive file.
 wildcards – Specify patterns in unix tar command.
That’s it for now, hope the above tar command examples are enough for you to
learn and for more information please use man tar command.
If you are looking to split any large tar archive file into multiple parts or blocks,
just go through this article:

Don’t Miss: Split Large ‘tar’ Archive into Multiple Files of Certain Size
If we’ve missed any example please do share with us via comment box and
please don’t forget to share this article with your friends. This is the best way
to say thanks

Linux chmod command


Updated: 05/04/2019 by Computer Hope

In Unix-like operating systems,


the chmod command sets
the permissions of files or directories.

This document describes


the GNU/Linux version of chmod.
 Description

 Syntax

 Viewing permissions in the file listing

 Examples

 Related commands

 Linux commands help

Description
On Unix-like operating systems, a set
of flags associated with each file determines
who can access that file, and how they can
access it. These flags are called
file permissions or modes, as in "mode of
access." The command name chmod stands
for "change mode." It restricts the way a file
can be accessed.

For more information about file modes,


see What Are File Permissions, And How Do
They Work? in our documentation of
the umask command. It contains a
comprehensive description of how to define
and specify file permissions.

In general, chmod commands take the


form:

chmod options permissions file name


If no options are specified, chmod modifies
the permissions of the file specified by file
name to the permissions specified
by permissions.

permissions defines the permissions for the


owner of the file (the "user"), members of
the group who owns the file (the "group"),
and anyone else ("others"). There are two
ways to represent these permissions: with
symbols (alphanumeric characters), or
with octalnumbers (the digits 0 through 7).

Let's say you are the owner of a file


named myfile, and you want to set its
permissions so that:

1. the user can read, write, and execute it;


2. members of your group can read and execute it; and
3. others may only read it.

This command will do the trick:

chmod u=rwx,g=rx,o=r myfile

This example uses symbolic permissions


notation. The letters u, g, and o stand for
"user", "group", and "other". The equals
sign ("=") means "set the permissions
exactly like this," and the letters "r", "w",
and "x" stand for "read", "write", and
"execute", respectively. The commas
separate the different classes of permissions,
and there are no spaces in between them.

Here is the equivalent command using octal


permissions notation:

chmod 754 myfile

Here the digits 7, 5, and 4 each individually


represent the permissions for the user,
group, and others, in that order. Each digit is
a combination of the numbers 4, 2, 1,
and 0:

 4 stands for "read",

 2 stands for "write",

 1 stands for "execute", and

 0 stands for "no permission."

So 7 is the combination of
permissions 4+2+1 (read, write, and
execute), 5 is 4+0+1 (read, no write, and
execute), and 4 is 4+0+0(read, no write,
and no execute).

Syntax
chmod [OPTION]... MODE[,MODE]... FILE...

chmod [OPTION]... OCTAL-MODE FILE...


chmod [OPTION]... --reference=RFILE FILE...

Options
-c, --changes Like --verbose, but gives verbose output only when a change is actu
-f, --silent, --quiet Quiet mode; suppress most error messages.
-v, --verbose Verbose mode; output a diagnostic message for every file processed
--no-preserve-root Do not treat '/' (the root directory) in any special way, which is the d
--preserve-root Do not operate recursively on '/'.
--reference=RFILE Set permissions to match those of file RFILE, ignoring any specifie
-R, --recursive Change files and directories recursively.
--help Display a help message and exit.
--version Output version information and exit.

Technical Description
chmod changes the file mode of each
specified FILE according to MODE, which can
be either a symbolic representation of
changes to make, or an octal number
representing the bit pattern for the new
mode bits.

The format of a symbolic mode is:

[ugoa...][[+-=][perms...]...]

where perms is either zero or more letters


from the set r, w, x, X, s and t, or a single
letter from the set u, g, and o. Multiple
symbolic modes can be given, separated by
commas.

A combination of the letters u, g, o,


and a controls which users' access to the file
will be changed: the user who owns it (u),
other users in the file's group (g), other
users not in the file's group (o), or all users
(a). If none of these are given, the effect is
as if a were given, but bits that are set in
the umask are not affected.

The operator + causes the selected file mode


bits to be added to the existing file mode bits
of each file; - causes them to be removed;
and =causes them to be added and causes
unmentioned bits to be removed except that
a directory's unmentioned set user and group
ID bits are not affected.

The letters r, w, x, X, s and t select file


mode bits for the affected users: read (r),
write (w), execute (x), execute only if the
file is a directory or already has execute
permission for some user (X), set user or
group ID on execution (s), restricted deletion
flag or sticky bit (t). For directories, the
execute options X and X define permission to
view the directory's contents.
Instead of one or more of these letters, you
can specify exactly one of the letters u, g,
or o: the permissions granted to the user
who owns the file (u), the permissions
granted to other users who are members of
the file's group (g), and the permissions
granted to users that are in neither of the
two preceding categories (o).

A numeric mode is from one to four octal


digits (0-7), derived by adding up the bits
with values 4, 2, and 1. Omitted digits are
assumed to be leading zeros. The first digit
selects the set user ID (4) and set group ID
(2) and restricted deletion or sticky (1)
attributes. The second digit selects
permissions for the user who owns the read
(4), write (2), and execute (1); the third
selects permissions for other users in the
file's group, with the same values; and the
fourth for other users not in the file's group,
with the same values.

chmod never changes the permissions


of symbolic links; the chmodsystem call
cannot change their permissions. However,
this is not a problem since the permissions of
symbolic links are never used. However, for
each symbolic link listed on the command
line, chmodchanges the permissions of the
pointed-to file. In contrast, chmodignores
symbolic links encountered
during recursive directory traversals.

Setuid And Setgid Bits


chmod clears the set-group-ID bit of a
regular file if the file's group ID does not
match the user's effective group ID or one of
the user's supplementary group IDs, unless
the user has appropriate privileges.
Additional restrictions may cause the set-
user-ID and set-group-ID bits
of MODE or RFILE to be ignored. This
behavior depends on the policy and
functionality of the
underlying chmod system call. When in
doubt, check the underlying system
behavior.

chmod preserves a directory's set-user-ID


and set-group-ID bits unless you explicitly
specify otherwise. You can set or clear the
bits with symbolic modes like u+s and g-s,
and you can set (but not clear) the bits with
a numeric mode.

Restricted Deletion Flag (or "Sticky Bit")


The restricted deletion flag or sticky bit is a
single bit, whose interpretation depends on
the file type. For directories, it prevents
unprivileged users from removing or
renaming a file in the directory unless they
own the file or the directory; this is called the
restricted deletion flag for the directory, and
is commonly found on world-writable
directories like /tmp. For regular files on
some older systems, the bit saves the
program's text image on the swap device so
it will load more quickly when run; this is
called the sticky bit.

Viewing Permissions of files


A quick and easy way to list a file's
permissions are with the long listing (-l)
option of the ls command. For example, to
view the permissions of file.txt, you could
use the command:

ls -l file.txt

...which displays output that looks like the


following:

-rwxrw-r-- 1 hope hopestaff 123 Feb 03 15:36


file.txt

Here's what each part of this information


means:
- The first character represents the file type: "-" for a regular file, "d" for a dir
symbolic link.
rwx The next three characters represent the permissions for the file's owner: in th
may read from, write to, ore xecute the file.
rw- The next three characters represent the permissions for members of the file g
member of the file's owning group may read from or write to the file. The fin
placeholder; group members do not have permission to execute this file.
r-- The permissions for "others" (everyone else). Others may only read this file.
1 The number of hard links to this file.
hope The file's owner.
hopestaff The group to whom the file belongs.
123 The size of the file in blocks.
Feb 03 15:36 The file's mtime (date and time when the file was last modified).
file.txt The name of the file.

Examples
chmod 644 file.htm

Set the permissions of file.htm to "owner


can read and write; group can read only;
others can read only".

chmod -R 755 myfiles

Recursively (-R) Change the permissions of


the directory myfiles, and all folders and
files it contains, to mode 755: User can read,
write, and execute; group members and
other users can read and execute, but cannot
write.

chmod u=rw example.jpg

Change the permissions for the owner


of example.jpg so that the owner may read
and write the file. Do not change the
permissions for the group, or for others.

chmod u+s comphope.txt

Set the "Set-User-ID" bit of comphope.txt,


so that anyone who attempts to access that
file does so as if they are the owner of the
file.

chmod u-s comphope.txt

The opposite of the above command; un-sets


the SUID bit.

chmod 755 file.cgi

Set the permissions of file.cgi to "read,


write, and execute by owner" and "read and
execute by the group and everyone else".

chmod 666 file.txt


Set the permission of file.txt to "read and
write by everyone.".

chmod a=rw file.txt

Accomplishes the same thing as the above


command, using symbolic notation.

You might also like