3.Advance Linux Commands

Component of Operating Systems

What is an Operating System? (OS)

A program that acts as an intermediary between an user of a computer and the computer hardware.

Composed of KERNEL and SHELL.

KERNEL: Heart of OS.


Operating system goals:

•Execute user programs and make solving user problems easier.

•Make the computer system convenient to use.

•Use the computer hardware in an efficient manner.

•It manages Processes, Main Memory, Input-Output Devices and Files.


  • Linux is a modern, free operating system based on UNIX standards

  • First developed as a small but self-contained kernel in 1991 by Linus Torvalds, with the major design goal of UNIX compatibility.

  • It has been designed to run efficiently and reliably on common PC hardware, but also runs on a variety of other platforms.

  • LINUX Distributions in our Labs: UBUNTU and CENTOS

The Hierarchical File system


telnet or putty in Labs.

You can also use MOBAXTERM to access your LINUX account in server.

LOGIN: 2021A7PS0001U

Password: *****


The $ prompt appears and you have entered Bourne Shell.


Logout from the System:


You can also press CTRL-d to logout


Terminate / Abort a Job

You can press DEL key or CTRL-C to abort / cancel / terminate any job currently running.


You can use Lab Servers / ONLINE LINUX TERMINAL You can practice on Commands and Editor.



Choose the option: RUN TERMINAL NOW


1.ls – lists the contents of directory

Syntax: ls [-alR] [path]

Here the optional items or entries are given within square brackets. You can use the options if you wish.

$ is basically the default prompt that shows automatically. You need not type $. Type just the command with or without options.

a – will list entries beginning with a dot. i.e. .

l – long format (shows file permissions, owner, group, file size, date and time of last access, file name all are shown)

R – Recursively prints sub-directories (folders) and files.


$ls $ls –l $ls –al

$ls –alR $ls /bin $ls –al /bin | more

ls command continued:

$ls –al > myfile

$more < myfile (shows one page at a time, you can press space to see next page.)

$more myfile (same as $more < myfile)

Remember: | means PIPELINE. We can combine several commands in a pipeline. This is called concurrent processing.

> means output redirection

< means input Redirection

2. cat – type file contents or concatenate (combine) files.

Syntax: cat file1 [file2] [file] [file3] …………

Practice: $cat myfile

$cat myfile myfile myfile myfile > comfile

$more < comfile (press space to see next page)

$more comfile (output same as previous)

3. mv - Rename a file. (change the name of an existing file)

Syntax: mv <oldfilename> <newfilename>


$mv myfile shivaji

$ls (check whether the filename myfile is changed to shivaji)

4. pwd – display present working directory



5. mkdir: make (create) a new directory (folder).

Syntax: mkdir <directoryname>

Practice: $mkdir fort $ls -l (see if fort is created)

$ls –al fort (it will show empty directory)

If you see output, . Indicates current directory and

.. indicates the parent directory.

$mkdir uptron $ls –al (see if directory uptron is created)

6. cd: change directory (go to a specific directory)

Syntax: cd <pathname>

Practice: $cd fort $pwd

$cd (this will take you back to your home/login directory)

$cd fort $cd .. $pwd

7. cp: copies file(s) from source to target

Syntax: cp <source> <target>

If the wild card * is used, it means all.

Practice: $pwd $cd

$cp sivaji dilip

$ls –al (check if dilip is created)

$cp sivaji /home/user/ganesan (check if Ganesan is created)

$cp /home/user/sivaji /home/user/fort/sivajifort

$ls –lR | more (check if file sivajifort is created in the subdirectory fort)

