38 Basic Linux Commands To Learn With Examples: Syntax
38 Basic Linux Commands To Learn With Examples: Syntax
Examples
May 16, 2011 | By Bobbin Zachariah
Linux based Operating Systems are very powerful but their true power lies in the
command line. There is a lot that you can do with the help of commands but can't
otherwise (using GUI). In this article, we will find out about basic Linux
commands that are used most frequently. Now, let's start from the beginning.
Syntax
The commands in Linux have the following syntax:
$ pwd
/home/raghu
The cal command:
Displays the calendar of the current month.
$ cal
July 2012
Su Mo Tu We Th Fr Sa
1234567
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
‘cal ’ will display calendar for the specified month and year.
$ cal 08 1991
August 1991
Su Mo Tu We Th Fr Sa
123
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
The echo command:
This command will echo whatever you provide it.
$ echo "linoxide.com"
linoxide.com
The ‘echo’ command is used to display the values of a variable. One such
variable is ‘HOME’. To check the value of a variable precede the variable with a $
sign.
$ echo $HOME
/home/raghu
The date command:
Displays current time and date.
$ date
Fri Jul 6 01:07:09 IST 2012
If you are interested only in time, you can use 'date +%T' (in hh:mm:ss):
$ date +%T
01:13:14
The tty command:
Displays current terminal.
$ tty
/dev/pts/0
The whoami command:
This command reveals the user who is currently logged in.
$ whoami
raghu
The id command:
This command prints user and groups (UID and GID) of the current user.
$ id
uid=1000(raghu) gid=1000(raghu)
groups=1000(raghu),4(adm),20(dialout),24(cdrom),46(plugdev),112(lpadmin),120
(admin),122(sambashare)
By default, information about the current user is displayed. If another username
is provided as an argument, information about that user will be printed:
$ id root
uid=0(root) gid=0(root) groups=0(root)
The clear command:
This command clears the screen.
--help option
With almost every command, ‘--help’ option shows usage summary for that
command.
$ date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
The whatis command
This command gives a one line description about the command. It can be
used as a quick reference for any command.
$ whatis date
date (1) - print or set the system date and time
$ whatis whatis
whatis (1) - display manual page descriptions
Manual Pages
‘--help’ option and ‘whatis’ command do not provide thorough information about
the command. For more detailed information, Linux provides man pages and info
pages. To see a command's manual page, man command is used.
$ man date
The man pages are properly documented pages. They have following sections:
REPORTING BUGS: Link of website or mail-id where you can report any bug.
SEE ALSO: Any commands related to the command, for further reference.
With -k option, a search through man pages can be performed. This searches for
a pattern in the name and short description of a man page.
$ man -k gzip
gzip (1) - compress or expand files
lz (1) - gunzips and shows a listing of a gzip'd tar'd archive
tgz (1) - makes a gzip'd tar archive
uz (1) - gunzips and extracts a gzip'd tar'd archive
zforce (1) - force a '.gz' extension on all gzip files
Info pages
Info documents are sometimes more elaborate than the man pages. But for
some commands, info pages are just the same as man pages. These are like
web pages. Internal links are present within the info pages. These links are called
nodes. Info pages can be navigated from one page to another through these
nodes.
$ info date
$ pwd
/home/raghu
$ cd /usr/share/
$ pwd
/usr/share
$ cd doc
$ pwd
/usr/share/doc
In the first ‘cd’ command, absolute path (/usr/share) is used, and with second
command, relative path (doc) is used.
$ ls
example file1.txt file2.txt file3.txt
If a directory is given as an argument, files and directories in that directory are
shown.
$ ls /usr
bin games include lib lib64 local sbin share src
‘ls -l’ displays a long listing of the files.
$ ls -l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 12:52 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file1.txt
$ ls -la odesk
total 16
drwxr-xr-x 4 raghu raghu 4096 2012-07-06 13:46 .
drwxr-xr-x 11 raghu raghu 4096 2012-07-06 13:15 ..
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 12:52 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file1.txt
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file2.txt
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file3.txt
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 13:46 .hiddendir
-rw-r--r-- 1 raghu raghu 0 2012-07-06 13:46 .hiddenfile1.txt
-rw-r--r-- 1 raghu raghu 0 2012-07-06 13:46 .hiddenfile2.txt
If you want to see the properties of a directory instead of the files contained in it,
use -d (with -l) option:
$ ls -ld odesk/
drwxr-xr-x 4 raghu raghu 4096 2012-07-06 13:46 odesk/
Creating files and directories Command
mkdir command
To create a directory, the ‘mkdir’ command is used.
$ mkdir example
$ ls -l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
The touch command
For creating an empty file, use the touch command.
$ cp listing_copy.txt example/
$ ls -l example/
total 4
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:07 listing_copy.txt
Multiple files can also be copied, but in that case, the last argument will be
expected to be a directory where all the files are to be copied. And the rest of
the arguments will be treated as file names.
$ cp -r example /tmp/expertslogin/
$ ls -l /tmp/expertslogin
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 16:12 example
The move command
$ mv source destination
Move files or directories. The 'mv' command works like 'cp' command, except that
the original file is removed. But, the mv command can be used to rename the
files (or directories).
$ mv listing_copy.txt usrcopy
$ ls -l
total 12
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 16:10 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file2
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:02 usrcopy
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting
Here, 'listing_copy.txt' is moved with the name 'usrcopy' in the same directory (or
you can say that it has been renamed).
To remove or Delete
$ rmdir
'rmdir' command removes any empty directories, but cannot delete a directory if
a file is present in it. To use ‘rmdir’ command, you must first remove all the files
present in the directory you wish to remove (and possibly directories if any).
$ rm file2
$ rm -r example/
$ ls -l
total 8
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:02 usrcopy
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting
Here, the file named 'file2' is removed first, and then the directory 'example' is
removed recursively. This can be seen in the output of ‘ls -l’ command where
these two are no longer present.
$ stat usrcopy
File: `usrcopy'
Size: 491 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 149452 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ raghu) Gid: ( 1000/ raghu)
Access: 2012-07-06 16:07:06.413522009 +0530
Modify: 2012-07-06 16:02:30.204152386 +0530
Change: 2012-07-06 16:17:18.992559654 +0530
cat command
The 'cat' command is actually a concatenator, but can be used to view the
contents of a file.
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
pagers
The cat command lists file as a whole. But if the file is big enough to fit into one
screen, then we will be able to see only the last page of the file. The commands
'less' and 'more' display files one page at a time. So they are also called pagers.
You can navigate through a file using arrow keys. To quit from a pager, hit 'q'.
head command
Displays the first few lines of a file. By default, the ‘head’ command displays
the first 10 lines of a file. But with -n option, the number of lines to be viewed can
be specified.
$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
tail command
Similar to ‘head’; the ‘tail’ command shows the last 10 lines by default, and -n
option is available as well.
$ tail -n 4 /etc/passwd
raghu:x:1000:1000:Raghu Sharma,,,:/home/raghu:/bin/bash
sshd:x:113:65534::/var/run/sshd:/usr/sbin/nologin
dictd:x:114:123:Dictd Server,,,:/var/lib/dictd:/bin/false
mysql:x:115:124:MySQL Server,,,:/nonexistent:/bin/false
wc command
Word count
This command counts lines, words and letters of the input given to it.
$ wc /etc/passwd
35 57 1698 /etc/passwd
The /etc/passwd file has 35 lines, 57 words, and 1698 letters present in it.
grep command
The ‘grep’ command searches for a pattern in a file (or standard input). It
supports regular expressions. It returns a line if it matches the pattern in that line.
So, if we wish to find the lines containing the word ‘nologin’, we use ‘grep’ as
follows:
$ ln TARGET LINK_NAME
There are two types of links, soft links and hard links. By default, hard links are
created. If you want to create soft link, use -s option. In this example, both types
of links are created for the file usrlisting.
$ ln usrlisting hard_link
$ ln -s usrlisting soft_link
$ ls -l
total 12
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 2 raghu raghu 491 2012-07-06 14:23 hard_link
lrwxrwxrwx 1 raghu raghu 10 2012-07-09 14:00 soft_link -> usrlisting
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:02 usrcopy
-rw-r--r-- 2 raghu raghu 491 2012-07-06 14:23 usrlisting
Text Editors
Pico & Nano
‘Pico’ is a text editor in Linux. ‘Nano’ editor is inspired from ‘pico’. They work
almost the same. If the argument given as filename exists, then that file will be
opened for editing in pico/nano. Otherwise, a new file with that name will be
created. Let’s create a new file named hello.txt:
$ pico hello.txt
GNU nano 2.2.6 File: hello.txt Modified
VI editor
The VI stands for Visual editor; another text editor in Linux. This is a standard
editor in many Linux/Unix environments. This is the default editor that comes with
many Linux distributions. It might be possible that it is the only text editor
available with your distro.
You can open a file with vi for editing using the following:
$ vi hello.txt
The vi editor has 3 modes in which it performs its functions.The default is
COMMAND mode, in which tasks like copy, paste, undo etc can be performed.
You can change a mode from command mode only (and come back to it).The
second mode is the INSERT mode, in which whatever key you type is treated as
a character and will be loaded into the file buffer. To enter this mode, press ‘i’
when in command mode.
Final mode is EX mode or last line mode. The changes made in the buffer can be
saved or discarded in this mode.
Hello world.
This file is edited using vi editor.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"hello.txt" 2 lines, 50 characters
$ alias c="clear"
Next time you enter 'c ' on command line, your screen will get clear. Current
aliases can be checked with 'alias' command:
$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo
error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias c='clear'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
w command
w command is used to check which users are logged in to the system, and what
command they are executing at that particular time:
$w
10:06:56 up 57 min, 3 users, load average: 0.04, 0.06, 0.09
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 10:06 28.00s 1.02s 0.67s pager -s
raghu tty7 :0 09:19 57:33 1:22 0.20s gnome-session --session=classic-gnome
raghu pts/0 :0.0 09:34 0.00s 0.78s 0.00s w
It also shows the uptime, number of users logged in and load average of the
system (in the first line of output above).
last command
Displays information about the users who logged in and out of the system. The
output of the last command can be very large, so the following output has been
filtered (through head) to display the top 10 lines only:
$ last | head
root tty1 Mon Jul 9 10:06 still logged in
root tty1 Mon Jul 9 10:06 - 10:06 (00:00)
raghu pts/1 :0.0 Mon Jul 9 10:05 - 10:06 (00:00)
raghu pts/0 :0.0 Mon Jul 9 09:34 still logged in
raghu tty7 :0 Mon Jul 9 09:19 still logged in
reboot system boot 2.6.38-13-generi Mon Jul 9 09:09 - 10:12 (01:02)
raghu tty7 :0 Sun Jul 8 23:36 - 00:30 (00:54)
reboot system boot 2.6.38-13-generi Sun Jul 8 23:36 - 00:30 (00:54)
raghu tty7 :0 Sun Jul 8 21:07 - down (01:06)
reboot system boot 2.6.38-13-generi Sun Jul 8 21:07 - 22:14 (01:07)
A similar command is 'lastb' that shows the last unsuccessful login attempts. But
this command must be run as root otherwise you would get an error saying
permission denied.
$ lastb
raghu tty2 Mon Jul 9 10:16 - 10:16 (00:00)
UNKNOWN tty2 Mon Jul 9 10:15 - 10:15 (00:00)
ubuntu tty8 :1 Mon Jul 2 10:23 - 10:23 (00:00)
$ du /etc/passwd
4 /etc/passwd
$ du hello/
52 hello/HelloApp
4 hello/orb.db/logs
20 hello/orb.db
108 hello/
df command
The df reports file system usage. For example:
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda7 10079084 7372872 2194212 78% /
none 1522384 768 1521616 1% /dev
none 1529012 252 1528760 1% /dev/shm
none 1529012 108 1528904 1% /var/run
none 1529012 4 1529008 1% /var/lock
/dev/sda8 5039616 3758824 1024792 79% /home
/dev/sda2 209715196 196519248 13195948 94% /media/Data
fdisk command
The fdisk is a tool for getting partition information, and for adding and removing
partitions.The fdisk tool requires super user privileges. To list all the partitions of
all the hard drives available:
$ fdisk -l
$ fdisk /dev/sda
netstat command
The ‘netstat’ is a command used to check the network statistics of the system. It
will list the current network connections, routing table information, interface
statistics, masquerade connections and a lot more information.
$ netstat | head
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 13 [ ] DGRAM 8498 /dev/log
unix 2 [ ] DGRAM 6824 @/org/kernel/udev/udevd
unix 3 [ ] STREAM CONNECTED 56738 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 56113
unix 3 [ ] STREAM CONNECTED 29138
unix 3 [ ] STREAM CONNECTED 29137
history command
This command shows the commands you have entered on your terminal so far.
passwd command
To change your password with 'passwd' command.
shutdown -h now
Finally, you can shut down your system using this command.