Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Linux Commands

Unix is a multitasking, multi-user computer operating system originally developed in


1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis
Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna. The Unix operating system
was first developed in assembly language, but by 1973 had been almost entirely recoded
in C, greatly facilitating its further development and porting to other hardware. Unix is a
proprietary software.

A Unix-like operating system is one that behaves in a manner similar to a Unix system.

Linux is a Unix-like computer operating system assembled under the model of free and
open source software development and distribution. The defining component of any
Linux system is the Linux kernel, an operating system kernel first released October 5,
1991 by Linus Torvalds. Linux runs on a wide variety of computer hardware, including
mobile phones, tablet computers, network routers, televisions, video game consoles,
desktop computers, mainframes and supercomputers. Linux is a leading server operating
system and runs the 10 fastest supercomputers in the world. In addition, more than 90%
of today's supercomputers run some variant of Linux.

The development of Linux is one of the most prominent examples of free and open
source software collaboration: the underlying source code may be used, modified, and
distributed—commercially or non-commercially—by anyone under licenses such as the
GNU General Public License. Typically Linux is packaged in a format known as a Linux
distribution for desktop and server use. Some popular mainstream Linux distributions in-
clude Debian (and its derivatives such as Ubuntu), Fedora and openSUSE. Linux distri-
butions include the Linux kernel, supporting utilities and libraries and usually a large
amount of application software to fulfill the distribution's intended use.

Shell is a command-line interpreter.

Bash (Bourne Again Shell), Csh (C shell), Ksh (Korn Shell)are widely used shells.
Contents:
a) Help Commands (man, info, whatis, whereis, which, apropos)
b) Genaral Commands (echo, who, whoami, su, clear, date, cat, pwd)

a) Help Commands

a. i) Command Name : man

Description : format and display the manual pages

Syntax : man commandName / functionName

Example : man scanf

(displays the man page of “scanf” function in C)

a. ii) Command Name : info

Description : read info documents

Syntax : info (or) info commandName / functionName

Example : info scanf

a. iii) Command Name : whatis

Description : search the whatis database for complete words

Syntax : whatis commandName / functionName

Example : whatis scanf

(searches a set of database files containing short descriptions of system

commands for keywords and displays the result on the standard output.)
( scanf - input format conversion )

a. iv) Command Name : whereis

Description : locate the binary, source and manual page files for

a command / function

Syntax : whereis commandName

Example : whereis fopen

(fopen: /usr/share/man/man3/fopen.3.gz /usr/share/man/man3p/fopen.3p.gz )

a. v) Command Name : which

Description : shows the full path of commands (OS commands)

Syntax : which commandName1 [commandName2] ….

Example : which man

( /usr/bin/man )

a. vi) Command Name : apropos

Description : search the whatis database for strings

Syntax : apropos commandName / functionName

Example : apropos fscanf

(searches a set of database files containing short descriptions of system

commands for keywords and displays the result on the standard output.)

b) General Commands

b. i) Command Name : echo

Description : display a line of text

Syntax : echo STRING

Example : echo Hello VITIANS


echo –e “Hello \n VITIANS”

( display Hello in first line and VITIANS in second line )

b. ii) Command Name : who

Description : show who is logged on

Syntax : who

Example : who

bcs6094 pts/37 Sep 17 12:38 (192.168.1.2)

bcs6059 pts/13 Sep 17 12:38 (192.168.1.35)

bcs6030 pts/55 Sep 17 12:39 (192.168.1.14)

bcs6068 pts/22 Sep 17 12:39 (192.168.1.4)

bcs6069 pts/15 Sep 17 12:39 (192.168.1.58)

bcs6054 pts/11 Sep 17 12:39 (192.168.1.61)

bcs6093 pts/63 Sep 17 12:39 (192.168.1.34)

who am i

bcs6093 pts/63 Sep 17 12:39 (192.168.1.34)

b. iii) Command Name : whoami

Description : Print the user name associated with the current

effective user id

Syntax : whoami

Example : whoami

b. iv) Command Name : su

Description : run a shell with substitute user and group IDs

Syntax : su

Example : su (or) su userName


(new shell is created if the user is authenticated)

b. v) Command Name : clear

Description : clear the terminal screen

Syntax : clear

