Basic Commands For Os

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

WEEK-3
PERFORMANCE CRITERIA
FILE AND DIRECTORY COMMANDS

I]

1. mkdir command
This command is used to create or make new directory.

Syntax $ mkdir [options] directoryname

SN Example Meaning
1 $ mkdir cse creates a new directory called “cse”
2 $ mkdir cse ce me creates 3 new directory namely “cse”, “ce”, “me”
3 $ mkdir – v cse creates a new directory called “cse” in verbose mode(v) it prints
confirmation message directory is created

2. rmdir command
This command is used to remove empty directories present in your system.

Syntax $ rmdir [options] directoryname

SN Example Meaning
1 $ mkdir cse removes the directory called “cse”
3 $ mkdir – v cse removes the directory called “cse” in verbose mode

3. touch command
This command is used to create file.
Syntax $ touch test.txt

Dept. of CSE, GPT-KGF 2021-2022 1


20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

4. Redirect operator >


This is also used to create empty file.
Syntax $ > test2.txt

5. cat command
The cat command is short for concatenate. It can be used to output the contents of
several files, one file, or even part of a file. If the file doesn’t exist, the Linux cat command will
create it.
Syntax $ > cat test3.txt

6. rm command
This command is used to delete or remove files.
Syntax $ > rm [options] filename / directoryname

S Example Meaning
N
1 $ rm f1.c removes the file “f1.c”
2 $ rm -i f1.c prompt before deleting the file “f1.c”
3 $ rm - d cse removes the directory “cse”
4 $ rm *.c removes all the c files

7. mv command
This command is used to move the files and directories.
Syntax $ mv [options] source destination

SN Example Meaning
1 $ mv f1.c gptkgf Moves f1.c to the directory gptkgf
SN Example with Meaning
options
1 $ mv - i f1.c gptkgf Same as above but there is interactive prompt before moving
mv:move ‘gptkgf/f1.c’ ? y

Dept. of CSE, GPT-KGF 2021-2022 2


20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

8. cp command
This command is used to copy the contents of files and directories to other files and
directories.
Syntax $ cp [options] source destination

SN Example Meaning
1 $ cp f1.c f2.c Copies the contents of f1.c to f2.c
SN Example with Meaning
options
1 $ cp - a f1.c gptkgf Copies the contents of f1.c to the directory gptkgf
2 $ cp - R f1.c gptkgf Copies the contents of f1.c to the directory gptkgf
3 $ cp - i f1.c gptkgf Same as above but there is interactive prompt before copying
cp:overwrite ‘gptkgf/f1.c’ ? y

9. Pipes

 Named pipe: named pipe is also called as FIFO special file. An unnamed pipe is only
used for communication between a child and its parent process.

 $ mkfifo mypipe
 $ ls -l mypipe
 prw-r-----. 1 shs staff 0 Jan 31 13:59 mypipe

$ echo "Can you read this?" > mypipe

$ cat mypipe

Can you read this?

Once read, the contents of the pipe are gone.

 Unnamed pipe: named pipe can be used for communication between two unnamed
process.

 How to create an unnamed pipe?

Call the system call:

int pipe (int fd[2]);

Dept. of CSE, GPT-KGF 2021-2022 3


20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

The input parameter is an array of two file descriptors fd[0] and fd[1]. The system
call returns a –1 in case of a failure. If the call is successful, it will return two
integer values which are file descriptors fd[0] & fd[1]. In half-duplex pipes, fd[0]
and fd[1] are used for reading and writing, respectively.

II]

1. ls command
This command is used to list the files and directories present in the system.
Syntax $ ls [options] filename/dirname

SN Example Output
1 $ ls Desktop Downloads....
Documents......
SN Example with options Meaning
1 $ ls -r Lists all files & directories in reverse order (z-a)
2 $ ls -ls Lists all files in long format along with file size
3 $ ls -t Lists all files sorted by time and date
4 $ ls -a Lists all files & also hidden files starting with . (dot)
5 $ ls -l Lists all files & directories in a long list format

2. comm command
This command is used to compare two files line by line.
Syntax $ comm [options] filen1 filen2

Dept. of CSE, GPT-KGF 2021-2022 4


20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

3. cmp command
This command is used to compare two files byte by byte. It displays the byte and line
number where first difference is found. If no difference is found no output will be displayed.
Syntax $ cmp [options] file1 file2

