Basic Linux Commands
Basic Linux Commands
http://linoxide.com/linux-command/essential-linux-basic-commands/
Linux based OS are very powerful. But their true power lies in the command line. There is a lot that
you can do with commands but can't otherwise (using GUI). In this article, we will find out about
basic Linux commands that are most frequently used. Now, let's start from the beginning.
Syntax
The commands in Linux have following syntax:
The command is followed by options (optional of course) and a list of arguments. The options modify
the behavior of a command. The arguments may be files or directories or some other data on which
the command acts. Every command might not need arguments. Some commands work with or
without them (e.g. ‘ls’ command).The options can be provided in two ways: full word options with --
(e.g. --help), or single letter options with - (e.g. -a -b -c or multiple options, -abc).
pwd command
‘pwd’ command prints the absolute path to current working directory.
$ pwd
/home/raghu
cal command
Displays calendar of 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 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
echo command
This command will echo whatever you provide it.
$ echo "linoxide.com"
linoxide.com
‘echo’ command is used to display the values of variables. One such variable is ‘HOME’. To check
the value of a variable precede the variable with a $ sign.
$ echo $HOME
/home/raghu
date command
Display 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
tty command
Displays current terminal.
$ tty
/dev/pts/0
whoami command
This command reveals the current logged in user.
$ whoami
raghu
id command
This command prints user and groups (UID and GID) of 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(samb
ashare)
$ id root
uid=0(root) gid=0(root) groups=0(root)
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.
whatis command
This command gives one line description about the command. It can be used for quick reference for
a 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
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 elaborated than 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
Change the current working directory to the directory provided as argument. If no argument is given
to ‘cd’, it changes the directory to that user's home directory. The directory path can be an absolute
path or relative to current directory. The absolute path always starts with /. The current directory can
be checked with ‘pwd’ command (remember?).
$ 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.
List files and/or directories. If no argument is given, the contents of current directory are shown.
$ ls
example file1.txt file2.txt file3.txt
If a directory is given as argument, files and directories in that directory are shown.
$ ls /usr
bin games include lib lib64 local sbin share src
In this long listing, the first character is 'd' or '-'. It distinguishes between file types. The entries with a
'-' (dash) are regular files, and ones with 'd' are directories. Next 9 characters are permissions ('rwxr-
xr-x' in first listing). The number following the permissions is link count. Link count follows user and
group owner. In above example, the file owner is 'raghu' and group owner 'raghu' as well. Next is the
size of file. And then time stamp before the name of file (or directory).
By default hidden files or directories are not shown, to see hidden files as well, -a option is used.
Hidden files in Linux start with a period sign (.). Any file that starts with a period is hidden. So to hide
a file, you just need to rename it (and put a period before it).
$ 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/
$ mkdir example
$ ls -l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
touch command
For creating an empty file, use touch command.
If a file already exists, touch will update its time stamp.There are a lot of other methods to create a
new file, e.g. using a text editor like vi or gedit, or using redirection. Here is an example of creating a
file using redirection:
Copy files and directories. If source is a file and destination (file) name does not exit, then source is
copied with new name i.e. with the name provided as destination.
$ cp usrlisting listing_copy.txt
$ ls -l
total 12
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 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 listing_copy.txt
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting
If the destination is a directory, then the file is copied with its original name in that directory.
$ 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 be copied, but in that case, last argument will be expected to be a directory where
all the files are to be copied. And rest arguments will be treated as file names.
If a directory is to be copied, then it must be copied recursively with the files contained in it. To copy
a directory recursively, use -r option with ‘cp’ command.
$ cp -r example /tmp/expertslogin/
$ ls -l /tmp/expertslogin
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 16:12 example
Move command
$ mv source destination
Move files or directories. 'mv' command works like cp command, except that the original file is
removed. But 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).
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 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, first the file named 'file2' is removed, and then the directory 'example' is removed recursively.
This can be seen in the output of ‘ls -l’ command that these two are no longer present.
$ file /etc/passwd
/etc/passwd: ASCII text
You can provide more than one file as an argument to file command
stat command
Check status of a file. This is more detailed information about a file than ‘ls -l’ output.
$ 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
'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 whole. But if the file is big enough to fit into one screen, then we will be
able to see only last page of the file. The commands 'less' and 'more' display files one page at a
time. So they are also called as pagers. You can navigate through file using arrow keys. To quit from
a pager, hit 'q'.
head command
Display first few lines of the file. By default ‘head’ command displays first 10 lines of a file. But with -
n option, 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 as ‘head’; ‘tail’ shows 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
‘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. If we wish to find the lines containing the word
‘nologin’, we use ‘grep’ as:
$ 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 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
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text^T To Spell
Having made all the changes to the file, press ‘ctrl+o’ to write the changes to the file and ‘ctrl+x’ to
exit from the editor. There are a lot of functions available with this editor. The help menu can be
accessed with ‘ctrl+g’ keystrokes.
VI editor
vi is VIsual editor; another text editor in Linux. This is standard editor in many Linux/Unix
environments. This is the default editor that ships with many Linux distributions. It might be possible
that it is the only text editor available with your distro.
$ 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).Second mode is 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
Next time you enter c on command line, your screen will be 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 what users are logged in to the system, and what command they are
executing at that 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 system (in the first line of
output above).
last command
Display information about the users who logged in and out of the system. The output of last can be
very large, so the following output has been filtered (through head) to display 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 last bad login attempts. But this command must be run as
root otherwise you would get an error of 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 command
du command determines disk usage of a file. If the argument given to it is a directory, then it will list
disk usage of all the files and directories recursively under that directory:
$ du /etc/passwd
4 /etc/passwd
$ du hello/
52 hello/HelloApp
4 hello/orb.db/logs
20 hello/orb.db
108 hello/
df command
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 the tool for getting partition information, 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 is an interactive tool to edit the partition table. It takes a device (hard disk) as an argument,
whose partition table needs to be edited.
$ fdisk /dev/sda
Pressing ‘m’ at the fdisk prompt prints out above help that lists all the commands available for fdisk.
A new partition can be created with n and an existing partition can be deleted with d command.
When you are done editing the partitions, press w to write the changes to the disk, and finally, q to
quit from fdisk (q dies not save changes).
netstat command
‘netstat’ is the 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
Change your password with 'passwd' command.
shutdown -h now
Finally shut down your system using this command.