Example : clear

b. vi) Command Name : date

Description : print or set the system date and time

Syntax : date

Example : date

b. vii) Command Name : cat

Description : concatenate files and print on the standard output

Syntax : cat fileName1 [ fileName2 …]

Example : 1) cat file1.cpp ( displays file.cpp on stdout)

2) cat > file2.cpp ( create a file)

(type characters and press ctrl-D in a new line)

3) cat >> file2.cpp

(append to the file file2.cpp )

Command Name : copy

cp file1 file2 # copy file1 to file2


cp file1 directory # copy file1 into directory

Command Name : compare (Compare tow files and dispalys the line and
byte difference

cmp file1 file2


cmp rrr sss
byte1, lin1
b. viii) Command Name : pwd

Description : print name of current working directory

Syntax : pwd

Example : pwd

( /home/bce7001 )

Other commands

i) w – show who is logged in and what they are doing

e.g w

ii) finger - displays information about user

e.g finger

iii) grep – print line matching a pattern

e.g grep main file.c

(prints all lines in the file containing main)

iv) wc - print the number of newlines, words, and bytes in files

e.g wc file1.c

4 5 45 file1.c

e.g wc –l file1.c

4 file1.c

e.g wc –w file1.c

5 file1.c

e.g wc –c file1.c

46 file1.c

v) who | grep bce6001

bce6001 pts/22 Sep 17 12:39 (192.168.1.4)


Description: who is executed first and the result of who is piped to grep

grep prints all those lines containing “bce6001”

vi) cal - displays a calendar

e.g cal 1947 (displays the calendar of the year 1947)

vii) List of “vi” Commands

The default editor that comes with the Linux operating system is called vi (visual editor).
[Alternate editors for Linux environments include pico and emacs.]

The UNIX vi editor is a full screen editor and has two modes of operation:

1. Command mode -commands which cause action to be taken on the file, and

2. Insert mode - in which entered text is inserted into the file.

In the command mode, every character typed is a command that does something to the text file
being edited; a character typed in the command mode may even cause the vi editor to enter the
insert mode. In the insert mode, every character typed is added to the text in the file; pressing the
<Esc> (Escape) key turns off the Insert mode.

Note: Linux is case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the
results will not be what you expect.

The vi editor is a common editor which makes use of a regular keyboard with an escape key.
Complete documentation is available by typing

man vi

at the linux prompt.

Starting an Editing Session:


vi filename

where filename is the name of the file to be edited or created.

e.g vi first.c

Save & exit vi:

:wq Writes changes to current file and quits vi.

To compile:

gcc first.c

(or)

g++ first.cpp

To Execute:

./a.out

Text Insertion Commands

a Appends text after cursor.

A Appends text at the end of the line.

i Inserts text before cursor.

I Inserts text at the beginning of the line.

o Opens new line below the current line for text insertion.

O Opens new line above the current line for text insertion.
DEL Overwrites last character during text insertion.

ESC Stops text insertion.

Text Deletion Commands


x Deletes current character.

dd Deletes current line.

dw Deletes the current word.

D, d$ Deletes from cursor to end of line.

P Puts back text from the previous delete.

Exiting vi
:wq Writes changes to current file and quits vi.

:q! quit vi even though latest changes have not been saved for this vi call

Undo Command
u undo the last command.

Text Searching Commands

/pattern Moves cursor forward to next occurrence of pattern.

?pattern Moves cursor backward to next occurrence of pattern.

n Repeats last / or ? pattern search.

Try other commands

diff - Displays the different diff file1 file2


contents into two files diff rrr sss
>perundurai
>erode
wc – Count the number of wc filename
lines, words and characters in wc sss
file 3323

ls file # does the file exist?


ls -l file # show information about the file
ls *.txt # show all files ending in .txt
ls -lt # show information about all files in date order
ls -lrt # above reversed in order
ls - Show lists of files or ls -a # show all files including hidden files
information on the files ls dir # show contents of directory
ls -d dir # does the directory exist?
ls -p # adds meaning characters to ends of filenames
ls -R # show files also in subdirectories of directory
ls -1 # show one file per line

esc : w - To write a file in insert mode.


esc:wq - To write and quit from the file.
esc:q! - To quit the file without saving content

You might also like