$cp /home/user/fort/* /home/user/uptron

(copies all files from folder fort to folder uptron)

8,9. rm and rmdir: rm removes file(s). rmdir removes a directory, only iff the directory is empty. Before using rmdir command check if the directory which you want to remove is empty.

Syntax: rm [-ir] filename

i: interactive mode (asks your permission before removing)

r: delete files as well as whole directory recursively. Be Careful.

Syntax: rmdir <directoryname>

10. cal: prints the calender

Syntax: cal [month] [year]


$cal $cal 10 1963 $cal 2020

$cal 2020 > mycalender

$more < mycalender $cat mycalender | more

11. clear: clears the screen

Syntax: clear

Practice: $clear

$CTRL-d (logout) or you can close the browser.

A very Good Beginner’s Book for reading and practice.

SUMITABHA DAS, “Unix Concepts and Applications”, Tata Mcgraw-Hill Pub., 2006 or any Recent Edition.

Files: contain information (program, data, text, document)

Filename: You can give a meaningful name to your file.

Example: sample.c shivaji.cpp mgr.java

Text Editors: Process textual information.

•To create files for storing information

•Look at the contents of an existing file

•Modify the information stored in a file.

Functioning of a Text Editor

1)Copies the whole file from disk into main memory (buffer area)

2)Changes are made only in the buffer.

3)You can either save the changes or discard them.

Vi is a display-oriented, screen editor. It has two modes of operation.

1.Command Mode

•Whenever you enter vi, you are in this mode.

•Almost all commands are entered from this mode.

•To enter input mode, use the command i or a. (i.e. insert or append)

2. Input Mode

•Here you can insert or append text.

•To return to command mode press <ESC> key. Quite Often, you may forget pressing <ESC> key. Do not hesitate. You can press <ESC> key judiciously. It will not do any harm.

Entering vi editor (To create a new file / modify an existing file)

Syntax: vi <filename>

Example: $vi myfirstfile

Commands to add new text:

i insert text (at cursor position)

a append text (after cursor position)

After calling one of these commands, you can freely type text. Press <ESC> to return to command mode.

The above step is important. If you forget to press escape, you will still remain in input mode. You will be wondering! “why I am not able to give commands? Why I am still inputting text? I want to come out?”

Lesson: You can use the escape key judiciously so that you return back to command mode.

Commands for Exiting the vi editor

ZZ (in caps) save changes and exit editor.

:wq! <enter> save changes and exit editor.

:w! <enter> save changes and resume editing.

:q! <enter> quit editor without saving changes.


k - up j - down h – left l – right


CTRL f - page forward

CTRL b - page backward

Moving around the screen

H - top line on the screen

M - middle line on the screen

L - bottom line on the screen

:se nu <enter> : display line numbers

:se nonu <enter> : do not display line numbers

i : inserts text before cursor.

Example: This is that

This is to certify that

a : appends text after cursor.

Example: This is to certify that

This to certify that selva is a bonafide student of this institute.

Similarly, Try the commands O and o.

(basically, these commands open a new line above / below the current line respectively. Give a try.)

Moving within a LINE

w – next word b – previous word

0 – beginning of a line $ - end of a line

Try these commands after you type the following text:

vi is a display oriented editor. I am fine.

GOTO a Particular Line using Line Number

Syntax: linenumberG

Examples: 10G - goto line number 10

:10 <enter> - goto line number 10

1G - goto lie number 1

$G - goto the last line

SEARCHING: syntax: /string

Example: /sample

The above command displays the first occurrence of the string “sample”

n takes you to the next occurrence

?<enter> takes you back to the previous occurrence

:/sample/g this will display all occurrences of the string “sample”


x - deletes a single character at cursor position

Example: This Tis

r - correct a single character at cursor position

Example: Them Then

cw - change word

Example: That was a good play

That was a bad play. Press <ESC> after correction.

cc - change entire line.

Example: This is a pen.

Unix is a multi-user, multi-tasking, time-sharing OS.

Hit <ESC> key after correction.

Making Corrections: How to delete a word / line / a range of lines.

dw - delete word

Example: This is is a small tree

This is a small tree

dd - delete a single line

Example 123 123

456 789


Deleting a range of lines

syntax:- :<startlineno>,<endlineno>d <enter>

Example: To delete lines from 7 to 11, give this command- :7,11d <enter>

u - undo previous command

SUBSTITUTION (REPLACE) of one string with another string

Syntax: <startlineno>,<endlineno>s/source/target/


:1,$s/there/there was not/ <enter>

The above command will do the replacement from first line to last line. i.e. entire file. But it will replace only the first occurrence of the word within a line.

Try these commands:



The option g replaces all occurrences within a line for the word “good” with “bad”.

The option c will ask you to confirm before replacing the string. (interactive mode).

COPY and PASTE for text: For this we use single letter commands Y and P or p. i.e. YANK and PUT.

Y Yank (copy) lines to buffer. Y can be preceded by the number of lines. i.e. How many lines you want to copy?

P Put before cursor

p Put after cursor

Example: 1 AA

2 BB

3 CC

4 DD

To copy Lines 2 to 4 before line 1, give these commands:

1)Go to Line 2. (you can use arrow key or command 2G)

2) 3Y (you are marking 3 lines to copy)

3)Go to Line 1 (you wanted to copy before line number 1)

4)P (paste before line 1)


Writing a block of Lines from current file into another file

Syntax:L <startlineno>,<endlineno>w <newfilename> <enter>

Assume you are currently editing myfirstfile

Let the new file name be mysecondfile

example:- :7,11w mysecondfile <enter>

Verify if the new file has got 5 copied lines.

Reading and copying the contents of another existing file into the current file

Assume you are currently editing myfirstfile

Let there be another existing file called mysecondfile


:r mysecondfile <enter>

The above command reads and copies “mysecondfile” contents into vi buffer after current line, in current file.

:7r mysecondfile <enter>

The above command reads and copies after line number 7 in current file.

:$r mysecondfile <enter>

The above command reads and copies after last line in current file.

/* sample.c */

/* program to find sum of a series of numbers */

#include <stdio.h>

void main()


int startno=2, endno=100, step=2, i;

int sum = 0;

for ( i = startno; i <= endno; i += step)

sum = sum + i;

printf( "startno = %d\n", startno);

printf( "endno = %d\n", endno);

printf( "step = %d\n", step);

printf("sum = %d\n", sum);


$ vi sample.c

