Operating System (2140702)

BE | Semester-4   Winter-2018 | 10-12-2018

Q5) (b)

Explain Unix Commands – grep, sort, cat, chmod

Unix Command: grep

It selects and prints the lines from a file which matches a given string or pattern.

Syntax: grep [options] pattern [file]

Description

  • This command searches the specified input fully for a match with the supplied pattern and displays it.
  • While forming the patterns to be searched we can use shell match characters, or regular expressions.

Example: $ grep "Error" logfile.txt
This searches for the string "Error" in the log file and prints all the lines that has the word "Error".


Unix Command: Sort

It is used to sort the lines in a text file.

Syntax: sort [FILE]...

Example:
$ cat > data.txt
apples
oranges
pears
kiwis
bananas

$ sort data.txt apples
bananas
kiwis
oranges
pears

Note that this command does not actually change the input file, data.txt.


Unix Command: Cat

It is used to create, display and concatenate file contents.

Syntax: cat [options] [FILE]...

Example: $ cat file1.c
Above syntax will display the content of file1.c
Example: $ cat > file1.c
Above syntax creates file1.c and allow us to insert content for this file.

  • After inserting content you can use ctrl+d to exit the file.
  • If file with same name exist then it will overwrite that file.
Example: $ cat file1.c >> file2.c
It can concatenate the contents of two files. For this you have to use append output redirection operator.
The contents of file2.cwill be appended to file1.c.


Unix Command: chmod

chmod command allows you to alter / Change access rights to files and directories.

Syntax: chmod [options] [MODE] FileName

Description
chmod command is used to set the permissions of one or more files for all three categories of users (user,group and others ). It can be run only by the user and super user. Command can be used in two ways. Let’s first take a look at the abbreviations used by chmod command.