SN Example Meaning
1 $ cmp f1.txt f2.txt Compares the files f1.txt and f2.txt
Output:- differ: byte 15, line 3

4. file manipulating commands


Some of the file manipulating commands are
1. mkdir : used to create directories
2. rmdir : used to remove directories
3. cp : used to copy files and directories
4. mv : used to move or rename files and directories

We have already discussed the above commands, please refer

5. chmod command

 This command is used for changing the file permissions.
 chmod stands for “change mode”
 We can change permission for all 3 categories of user (user, group, others)
 The command can be specified in two ways

a) Relative permissions
When using this method, chmod changes the permissions specified in the
command and leaves the other permission unchanged.

Syntax $ chmod [options] permissions filename

SN Example Output
1 $ chmod u + x f.txt Adds execute permission on file f.txt for the user
2 $ chmod o + x f.txt Adds execute permission on file f.txt for the others
Dept. of CSE, GPT-KGF 2021-2022 5
20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

3 $ chmod o - x f.txt Removes execute permission on file f.txt for the others
4 $ chmod a + x f.txt Adds execute permission on file f.txt for the user,
group and others (all)
5 $ chmod o + x f.txt f1.txt Adds execute permission on file f.txt and f1.txt for the
others

b) Absolute permissions
In this method chmod uses 3-digit code.
Digits assigned for read = 4
write = 2
execute = 1
SN Example Output
1 $ chmod 777 f.txt Adds read, write and execute permission on file f.txt for all
2 $ chmod 766 f.txt User = r w x
Group = r w
Others = r w

6. File compression and decompression


gzip & zip: this command is used to compress the given file.
Syntax Description Example
gzip {filename} Gzip compress the size gzip mydata.doc
of the given files gzip *.jpg
ls -l
zip {.zip-filename} {filename-to- zip is used to compress zip mydata.zip mydata.doc
compress} the given file

gzip -d & unzip: this command with option d is used to decompress the given file.
Syntax Description Example
gzip -d {.gz file} Decompressed a file that is created gzip -d mydata.doc.gz
using gzip command. File is
restored to their original form
using this command.
unzip {.zip file} Extract the compressed files unzip file.zip

Dept. of CSE, GPT-KGF 2021-2022 6


20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

II]

1. Text processing commands


Commands affecting text and text files are called as text processing commands.
i. sort command
This command sorts the content of a file, line by line.
Syntax $ chmod [options] permissions filename

sort.txt (use tab after each word)

01 joe
02 marie
03 albert
04 dave

SN Example Output
1 $ sort sort.txt sorts the content of the file sort.txt
01 joe
02 marie
03 albert
04 dave

2 $ sort - r sort.txt sorts the content of the file sort.txt in reverse


order
04 dave

03 albert
02 marie
01 joe
3 $ paste - k 2 sorts the content of the file sort.txt based on the
sort.txt key (k) 2 represents the second coloumn
03 albert
04 dave
01 joe
02 marie

ii. uniq command


Dept. of CSE, GPT-KGF 2021-2022 7
20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

This command reports or filters the repeated lines in a file.


Syntax $ uniq [options] filename

u.txt
this is a line
this is a line
this is a line
this is also a line

SN Example Output
1 $ uniq u.txt displays only single copy of each line present in u.txt
this is a line
this is also a line
this is also also a line
2 $ uniq - c u.txt displays only single copy of each line present in u.txt along
with their count
3 this is a line
2 this is also a line
1 this is also also a line
3 $ uniq - d u.txt displays only the lines at are duplicate in u.txt
this is a line
this is also a line

iii. cut command

This command removes or cuts the section of each line of a specified file.

Syntax $ cut [options] filename

cut.txt (use tab after each word)

one two three four five six

apple grapes mango peach berries kiwi

SN Example Output
1 $ cut - f 4 cut.txt four
peach
2 $ cut - f 2-3 cut.txt two three
Dept. of CSE, GPT-KGF 2021-2022 8
20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

grapes mango
3 $ cut - f 1, 4, 6 cut.txt one four six
apple peach kiwi
In the above examples f stands for field

iv. paste command

Syntax $ paste [options] file1 file2

SN Example Output
1 $ paste a.txt b.txt displays the content of a.txt and b.txt in parallel
here tab is used as a delimiter
2 $ paste - d ‘:’ a.txt b.txt displays the content of a.txt and b.txt in parallel
here : is used as a delimiter
3 $ paste - s a.txt b.txt displays the content of a.txt and b.txt one after
the another (in serial)
v. head command

