Linux Lesson III (SA I)

Page 1

SA1 LECTURE 3 LINUX BASIC COMMANDS: mkdir:

Create directories and directory structure.

Usage:

mkdir [-option] [PATH]/NEW_DIR_NAME [PATH]/NEW_DIR_NAME

mkdirtest mkdir/test/abc /test/doc mkdir –p /test/a/b/c

To create test directory in current directory. To create abc and doc directories in /test directory. To create /test/a/b/c directory structure.

touch:

creates empty files and set files’ creation and access time.

Usage:

touch [-option] [PATH] FILE_NAME

touch f1 To create f1 file in current directory touch –t 08111900 f1 To change creation date and time of file f1 to 11 August 07:00pm touch –a 08111900 f1 To change access time and date of file f1 Here: 08: 8th month = August 11: Date 19: Hour 00: Minutes -a: To change access time of a file. -t: To change creation time of a file. cp:

Copies files, directories and directory structure from one place to an other

Usage:

cp [-option] [PATH]/SOURCE [PATH]/DESTINATION

cp /test/f1 ~

To copy a file f1 present in /test directory to home directory of the user executing this command. cp –r /test /root To copy /test directory cp –f /test /root To cop /test directory forcefully in /root directory. cp –p /test /root To preserve file permission and ownership during copy. Otherwise they are lost

Here: -r: copy directories as well (recursively) -f: copy force fully, do not prompt for overwriting if file / directory with same name is present at destination. -p: Preserve file permission and owner ship etc. ~ means home directory of the current user.

mv:

Renames and Moves files and directories.

Usage:

mv [-option] [PATH]/SOURCE [PATH]/DESTINATION

mv /test/f1 ~ To move a file f1 present in /test directory to home directory of the user mv –f /test/abc /root To move /test/abc file forcefully in /root directory.


mv /test/f1 /test/f2 mv * /root

To rename a file /test/f1 to /test/f2 To move all files/directories present in current directory to /root

rm:

To remove files and directories.

Usage:

rm [-option] [PATH]/FILE_DIRECTORY_TO_REMOVE

rm /test/f1 rm -r /test rm -rf /test

To remove a file f1 present in /test directory. To remove /test directory. To remove forcefully /test directory.

cat:

To display contents of a file on monitor.

Usage:

cat [-option] [PATH]/FILE_NAME

cat /test/f1 To display contents of file /test/f1 on screen. cat –n /test/f1 To display contents of file /test/f1 with numbered output on screen. To find a certain word/string in a file. Grep displays complete line that contains the specified word/string.

grep:

Usage:

grep [-option] [PATH]/FILE_NAME

grep “test” /test/f1 grep –A2 “test” /test/f1 grep –B2 “test” /test/f1

To find word “test” in /test/f1 file. To find word “test” in /test/f1 file. Also display two lines after the line in which word “test” is found. To find word “test” in /test/f1 file. Also display two lines before the line in which word “test” is found.

Operators/Input Output Redirectors & Pipe: By default standard input device in Linux/Unix is keyboard and default standard output device is monitor. To change these defaults to some other device or file we have to use input output redirectors. There are two operators. 1. > : 2. < :

Output operator/redirector Input operator/redirector

Output Redirector (>): Most commonly used operator. By default most of the commands displays their output on screen we can change this default and save output of commands in files by changing output device from monitor to a file or we can print output of commands by redirecting their output to parallel port which is attached to a printer.

ls > list ls > /dev/lp0

This will save output of ls command in a newly created file list in current directory. If list file were present in the current directory then this file would overwrite the existing file. This command will send output of ls command to first parallel port if a printer is attached with that port then printer will print the output of ls command.

df –h > /home/test/freespace This command will save output of df command in a file /home/test/freespace.


Input Redirector (<): This operator is rarely used in real life. It changes the default input device from keyboard to a file. Means a command that takes input by our typing (keyboard) will take that input from a file

