Linux Tutorial
Linux Tutorial
Just to start with, the first thing a user should know is the basic commands in terminal. Linux
GUI runs on Shell. When GUI is not running but Shell is running, Linux is running. If Shell is
not running, nothing is running. Commands in Linux is a means of interaction with Shell. For a
beginners some of the basic computational task is to:
1. View the contents of a directory : A directory may contains visible and invisible files
with different file permissions.
2. Viewing blocks, HDD partition, External HDD
3. Checking the integrity of Downloaded/Transferred Packages
4. Converting and copying a file
5. Know your machine name, OS and Kernel
6. Viewing history
7. Being root
8. Make Directory
9. Make Files
10. Changing the file permission
11. Own a file
12. Install, Update and maintain Packages
13. Uncompressing a file
14. See current date, time and calendar
15. Print contents of a file
16. Copy and Move
17. See the working directory for easy navigation
18. Change the working directory, etc…
1. Command: ls
The command “ls” stands for (List Directory Contents), List the contents of the folder, be it file
or folder, from which it runs.
root@tecmint:~# ls
Android-Games Music
Pictures Public
Desktop Tecmint.com
Documents TecMint-Sync
Downloads Templates
The command “ls -l” list the content of folder, in long listing fashion.
root@tecmint:~# ls -l
total 40588
drwxrwxr-x 2 ravisaive ravisaive 4096 May 8 01:06 Android Games
drwxr-xr-x 2 ravisaive ravisaive 4096 May 15 10:50 Desktop
drwxr-xr-x 2 ravisaive ravisaive 4096 May 16 16:45 Documents
drwxr-xr-x 6 ravisaive ravisaive 4096 May 16 14:34 Downloads
drwxr-xr-x 2 ravisaive ravisaive 4096 Apr 30 20:50 Music
drwxr-xr-x 2 ravisaive ravisaive 4096 May 9 17:54 Pictures
drwxrwxr-x 5 ravisaive ravisaive 4096 May 3 18:44 Tecmint.com
drwxr-xr-x 2 ravisaive ravisaive 4096 Apr 30 20:50 Templates
Command “ls -a“, list the content of folder, including hidden files starting with ‘.’.
root@tecmint:~# ls -a
. .gnupg .dbus .goutputs
tream-PI5VVW .mission-control
.adobe deja-
dup .grsync .mozilla .themes
.gstreamer-0.10 .mtpaint .thumbnails .gtk-
bookmarks .thunderbird
.HotShots .mysql_history .htaccess .apport-
ignore.xml .ICEauthority
.profile .bash_history .icons .bash_
logout .fbmessenger
.jedit .pulse .bashrc .lifer
ea_1.8 .pulse-cookie
.Xauthority .gconf .local .Xautho
rity.HGHVWW .cache
.gftp .macromedia .remmina .cinna
mon .gimp-2.8
.ssh .xsession-errors .compiz .gnome
teamviewer_linux.deb
.xsession-errors.old
.config .gnome2 .zoncolor
Note: In Linux file name starting with ‘.‘ is hidden. In Linux every file/folder/device/command
is a file. The output of ls -l is:
For more “ls” command examples read 15 ‘ls’ Command Examples in Linux.
2. Command: lsblk
The “lsblk” stands for (List Block Devices), print block devices by their assigned name (but not
RAM) on the standard output in a tree-like fashion.
root@tecmint:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 232.9G 0 disk
├─sda1 8:1 0 46.6G 0 part /
├─sda2 8:2 0 1K 0 part
├─sda5 8:5 0 190M 0 part /boot
├─sda6 8:6 0 3.7G 0 part [SWAP]
├─sda7 8:7 0 93.1G 0 part /data
└─sda8 8:8 0 89.2G 0 part /personal
sr0 11:0 1 1024M 0 rom
The “lsblk -l” command list block devices in ‘list‘ structure (not tree like fashion).
root@tecmint:~# lsblk -l
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 232.9G 0 disk
sda1 8:1 0 46.6G 0 part /
sda2 8:2 0 1K 0 part
sda5 8:5 0 190M 0 part /boot
sda6 8:6 0 3.7G 0 part [SWAP]
sda7 8:7 0 93.1G 0 part /data
sda8 8:8 0 89.2G 0 part /personal
sr0 11:0 1 1024M 0 rom
Note: lsblk is very useful and easiest way to know the name of New Usb Device you just
plugged in, especially when you have to deal with disk/blocks in terminal.
3. Command: md5sum
The “md5sum” stands for (Compute and Check MD5 Message Digest), md5 checksum
(commonly called hash) is used to match or verify integrity of files that may have changed as a
result of a faulty file transfer, a disk error or non-malicious interference.
Note: The user can match the generated md5sum with the one provided officially. Md5sum is
considered less secure than sha1sum, which we will discuss later.
4. Command: dd
Command “dd” stands for (Convert and Copy a file), Can be used to convert and copy a file
and most of the times is used to copy a iso file (or any other file) to a usb device (or any other
location), thus can be used to make a ‘Bootlable‘ Usb Stick.
Note: In the above example the usb device is supposed to be sdb1 (You should Verify it using
command lsblk, otherwise you will overwrite your disk and OS), use name of disk very
Cautiously!!!.
dd command takes some time ranging from a few seconds to several minutes in execution,
depending on the size and type of file and read and write speed of Usb stick.
5. Command: uname
The “uname” command stands for (Unix Name), print detailed information about the machine
name, Operating System and Kernel.
root@tecmint:~# uname -a
Linux tecmint 3.8.0-19-generic #30-Ubuntu SMP Wed May 1 16:36:13 UTC 2013 i686
i686 i686 GNU/Linux
Note: uname shows type of kernel. uname -a output detailed information. Elaborating the above
output of uname -a.
6. Command: history
The “history” command stands for History (Event) Record, it prints the history of long list of
executed commands in terminal.
root@tecmint:~# history
1 sudo add-apt-repository ppa:tualatrix/ppa
2 sudo apt-get update
3 sudo apt-get install ubuntu-tweak
4 sudo add-apt-repository ppa:diesch/testing
5 sudo apt-get update
6 sudo apt-get install indicator-privacy
7 sudo add-apt-repository ppa:atareao/atareao
8 sudo apt-get update
9 sudo apt-get install my-weather-indicator
10 pwd
11 cd && sudo cp -r unity/6 /usr/share/unity/
12 cd /usr/share/unity/icons/
13 cd /usr/share/unity
Note: Pressing “Ctrl + R” and then search for already executed commands which lets your
command to be completed with auto completion feature.
(reverse-i-search)`if': ifconfig
7. Command: sudo
The “sudo” (super user do) command allows a permitted user to execute a command as the
superuser or another user, as specified by the security policy in the sudoers list.
“To err is human, but to really foul up everything, you need root password.”
8. Command: mkdir
The “mkdir” (Make directory) command create a new directory with name path. However is
the directory already exists, it will return an error message “cannot create folder, folder
already exists”.
Note: Directory can only be created inside the folder, in which the user has write permission.
mkdir: cannot create directory `tecmint‘: File exists
(Don’t confuse with file in the above output, you might remember what i said at the beginning –
In Linux every file, folder, drive, command, scripts are treated as file).
9. Command: touch
The “touch” command stands for (Update the access and modification times of each FILE to the
current time). touch command creates the file, only if it doesn’t exist. If the file already exists it
will update the timestamp and not the contents of the file.
Note: touch can be used to create file under directory, on which the user has write permission,
only if the file don’t exist there.
The Linux “chmod” command stands for (change file mode bits). chmod changes the file mode
(permission) of each given file, folder, script, etc.. according to mode asked for.
There exist 3 types of permission on a file (folder or anything but to keep things simple we will
be using file).
Read (r)=4
Write(w)=2
Execute(x)=1
So if you want to give only read permission on a file it will be assigned a value of ‘4‘, for write
permission only, a value of ‘2‘ and for execute permission only, a value of ‘1‘ is to be given. For
read and write permission 4+2 = ‘6‘ is to be given, ans so on.
Now permission need to be set for 3 kinds of user and usergroup. The first is owner, then
usergroup and finally world.
rwxr-x--x abc.sh
To change its permission and provide read, write and execute permission to owner, group and
world.
read, write and execute to owner and only execute to group and world.
Note: one of the most important command useful for sysadmin and user both. On a multi-user
environment or on a server, this command comes to rescue, setting wrong permission will either
makes a file inaccessible or provide unauthorized access to someone.
The Linux “chown” command stands for (change file owner and group). Every file belongs to
a group of user and a owner. It is used Do ‘ls -l‘ into your directory and you will see something
like this.
root@tecmint:~# ls -l
drwxr-xr-x 3 server root 4096 May 10 11:14 Binary
drwxr-xr-x 2 server server 4096 May 13 09:42 Desktop
Here the directory Binary is owned by user “server” and it belongs to usergroup “root” where
as directory “Desktop” is owned by user “server” and belongs to user group “server“.
This “chown” command is used to change the file ownership and thus is useful in managing and
providing file to authorised user and usergroup only.
Note: “chown” changes the user and group ownership of each given FILE to NEW-OWNER or
to the user and group of an existing reference file.
12. Command: apt
The Debian based “apt” command stands for (Advanced Package Tool). Apt is an advanced
package manager for Debian based system (Ubuntu, Kubuntu, etc.), that automatically and
intelligently search, install, update and resolves dependency of packages on Gnu/Linux
system from command line.
Note: The above commands results into system-wide changes and hence requires root password
(Check ‘#‘ and not ‘$’ as prompt). Apt is considered more advanced and intelligent as compared
to yum command.
As the name suggest, apt-cache search for package containing sub package mpalyer. apt-get
install, update all the packages, that are already installed, to the newest one.
Read more about apt-get and apt-cache commands at 25 APT-GET and APT-CACHE
Commands
Note: A ‘tar.gz‘ means gzipped. ‘tar.bz2‘ is compressed with bzip which uses a better but
slower compression method.
The “cal” (Calendar), it is used to displays calendar of the present month or any other month of
any year that is advancing or passed.
root@tecmint:~# cal
May 2013
Su Mo Tu We Th Fr Sa
1 2 3 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
Show calendar of year 1835 for month February, that already has passed.
Shows calendar of year 2145 for the month of July, that will advancing
Note: You need not to turn the calendar of 50 years back, neither you need to make complex
mathematical calculation to know what day you were worn or your coming birthday will fall on
which day.
15. Command: date
The “date” (Date) command print the current date and time on the standard output, and can
further be set.
root@tecmint:~# date
Fri May 17 14:13:29 IST 2013
root@tecmint:~# date --set='14 may 2013 13:57'
Mon May 13 13:57:00 IST 2013
Note: This Command will be very use-full in scripting, time and date based scripting, to be more
perfect. Moreover changing date and time using terminal will make you feel GEEK!!!.
(Obviously you need to be root to perform this operation, as it is a system wide change).
The “cat” stands for (Concatenation). Concatenate (join) two or more plain file and/or print
contents of a file on standard output.
Note: “>>” and “>” are called append symbol. They are used to append the output to a file and
not on standard output. “>” symbol will delete a file already existed and create a new file hence
for security reason it is advised to use “>>” that will write the output without overwriting or
deleting the file.
Before Proceeding further, I must let you know about wildcards (you would be aware of
wildcard entry, in most of the Television shows) Wildcards are a shell feature that makes the
command line much more powerful than any GUI file managers. You see, if you want to select a
big group of files in a graphical file manager, you usually have to select them with your mouse.
This may seem simple, but in some cases it can be very frustrating.
For example, suppose you have a directory with a huge amount of all kinds of files and
subdirectories, and you decide to move all the HTML files, that have the word “Linux”
somewhere in the middle of their names, from that big directory into another directory. What’s a
simple way to do this? If the directory contains a huge amount of differently named HTML files,
your task is everything but simple!
In the Linux CLI that task is just as simple to perform as moving only one HTML file, and it’s so
easy because of the shell wildcards. These are special characters that allow you to select file
names that match certain patterns of characters. This helps you to select even a big group of files
with typing just a few characters, and in most cases it’s easier than selecting the files with a
mouse.
Here’s a list of the most commonly used wildcards :
Wildcard Matches
* zero or more characters
? exactly one character
[abcde] exactly one character listed
[a-e] exactly one character in the given range
[!abcde] any character that is not listed
[!a-e] any character that is not in the given range
{debian,linux} exactly one entire word in the options given
! is called not symbol, and the reverse of string attached with ‘!’ is true.
Read more examples of Linux “cat command” at 13 Cat Command Examples in Linux
17. Command: cp
The “copy” stands for (Copy), it copies a file from one location to another location.
Note: cp is one of the most commonly used command in shell scripting and it can be used with
wildcard characters (Describe in the above block), for customised and desired file copying.
18. Command: mv
The “mv” command moves a file from one location to another location.
Note: mv command can be used with wildcard characters. mv should be used with caution, as
moving of system/unauthorised file may lead to security as well as breakdown of system.
The command “pwd” (print working directory), prints the current working directory with full
path name from terminal.
root@tecmint:~# pwd
/home/user/Desktop
Note: This command won’t be much frequently used in scripting but it is an absolute life saver
for newbie who gets lost in terminal in their early connection with nux. (Linux is most
commonly referred as nux or nix).
20. Command: cd
Finally, the frequently used “cd” command stands for (change directory), it change the working
directory to execute, copy, move write, read, etc. from terminal itself.
root@tecmint:~# cd /home/user/Desktop
server@localhost:~$ pwd
/home/user/Desktop
Note: cd comes to rescue when switching between directories from terminal. “Cd ~” will change
the working directory to user’s home directory, and is very useful if a user finds himself lost in
terminal. “Cd ..” will change the working directory to parent directory (of current working
directory).
Part 11
What after these initial commands? Obviously we moved to the next part of this article where we
provided commands for computational tasks like:
Search for files in the given directory, hierarchically starting at the parent directory and moving
to sub-directories.
Note: The `-name‘ option makes the search case sensitive. You can use the `-iname‘ option to
find something regardless of case. (* is a wildcard and searches all the file having extension ‘.sh‘
you can use filename or a part of file name to customise the output).
root@tecmint:~# find -iname *.SH ( find -iname *.Sh / find -iname *.sH)
./Desktop/load.sh
./Desktop/test.sh
./Desktop/shutdown.sh
./Binary/firefox/run-mozilla.sh
./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh
./Downloads/kdewebdev-3.5.8/admin/doxygen.sh
./Downloads/kdewebdev-3.5.8/admin/cvs.sh
./Downloads/kdewebdev-3.5.8/admin/ltmain.sh
./Downloads/wheezy-nv-install.sh
root@tecmint:~# find -name *.tar.gz
/var/www/modules/update/tests/aaa_update_test.tar.gz
./var/cache/flashplugin-nonfree/install_flash_player_11_linux.i386.tar.gz
./home/server/Downloads/drupal-7.22.tar.gz
./home/server/Downloads/smtp-7.x-1.0.tar.gz
./home/server/Downloads/noreqnewpass-7.x-1.2.tar.gz
./usr/share/gettext/archive.git.tar.gz
./usr/share/doc/apg/php.tar.gz
./usr/share/doc/festival/examples/speech_pm_1.0.tar.gz
./usr/share/doc/argyll/examples/spyder2.tar.gz
./usr/share/usb_modeswitch/configPack.tar.gz
Note: The above command searches for all the file having extension ‘tar.gz‘ in root directory
and all the sub-directories including mounted devices.
Read more examples of Linux ‘find‘ command at 35 Find Command Examples in Linux
The ‘grep‘ command searches the given file for lines containing a match to the given strings or
words. Search ‘/etc/passwd‘ for ‘tecmint‘ user.
Ignore word case and all other combination with ‘-i‘ option.
Note: You can use these following options along with grep.
The ‘man‘ is the system’s manual pager. Man provides online documentation for all the possible
options with a command and its usages. Almost all the command comes with their corresponding
manual pages. For example,
Manual page for man page itself, similarly ‘man cat‘ (Manual page for cat command) and ‘man
ls‘ (Manual page for command ls).
24. Command: ps
ps (Process) gives the status of running processes with a unique Id called PID.
root@tecmint:~# ps
PID TTY TIME CMD
4170 pts/1 00:00:00 bash
9628 pts/1 00:00:00 ps
To list status of all the processes along with process id and PID, use option ‘-A‘.
root@tecmint:~# ps -A
PID TTY TIME CMD
1 ? 00:00:01 init
2 ? 00:00:00 kthreadd
3 ? 00:00:01 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
7 ? 00:00:00 kworker/u:0H
8 ? 00:00:00 migration/0
9 ? 00:00:00 rcu_bh
....
Note: This command is very useful when you want to know which processes are running or may
need PID sometimes, for process to be killed. You can use it with ‘grep‘ command to find
customised output. For example,
Here ‘ps‘ is pipelined with ‘grep‘ command to find customised and relevant output of our need.
OK, you might have understood what this command is for, from the name of the command. This
command is used to kill process which is not relevant now or is not responding. It is very useful
command, rather a very very useful command. You might be familiar with frequent windows
restarting because of the fact that most of the time a running process can’t be killed, and if killed
it needs windows to get restart so that changes could be taken into effect but in the world of
Linux, there is no such things. Here you can kill a process and start it without restarting the
whole system.
You need a process’s pid (ps) to kill it.
Let suppose you want to kill program ‘apache2‘ that might not be responding. Run ‘ps -A‘ along
with grep command.
Find process ‘apache2‘, note its pid and kill it. For example, in my case ‘apache2‘ pid is
‘1285‘.
Note: Every time you re-run a process or start a system, a new pid is generated for each process
and you can know about the current running processes and its pid using command ‘ps‘.
Note: Kill requires job id / process id for sending signals, where as in pkill, you have an option
of using pattern, specifying process owner, etc.
The ‘whereis‘ command is used to locate the Binary, Sources and Manual Pages of the
command. For example, to locate the Binary, Sources and Manual Pages of the command ‘ls‘
and ‘kill‘.
root@tecmint:~# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
root@tecmint:~# whereis kill
kill: /bin/kill /usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz
Note: This is useful to know where the binaries are installed for manual editing sometimes.
The ‘service‘ command controls the Starting, Stopping or Restarting of a ‘service‘. This
command make it possible to start, restart or stop a service without restarting the system, for
the changes to be taken into effect.
Note: All the process script lies in ‘/etc/init.d‘, and the path might needs to be included on
certain system, i.e., in spite of running “service apache2 start” you would be asked to run
“/etc/init.d/apache2 start”.
alias is a built in shell command that lets you assign name for a long command or frequently
used command.
I uses ‘ls -l‘ command frequently, which includes 5 characters including space. Hence I created
an alias for this to ‘l‘.
root@tecmint:~# l
total 36
drwxr-xr-x 3 tecmint tecmint 4096 May 10 11:14 Binary
drwxr-xr-x 3 tecmint tecmint 4096 May 21 11:21 Desktop
drwxr-xr-x 2 tecmint tecmint 4096 May 21 15:23 Documents
drwxr-xr-x 8 tecmint tecmint 4096 May 20 14:56 Downloads
drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Music
drwxr-xr-x 2 tecmint tecmint 4096 May 20 16:17 Pictures
drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Public
drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Templates
drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Videos
root@tecmint:~# unalias l
check, if ‘l‘ still is alias or not.
root@tecmint:~# l
bash: l: command not found
Making a little fun out of this command. Make alias of certain important command to some other
important command.
Now when your friend types ‘cd‘, just think how funny it would be when he gets directory listing
and not directory changing. And when he tries to be ‘su‘ the all he gets is the location of working
directory. You can remove the alias later using command ‘unalias‘ as explained above.
29. Command: df
Report disk usages of file system. Useful for user as well as System Administrator to keep track
of their disk usages. ‘df‘ works by examining directory entries, which generally are updated only
when a file is closed.
root@tecmint:~# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 47929224 7811908 37675948 18% /
none 4 0 4 0% /sys/fs/cgroup
udev 1005916 4 1005912 1% /dev
tmpfs 202824 816 202008 1% /run
none 5120 0 5120 0% /run/lock
none 1014120 628 1013492 1% /run/shm
none 102400 44 102356 1% /run/user
/dev/sda5 184307 79852 94727 46% /boot
/dev/sda7 95989516 61104 91045676 1% /data
/dev/sda8 91953192 57032 87218528 1% /personal
For more examples of ‘df‘ command, read the article 12 df Command Examples in Linux.
30. Command: du
Estimate file space usage. Output the summary of disk usages by ever file hierarchically, i.e., in
recursive manner.
root@tecmint:~# du
8 ./Daily Pics/wp-polls/images/default_gradient
8 ./Daily Pics/wp-polls/images/default
32 ./Daily Pics/wp-polls/images
8 ./Daily Pics/wp-polls/tinymce/plugins/polls/langs
8 ./Daily Pics/wp-polls/tinymce/plugins/polls/img
28 ./Daily Pics/wp-polls/tinymce/plugins/polls
32 ./Daily Pics/wp-polls/tinymce/plugins
36 ./Daily Pics/wp-polls/tinymce
580 ./Daily Pics/wp-polls
1456 ./Daily Pics
36 ./Plugins/wordpress-author-box
16180 ./Plugins
12 ./May Articles 2013/Xtreme Download Manager
4632 ./May Articles 2013/XCache
Note: ‘df‘ only reports usage statistics on file systems, while ‘du‘, on the other hand, measures
directory contents. For more ‘du‘ command examples and usage, read 10 du (Disk Usage)
Commands.
31. Command: rm
The command ‘rm‘ stands for remove. rm is used to remove files (s) and directories.
Removing a directory
root@tecmint:~# rm PassportApplicationForm_Main_English_V1.0
rm: cannot remove `PassportApplicationForm_Main_English_V1.0': Is a directory
The directory can’t be removed simply by ‘rm‘ command, you have to use ‘-rf‘ switch along
with ‘rm‘.
Warning: “rm -rf” command is a destructive command if accidently you make it to the wrong
directory. Once you ‘rm -rf‘ a directory all the files and the directory itself is lost forever, all of
a sudden. Use it with caution.
echo as the name suggest echoes a text on the standard output. It has nothing to do with shell,
nor does shell reads the output of echo command. However in an interactive script, echo passes
the message to the user through terminal. It is one of the command that is commonly used in
scripting, interactive scripting.
#!/bin/bash
echo "Please enter your name:"
read name
echo "Welcome to Linux $name"
Next, set execute permission and run the script.
Note: ‘#!/bin/bash‘ tells the shell that it is an script an it is always a good idea to include it at the
top of script. ‘read‘ reads the given input.
This is an important command that is useful for changing own password in terminal. Obviously
you need to know your current passowrd for Security reason.
root@tecmint:~# passwd
Changing password for tecmint.
(current) UNIX password: ********
Enter new UNIX password: ********
Retype new UNIX password: ********
Password unchanged [Here was passowrd remians unchanged, i.e., new
password=old password]
Enter new UNIX password: #####
Retype new UNIX password:#####
Note: The ‘lpq‘ command lets you view the status of a printer (whether it’s up or not), and the
jobs (files) waiting to be printed.
compare two files of any type and writes the results to the standard output. By default, ‘cmp‘
Returns 0 if the files are the same; if they differ, the byte and line number at which the first
difference occurred is reported.
file1.txt
root@tecmint:~# cat file1.txt
Hi My name is Tecmint
file2.txt
root@tecmint:~# cat file2.txt
Hi My name is tecmint [dot] com
Now, let’s compare two files and see output of the command.
Wget is a free utility for non-interactive (i.e., can work in background) download of files from
the Web. It supports HTTP, HTTPS, FTP protocols and HTTP proxies.
Mount is an important command which is used to mount a filesystem that don’t mount itself.
You need root permission to mount a device.
First run ‘lsblk‘ after plugging-in your filesystem and identify your device and note down you
device assigned name.
root@tecmint:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 923.6G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 7.9G 0 part [SWAP]
sr0 11:0 1 1024M 0 rom
sdb 8:16 1 3.7G 0 disk
└─sdb1 8:17 1 3.7G 0 part
From this screen it was clear that I plugged in a 4 GB pendrive thus ‘sdb1‘ is my filesystem to
be mounted. Become a root to perform this operation and change to /dev directory where all the
file system is mounted.
root@tecmint:~# su
Password:
root@tecmint:~# cd /dev
Now you can navigate to /dev/usb from terminal or X-windows system and acess file from the
mounted directory.
gcc is the in-built compiler for ‘c‘ language in Linux Environment. A simple c program, save it
on ur desktop as Hello.c (remember ‘.c‘ extension is must).
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
Compile it
root@tecmint:~# gcc Hello.c
Run it
root@tecmint:~# ./a.out
Hello world
Note: On compiling a c program the output is automatically generated to a new file “a.out” and
everytime you compile a c program same file “a.out” gets modified. Hence it is a good advice to
define a output file during compile and thus there is no risk of overwrite to output file.
Here ‘-o‘ sends the output to ‘Hello‘ file and not ‘a.out‘. Run it again.
root@tecmint:~# ./Hello
Hello world
g++ is the in-built compiler for ‘C++‘ , the first object oriented programming language. A simple
c++ program, save it on ur desktop as Add.cpp (remember ‘.cpp‘ extension is must).
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter first number:\n";
cin >> a;
cout <<"Enter the second number:\n";
cin>> b;
cin.ignore();
int result = a + b;
cout<<"Result is"<<" "<<result<<endl;
cin.get();
return 0;
}
Compile it
root@tecmint:~# g++ Add.cpp
Run it
root@tecmint:~# ./a.out
Enter first number:
...
...
Note: On compiling a c++ program the output is automatically generated to a new file “a.out”
and everytime you compile a c++ program same file “a.out” gets modified. Hence it is a good
advice to define a output file during compile and thus there is no risk of overwrite to output file.
Java is one of the world’s highly used programming language and is considered fast, secure, and
reliable. Most of the the web based service of today runs on java.
Create a simple java program by pasting the below test to a file, named tecmint.java (remember
‘.java‘ extension is must).
class tecmint {
public static void main(String[] arguments) {
System.out.println("Tecmint ");
}
}
Run it
root@tecmint:~# java tecmint
Note: Almost every distribution comes packed with gcc compiler, major number of distros have
inbuilt g++ and java compiler, while some may not have. You can apt or yum the required
package.
Part III
In our Third and last article of this series, we tried to cover the commands that would be
required for the computational task like:
ifconfig is used to configure the kernel-resident network interfaces. It is used at boot time to set
up interfaces as necessary. After that, it is usually only needed when debugging or when system
tuning is needed.
Display details of All interfaces including disabled interfaces using “-a” argument.
Disable an Interface
[avishek@tecmint ~]$ ifconfig eth0 down
Enable an Interface
[avishek@tecmint ~]$ ifconfig eth0 up
Note: If using a wireless network you need to use command “iwconfig“. For more “ifconfig”
command examples and usage, read 15 Useful “ifconfig” Commands.
netstat command displays various network related information such as network connections,
routing tables, interface statistics, masquerade connections, multicast memberships etc..,
OK! For some reason if you want not to resolve host, port and user name as a output of netstat.
Fine, you may need to get the output of netstat continuously till interrupt instruction is passed
(ctrl+c).
For more “netstat” command examples and usage, see the article 20 Netstat Command
Examples.
A network utility program used to obtain information about Internet servers. As its name
suggests, the utility finds name server information for domains by querying DNS.
dig is a tool for querying DNS nameservers for information about host addresses, mail
exchanges, nameservers, and related information. This tool can be used from any Linux (Unix)
or Macintosh OS X operating system. The most typical use of dig is to simply query a single
host.
You have just connected to your Linux Server Machine and founds Something unusual or
malicious, what you will do? Guessing…. NO, definitely not you could run uptime to verify
what happened actually when the server was unattended.
one of the most important command for administrator, wall sends a message to everybody
logged in with their mesg permission set to “yes“. The message can be given as an argument to
wall, or it can be sent to wall’s standard input.
[avishek@tecmint ~]$ wall "we will be going down for maintenance for one hour
sharply at 03:30 pm"
Broadcast message from [email protected] (pts/0) (Sat Jun 29 14:44:02
2013):
we will be going down for maintenance for one hour sharply at 03:30 pm
Lets you control if people can use the “write” command, to send text to you over the screen.
mesg [n|y]
n - prevents the message from others popping up on the screen.
y – Allows messages to appear on your screen.
Let you send text directly to the screen of another Linux machine if ‘mesg’ is ‘y’.
An enhancement to write command, talk command lets you talk to the logged in users.
Note: If talk command is not installed, you can always apt or yum the required packages.
50. Command: w
what command ‘w’ seems you funny? But actually it is not. t’s a command, even if it’s just one
letter long! The command “w” is a combination of uptime and who commands given one
immediately after the other, in that order.
[avishek@tecmint ~]$ w
15:05:42 up 4:49, 3 users, load average: 0.02, 0.01, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
server tty7 :0 14:06 4:43m 1:42 0.08s pam: gdm-
passwo
server pts/0 :0.0 14:18 0.00s 0.23s 1.65s gnome-terminal
server pts/1 :0.0 14:47 4:43 0.01s 0.01s bash
As the name suggests, this command rename files. rename will rename the specified files by
replacing the first occurrence from the file name.
rename a1 a0 a?
rename a1 a0 a??
Displays the processes of CPU. This command refresh automatically, by default and continues to
show CPU processes unless interrupt-instruction is given.
This command create a new ext4 file system on the specified device, if wrong device is followed
after this command, the whole block will be wiped and formatted, hence it is suggested not to
run this command unless and until you understand what you are doing.
vi (visual), emacs, nano are some of the most commonly used editors in Linux. They are used
oftenly to edit text, configuration,… files. A quick guide to work around vi and nano is, emacs is
a.
vi-editor
[avishek@tecmint ~]$ touch a.txt (creates a text file a.txt)
[avishek@tecmint ~]$ vi a.txt (open a.txt with vi editor)
[press ‘i’ to enter insert mode, or you won’t be able to type-in anything]
1. alt+x (exit insert mode, remember to keep some space between the last letter.
2. ctrl+x command or your last word will be deleted).
3. :wq! (saves the file, with the current text, remember ‘!’ is to override).
nano editor
[avishek@tecmint ~]$ nano a.txt (open a.txt file to be edited with nano)
edit, with the content, required
Click ‘y’ to yes and enter file name, and you are done.
Rsync copies files and has a -P switch for a progress bar. So if you have rsync installed, you
could use a simple alias.
Now try to copy a large file in terminal and see the output with remaining items, similar to a
progress bar.
Moreover, Keeping and Maintaining backup is one of the most important and boring work a
system administrator, needs to perform. Rsync is a very nice tool (there exists, several other) to
create and maintain backup, in terminal.
Keeping track of memory and resources is as much important, as any other task performed by an
administrator, and ‘free‘ command comes to rescue here.
Ok till now you would have understood what this command actually stands for, from the name of
this command.mysqldump commands dumps (backups) all or a particular database data into a
given a file.For example,
Note: mysqldump requires mysql to be running and correct password for authorisation. We have
covered some useful “mysqldump” commands at Database Backup with mysqldump Command
Merge two or more text files on lines using. Example. If the content of file1 was:
1
2
3
and file2 was:
a
b
c
d
the resulting file3 would be:
1 a
2 b
3 c
d
60.Command: lsof
lsof stands for “list open files” and displays all the files that your system has currently opened.
It’s very useful to figure out which processes uses a certain file, or to display all the files for a
single process. Some useful 10 lsof Command examples, you might be interested in reading.
In the below example, it will show long listing of open files some of them are extracted for better
understanding which displays the columns like Command, PID, USER, FD, TYPE etc.
# lsof
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 253,0 4096 2 /
init 1 root rtd DIR 253,0 4096 2 /
init 1 root txt REG 253,0 145180 147164
/sbin/init
init 1 root mem REG 253,0 1889704 190149
/lib/libc-2.12.so
init 1 root 0u CHR 1,3 0t0 3764
/dev/null
init 1 root 1u CHR 1,3 0t0 3764
/dev/null
init 1 root 2u CHR 1,3 0t0 3764
/dev/null
init 1 root 3r FIFO 0,8 0t0 8449 pipe
init 1 root 4w FIFO 0,8 0t0 8449 pipe
init 1 root 5r DIR 0,10 0 1 inotify
init 1 root 6r DIR 0,10 0 1 inotify
init 1 root 7u unix 0xc1513880 0t0 8450 socket
Sections and it’s values are self-explanatory. However, we’ll review FD & TYPE columns more
precisely.
FD – stands for File descriptor and may seen some of the values as:
Also in FD column numbers like 1u is actual file descriptor and followed by u,r,w of it’s mode
as:
1. DIR – Directory
2. REG – Regular file
3. CHR – Character special file.
4. FIFO – First In First Out
The below command will display the list of all opened files of user tecmint.
# lsof -u tecmint
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1838 tecmint cwd DIR 253,0 4096 2 /
sshd 1838 tecmint rtd DIR 253,0 4096 2 /
sshd 1838 tecmint txt REG 253,0 532336 188129 /usr/sbin/sshd
sshd 1838 tecmint mem REG 253,0 19784 190237 /lib/libdl-2.12.so
sshd 1838 tecmint mem REG 253,0 122436 190247
/lib/libselinux.so.1
sshd 1838 tecmint mem REG 253,0 255968 190256
/lib/libgssapi_krb5.so.2.2
sshd 1838 tecmint mem REG 253,0 874580 190255
/lib/libkrb5.so.3.3
3. Find Processes running on Specific Port
To find out all the running process of specific port, just use the following command with option -
i. The below example will list all running process of port 22.
# lsof -i TCP:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1471 root 3u IPv4 12683 0t0 TCP *:ssh (LISTEN)
sshd 1471 root 4u IPv6 12685 0t0 TCP *:ssh (LISTEN)
In below example shows only IPv4 and IPv6 network files open with separate commands.
# lsof -i 4
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc
rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954
rpcbind 1203 rpc 8u IPv4 11331 0t0 TCP *:sunrpc (LISTEN)
avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns
avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600
# lsof -i 6
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 1203 rpc 9u IPv6 11333 0t0 UDP *:sunrpc
rpcbind 1203 rpc 10u IPv6 11335 0t0 UDP *:954
rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN)
rpc.statd 1277 rpcuser 10u IPv6 11858 0t0 UDP *:55800
rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN)
cupsd 1346 root 6u IPv6 12112 0t0 TCP localhost:ipp (LISTEN)
To list all the running process of open files of TCP Port ranges from 1-1024.
# lsof -i TCP:1-1024
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN)
cupsd 1346 root 7u IPv4 12113 0t0 TCP localhost:ipp (LISTEN)
sshd 1471 root 4u IPv6 12685 0t0 TCP *:ssh (LISTEN)
master 1551 root 13u IPv6 12898 0t0 TCP localhost:smtp (LISTEN)
sshd 1834 root 3r IPv4 15101 0t0 TCP 192.168.0.2:ssh-
>192.168.0.1:conclave-cpp (ESTABLISHED)
sshd 1838 tecmint 3u IPv4 15101 0t0 TCP 192.168.0.2:ssh-
>192.168.0.1:conclave-cpp (ESTABLISHED)
sshd 1871 root 3r IPv4 15842 0t0 TCP 192.168.0.2:ssh-
>192.168.0.1:groove (ESTABLISHED)
httpd 1918 root 5u IPv6 15991 0t0 TCP *:http (LISTEN)
httpd 1918 root 7u IPv6 15995 0t0 TCP *:https (LISTEN)
Here, we have excluded root user. You can exclude a particular user using ‘^’ with command as
shown above.
# lsof -i -u^root
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc
rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954
rpcbind 1203 rpc 8u IPv4 11331 0t0 TCP *:sunrpc (LISTEN)
rpcbind 1203 rpc 9u IPv6 11333 0t0 UDP *:sunrpc
rpcbind 1203 rpc 10u IPv6 11335 0t0 UDP *:954
rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN)
avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns
avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600
rpc.statd 1277 rpcuser 5r IPv4 11836 0t0 UDP *:soap-beep
rpc.statd 1277 rpcuser 8u IPv4 11850 0t0 UDP *:55146
rpc.statd 1277 rpcuser 9u IPv4 11854 0t0 TCP *:32981 (LISTEN)
rpc.statd 1277 rpcuser 10u IPv6 11858 0t0 UDP *:55800
rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN)
Below example shows user tecmint is using command like ping and /etc directory .
# lsof -i -u tecmint
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 1839 tecmint cwd DIR 253,0 12288 15 /etc
ping 2525 tecmint cwd DIR 253,0 12288 15 /etc
The following command with option ‘-i’ shows the list of all network connections ‘LISTENING
& ESTABLISHED’.
# lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc
rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954
rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN)
avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns
avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600
rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN)
cupsd 1346 root 6u IPv6 12112 0t0 TCP localhost:ipp (LISTEN)
cupsd 1346 root 7u IPv4 12113 0t0 TCP localhost:ipp (LISTEN)
sshd 1471 root 3u IPv4 12683 0t0 TCP *:ssh (LISTEN)
master 1551 root 12u IPv4 12896 0t0 TCP localhost:smtp
(LISTEN)
master 1551 root 13u IPv6 12898 0t0 TCP localhost:smtp
(LISTEN)
sshd 1834 root 3r IPv4 15101 0t0 TCP 192.168.0.2:ssh-
>192.168.0.1:conclave-cpp (ESTABLISHED)
httpd 1918 root 5u IPv6 15991 0t0 TCP *:http (LISTEN)
httpd 1918 root 7u IPv6 15995 0t0 TCP *:https (LISTEN)
clock-app 2362 narad 21u IPv4 22591 0t0 TCP 192.168.0.2:45284-
>www.gov.com:http (CLOSE_WAIT)
chrome 2377 narad 61u IPv4 25862 0t0 TCP 192.168.0.2:33358-
>maa03s04-in-f3.1e100.net:http (ESTABLISHED)
chrome 2377 narad 80u IPv4 25866 0t0 TCP 192.168.0.2:36405-
>bom03s01-in-f15.1e100.net:http (ESTABLISHED)
9. Search by PID
# lsof -p 1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 253,0 4096 2 /
init 1 root rtd DIR 253,0 4096 2 /
init 1 root txt REG 253,0 145180 147164 /sbin/init
init 1 root mem REG 253,0 1889704 190149 /lib/libc-2.12.so
init 1 root mem REG 253,0 142472 189970 /lib/ld-2.12.so
Sometimes you may have to kill all the processes for a specific user. Below command will kills
all the processes of tecmint user.