This command prints the FIRST 10 lines of the specified file

Syntax $ head [options] filename

SN Example Output
1 $ head f1.txt prints the first 10 lines of the file f1.txt
2 $ head - 4 f1.txt prints only the first 4 lines of the file f1.txt
3 $ head - c 4 f1.txt prints only the first 4 characters of the file f1.txt
vi. tail command

This command prints the LAST 10 lines of the specified file

Syntax $ tail [options] filename

SN Example Output
1 $ tail f1.txt prints the last 10 lines of the file f1.txt
2 $ tail - 4 f1.txt prints only the last 4 lines of the file f1.txt
3 $ tail - c 4 f1.txt prints only the last 4 characters of the file f1.txt

vii. tr command

This command translates one set of character to another.

Syntax $ tr [options] filename

Dept. of CSE, GPT-KGF 2021-2022 9


20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

t.txt

hello

SN Example Output
1 $ tr “[:lower:]” “[:upper:]” < t.txt HELLO

viii. pr command

This command is used to format the file before printing it.

Syntax $ pr [options] filename

SN Example Output
1 $ pr - h “” u.txt prints the file with file name (u.txt) as the page header
2 $ pr - m u.txt prints the file in parallel (merge)

ix. grep command



 grep stands for global regular expression print
 This command processes or analyse the given file line by line and prints any lines that
matches the specified pattern.

Syntax $ grep [options] pattern filename

 Regular Expression: They provide the way for matching patterns in a given input.

SN Regex(Reg-regular, ex-expression) Meaning


Patterns
1 . (dot) Matches any character expect \n
2 * (asterisk) Matches 0 or more occurrences of regular
expression
3 + (plus) Matches 1 or more occurrences of regular
expression
4 ? (question mark) Matches 0 or 1 occurrences of regular
expression
5 ^ (caret) Matches the beginning of a line
6 \ (backslash) Any special character should be preceded by \

Dept. of CSE, GPT-KGF 2021-2022 10


20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

reg.txt

hello this is gpt (kgf)


good morning my friends
This is abc from 4th sem cse
this is my linuxlab
bye take care this
is abc logging off see you.

SN Example Meaning
1 $ grep “linuxlab” reg.txt displays/matches any line that contains the word
“linuxlab”
this is my linuxlab
2 $ grep “this.” reg.txt displays any line that contains the word “this” and
“this” should not be present at end of the line.
hello this is gpt (kgf)
this is my linuxlab
3 $ grep “this*” reg.txt displays any line that contains the word “this”
hello this is gpt (kgf)
this is my linuxlab
bye take care this
4 $ grep “[^t]his” reg.txt displays any line that contains the spelling “his”
and beginning character of “his” should not be “t”
This is abc from 4th sem cse

Dept. of CSE, GPT-KGF 2021-2022 11


20CS42P- OS & ADMINISTRATION Prepared by: NANDINI V, L/CSE GPT-KGF

WEEK-3 ACTIVITY

COMPARE EXT2, EXT3 & EXT4 FILESYSTEM ATTRIBUTES

EXT-2 EXT-3 EXT-4

Ext2 stands for second Ext3 stands for third Ext4 stands for fourth
extended file system. extended file system. extended file system.

It was introduced in 1993. It was introduced in 2001. It was introduced in 2008.


Developed by Rémy Card. Developed by Stephen
Tweedie.

This was developed to Starting from Linux Kernel Starting from Linux Kernel
overcome the limitation of 2.4.15 ext3 was available. 2.6.19 ext4 was available.
the original ext file system.

Ext2 does not have The main benefit of ext3 is Supports huge individual file
journaling feature. that it allows journaling. size and overall file system
size.
Maximum individual file size Maximum individual file size Maximum individual file
can be from 16 GB to 2 TB can be from 16 GB to 2 TB size can be from 16 GB to 16
TB
Overall ext2 file system size Overall ext3 file system size Overall maximum ext4 file
can be from 2 TB to 32 TB can be from 2 TB to 32 TB system size is 1 EB
(exabyte). 1 EB = 1024 PB
(petabyte). 1 PB = 1024 TB
(terabyte).

Dept. of CSE, GPT-KGF 2021-2022 12

You might also like