read a (PRESS ENTER) abc Read command takes input from keyboard and stores it in a variable. When we execute read command we provide variable name after it (a) and press enter now read command waits for your keystrokes and what ever you type saves it in the specified variable. Like here we saved “abc” in variable “a”.

read a < /etc/fstab Here we have changed the default input device and we are giving input to read command from a file “/etc/fstab”. Now variable “a” will contain contents of /etc/fstab file as its value.

Pipe:

Pipes makes output of a command, input of another command. Picture 1:

As explained in “Picture 1” due to “|” sign the output of “ls /etc” command was saved in RAM in a temporary file temporarily. “wc” command counts lines, words etc. in a file and we have to give file name after “wc” command and here “|” provided this temp file to “wc –l” command. And “wc –l” command counted lines in that temp file and displayed output on screen.

File and Directory Compression: There are many utilities available for compression in Linux but we will study three most commonly used utilities. 1. gzip & gunzip 2. bzip2 & bunzip2 3. zip & unzip Note: A file or directory zipped with one utility cannot be unzipped with other utility. We have to use unzip command of the same utility to unzip a file or directory.

gzip:

gzip only zips files. gzip adds “.gz” extension after zipped file name. It works on original file and after zip process original file is not present.

Usage:

gzip [-option] [PATH]/FILE_NAME

gzip /test/f1 gzip -l /test/f1.gz gzip -rv /test

To zip a single file “/test/f1”. To display how much % file was zipped actual size and size after zip. To zip all files in /test directory separately and recursively (-r) and also display verbose (-v) information.


gunzip -l /test/f1.gz gunzip -r /test

To unzip /test/f1.gz file. To unzip all zipped files in /test directory.

bzip2:

bzip2 only zips files. bzip2 adds “.bz2” extension after zipped file name. It works on original file and after zip process original file is not present.

Usage:

bzip2 [-option] [PATH]/FILE_NAME

bzip2 /test/f1 bzip2 /test/f1.bz2

To zip a single file “/test/f1”. To unzip a single file “/test/f1.bz2”.

zip:

zip compresses directories and adds “.zip” extension after zipped file name.

Usage:

zip [-option] Destination_File.zip Files_And_Directories_to_Zip

zip -r test.zip /test zip abc.zip f1 f2 f3 zip –r abc.zip f1 f2 f3 d1 zip -u abc.zip f4 unzip abc.zip

To zip “/test” directory and save it with test.zip name. –r is used to zip directories. To zip f1, f2 and f3 files in a zip file abc.zip. To zip f1, f2 and f3 files and d1 directory in a zip file abc.zip. To zip add a file “f4” in an existing zip file abc.zip. To unzip abc.zip file.

Backup: There are many utilities available for backup purpose in Linux. We will use tar utility. Tar adds files and directories in a single file called tar ball. Tar can be used to take backup on hard drive or tape device.

Usage:

tar [option] [Destination_File.tar] Files_And_Directories_to_tar

There are five main modes of tar and that are controlled by options. Means options tells tar command that what you want to do. Tar adds files end to end and do not zips files. Create Backup:

c option tells tar utility that you want to take backup.

tar cvf test.tar /test

To take backup of /test directory in test.tar file.

Here:

V: F:

To display verbose information Tells tar that you do not want to take backup in tape drive. You want to save backup in a file on hard drive.

Note: In tar command we do not need to add – sign before option.

tar cv /etc

To take backup of /etc directory in tape device. Tape device is the default output device of tar utility if you want to take backup on tape device then do not specify destination file and do not specify “f” option.

Extract Backup:

x option tells tar utility that you want to extract backup.

tar xvf test.tar tar xv

To extract backup from test.tar backup file in current directory. To extract backup from tape device in current directory.


List Backup File:

t option tells tar utility that you want to take list of files present in backup.

tar tf test.tar tar t

To take list of files present in test.tar backup file. To take list of files present in backup on tape device.

Update Backup:

u option tells tar utility that you want to add some more files and directories in backup.

tar uf test.tar abc.txt tar u abc.txt

To add “abc.txt” file in existing tar backup file test.tar. To add “abc.txt” file in existing tar backup on tape device.