$ cc sample.c

$ ./a.out

Note: we are giving ./ before running any program?

Do you know why?

DOT (.) stands for current directory. Your program is executed from current directory.

Alternately, you can set the PATH variable in your .bash_profile file in your login directory so that the path is automatically set for your current directory. In that case You can directly type the command a.out (without a preceding dot).

i.e. $ a.out

Occasionally, you may get message: file is read only. You are unable to edit file or compile a program, since the file may be corrupted). Now, You can use Recovery mode in editor.

$vi -r <existing_filename>

Then write the contents to another file :

:1,$w <newfilename> enter

:q! enter

$vi <newfilename>

and then verify if all lines are present in the new file.

There are 3 categories of users.

OWNER: You are the owner of your User Account.

GROUP: A group of similar users. Say, all Computer Science Students

OTHERS: Those who do not belong to your group. Say, all Mechanical Engineering Students

For each of these 3 divisions, there are 3 sorts of permissions:

read write execute

While displaying the contents of the directory in the long format using the command:

$ls –al you can see the following output

drwxr-xr-x 7 user user 29 Jul 14 02:25 .

drwxr-xr-x 1 root root 4096 Jul 12 06:36 ..

--rw------- 1 user user 796 Jul 11 20:26 .bash_history

lrwxrwxrwx 1 user user 18 Jul 11 06:01 .bash_profile

-rw-r--r-- 1 user user 350 Jul 13 02:50 sample.c

You can take help to know the syntax for any command.

The utilities to provide help are man and info

man: manual pages for any command

info: provide help information for any command

You can use any one of the above utilities.

Syntax: man <command> info <command>

Examples: $man ls

$info ls

Press q to quit. Press space to see next page.

You can set access permissions for your files and directories for the different types of users.

Syntax: chmod [ugo+-rwx] {file(s)}

chmod [ugo+-rwx] {directory}

chmod octal_number {file | directory}

u – owner g – group o – others

+: permit -:deny

r – read w – write x – execute


$chmod go-rwx sample.c

$ls -al

$chmod go-rwx *

$ls -al

$chmod 750 * $ls –al

(750 in base 8 is 111 101 000 in base 2 which is same as

rwx r-x --- )

Command: file

This command determines file type.

It will determine whether the file is ASCII text, Executable file or C Program text, or Python Program Text etc.

Syntax: file <filename>

Examples: $file sample.c

$file a.out

Command: $date $man date

It displays today’s date as well as current time.

Setting date can be done by system administrator only using the date command with parameters.

wc: Word count program. Counts the number of words, lines and characters in a file.

Syntax: wc [-lwc] file

l – number of lines, w – number of words, c – number of characters

Examples: $ wc sample.c $ wc –l sample.c

df: number of free blocks in your hard disk. It will show free space for each file system separately.

Example: $df

du: disk usage. It will show how much space is used by each file and directory.

Example: $du | more

bc: Arbitrary Precison Arithmatic Language

It performs simple calculations .

It can convert a number in one number system to another.

(say decimal to binary, binary to decimal, binary to octal, octal to binary, octal to hexadecimal, decimal to hexadecimal, base 9 to base 7, any input base to any output base)



10+17 ibase=A

27 obase=16

10*17.50 237

175 ED (i.e (14*16 power 1)+(13*16 power 0))

ibase=A ibase=A

obase=2 obase=8

4 99

100 143

sleep: introduce delay for finite time (in seconds)

Example: $sleep 10

Comparing two files: cmp, diff

cmp: compares two files. Reports either identical or shows the first mismatch.

diff: This command is useful when we want to compare two versions of the same file.

Syntax: cmp <file1> <file2> diff <file1> <file2>

Examples: $ cp sample.c sand.c

$ cmp sample.c sand.c

$ diff sample.c sand.c

locate, find

locate: This command finds a specific file by name. It will show where it is stored. locate uses a previously built database (command updatedb ). Is much faster, but uses an 'older' database and searches only names

Syntax: locate <filename>

Example: $ locate sample.c

find: This command will search for files in the directory hierarchy. find searches in the real system. Is slower, but always up-to-date and has more options.

Syntax: find path -name -list [expression]

Example: $ find /home/user -name sample.c -print

split: split a file into pieces

Syntax: split [-n] inputfile [prefix_for_outputfiles]

n – number of lines in each output file.

Output files will have the prefix followed by aa,ab,ac,…..az, then ba,bb,…….,bz and so on.

Example: $ split -2 sample.c sam


-rw-r--r-- 1 user user 64 Jul 15 02:04 samaa

-rw-r--r-- 1 user user 20 Jul 15 02:04 samab

-rw-r--r-- 1 user user 15 Jul 15 02:04 samac

-rw-r--r-- 1 user user 58 Jul 15 02:04 samad


head, tail

head: displays first few lines of a file.

tail: displays last few lines of a file.

Syntax: head -n <filename> tail -n <filename>

n: number of lines that you are interested to view.

Example: $ head -3 sample.c $ tail -4 sample.c