40_basic_linux_commands_used_frequently
40_basic_linux_commands_used_frequently
In this tutorial, I will show the very basic Linux commands with examples that are frequently
used to get you more familiar with the Linux command line. To be an expert in Linux first step
for a beginner would be to start learning the basic commands.
The command is followed by options (optional of course) and a list of arguments. The options
can 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).
Syntax
$ pwd
/home/raghu
2) cal command
$ 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
3) echo command
$ 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
4) date command
$ date
If you are interested only in time, you can use 'date +%T' (in hh:mm:ss):
$ date +%T
01:13:14
5) tty command
$ tty
/dev/pts/0
6) whoami command
$ whoami
raghu
7) id command
This command prints user and groups (UID and GID) of the current user.
$ id
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
Help command
Nobody can remember all the commands. We can use help option from command like
9) help option
With almost every command, ‘--help’ option shows usage summary for that command.
$ date --help
Display the current time in the given FORMAT, or set the system date.
This command gives a one line description about the command. It can be used as a quick
reference for any command.
$ whatis date
$ whatis whatis
‘--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
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
$ cd [path-to-directory]
Change the current working directory to the directory provided as argument. If no argument is
given to ‘cd’, it changes the directory to the 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.
$ ls [files-or-directories]
List files and/or directories. If no argument is given, the contents of current directory are
shown.
$ ls
If a directory is given as an argument, files and directories in that directory are shown.
$ ls /usr
$ ls -l
total 4
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. The next 9 characters are
permissions ('rwxr-xr-x' in first listing). The number following the permissions is the link count.
Link count follows user and group owner. In the above example, the file owner is 'raghu' and
group owner is 'raghu' as well. Next is the size of the 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
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/
$ mkdir example
$ ls -l
total 4
total 4
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:
$ ls -l
total 8
Copy files and directories. If the source is a file, and the destination (file) name does not exit,
then source is copied with new name i.e. with the name provided as the destination.
$ cp usrlisting listing_copy.txt
$ ls -l
total 12
$ cp listing_copy.txt example/
$ ls -l example/
total 4
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.
$ ls -l example/
total 4
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
$ 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
Here, 'listing_copy.txt' is moved with the name 'usrcopy' in the same directory (or you can say
that it has been renamed).
$ 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 files|directories
$ rm file2
$ rm -r example/
$ ls -l
total 8
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.
The file command determines the file type of a given file. For example:
$ file /etc/passwd
idl.rar List.pdf
td.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Lin
To check the status of a file. This provides more detailed information about a file than ‘ls -l’
output.
$ stat usrcopy
File: `usrcopy'
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
23) 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'.
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
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
26) 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.
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:
sshd:x:113:65534::/var/run/sshd:/usr/sbin/nologin
28) ln command
The ln command is used in linux to create links. Links are a kind of shortcuts to other files.
The general form of command is:
$ 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
Text Editors
29) 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
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
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.
30) 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.
The 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.
~
~
Useful commands
31) alias command
The ‘alias’ is another name for a command. If no argument is given, it shows current aliases.
Aliases can be used for short names of commands. For example, you might use the clear
command frequently. You can create an alias for it:
$ 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'
32) 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
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
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
34) du command
The 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/
35) df command
$ df
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
The 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
switch off the mode (command 'c') and change display units to
Command action
d delete a partition
Pressing ‘m’ at the fdisk prompt prints out the help shown above that lists all the commands
available for fdisk. A new partition can be created with 'n' and an existing partition can be
deleted with the 'd' command. When you are done editing the partitions, press 'w' to write the
changes to the disk, and finally, hit 'q' to quit from fdisk (q does not save changes).
37) 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
History command shows the commands you have entered on your terminal so far.
In Linux, you can use shutdown command to gracefully halt your system. Most commonly
used command is shutdown -h now .
Read Also:
Leave a Comment
LinOxide
Back to top