Zip Backup File:

z or j option tells tar utility that you want to zip backup file as well.

tar cvzf test.tar.gz /test tar cvjf test.tar.bz2 /test

To take file with To take file with

backup of /test directory in test.tar file and also zip tar gzip utility. backup of /test directory in test.tar file and also zip tar bzip2 utility.

tar xvzf test.tar.gz

To unzip “test.tar.gz” backup file first then extract backup. Tar was zipped with “gzip” utility

tar xvjf test.tar.bz2

To unzip “test.tar.bz2” backup file first then extract backup. Tar was zipped with “bzip2” utility

vi Editor: vi is a standard text mode editor present in almost all Unix flavors. In vi editor you can perform your tasks easily and fast. Vi editor is very use full for System Admins and Programers.

Working With Vi Editor: There are three working modes of vi editor. 1. Command Mode (default Mode) 2. Insert Mode 3. Last Line Mode OR Colon Mode Navigation Between vi Modes: When we open vi editor we are in COMMAND MODE by default and we can enter in INSERT MODE by typing “i” or “a”. We cannot move from INSERT MODE to COLON MODE we have to go in COMMAND MODE first then COLON MODE. Same we have to do if we want to go in INSERT MODE from COLON MODE, we have to go in COMMAND MODE first then we can enter in INSERT MODE. As explained in “Picture 2” Picture 2:


Command Mode (default Mode): When we start vi editor we are in command mode by default In command mode we can perform following tasks: Note: To Perform all tasks given bellow in COMMAND MODE only. And options are case sensitive.

Copy Lines: Suppose you want to copy four lines from 8 to 12 then take your cursor to the top most line from where you want to copy lines (line number 8) then Press number of lines you want to copy (4) then press “yy”. This action will copy 4 lines in memory.

Cut Lines: Suppose you want to copy ten lines from 22 to 31 then take your cursor to the top most line from where you want to copy lines (line number 22) then Press number of lines you want to copy (10) then press “dd”. This action will cut 10 lines in memory.

Paste Lines: Suppose you want to past copied lines bellow line number 106 then take your cursor to line number 106 and press “p”. This will paste copied lines bellow line number 106

Undo Last Actions: Press “u”. If you press “u” single time it will undo last action and pressing it more will undo other actions.

Delete Lines: Suppose you want to copy ten lines from 22 to 31 then take your cursor to the top most line from where you want to copy lines (line number 22) then Press number of lines you want to copy (10) then press “dd”. This action will cut 10 lines in memory. Then do not paste these lines and lines will be deleted.

Display number of lines and your current position in file: Press “Ctrl + g” keys

Search for a particular word: Press “/” and then type string you want to search in the file and press enter. You should double quote “ “ string if it contains special characters or spaces. To move to next incidence of the same word press “n”.

And many more

Insert Mode: In insert mode we can type our text. Last Line Mode OR Colon Mode: In colon mode we can perform following tasks: Note: To Perform all tasks given bellow in COLON MODE only. And options are case sensitive.

Save file and quit In Colon Mode after : press “wq” or “x”

Quit without save In Colon Mode after : press “q!”


Give name to file or save file with another name In Colon Mode after “:” press “wq new_name_of_file”

Add contents of another file Suppose you want to add contents of “/root/list” file between line number 10 and 11 then take your cursor to line number 10 and go in COLON MODE and after “:” type full path and file name of the file whose content you want to add in your file. Like “:/root/list”

Move to a specified line In Colon Mode after “:” press the number of the line you want to take your cursor. Suppose you want to go in line number 1109 then in colon mode press “:1109” and press enter. If you want to move to last line of the file then press “$” after “:”. Like “:$”

Invoking vi Editor: vi new_file_name vi existing file name vi

To create a new file To open existing file To open a blank file with no name.

Note: Vi creates .filename.swp file to keep backup of your actions if vi process is terminated abnormally then you can use the .filename.swp file to recover some of the actions you made but did not save with the following command. Vi –r .filename.swp


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.