Unix Exercises

You might also like

You are on page 1of 12

1) Entering Commands

2) Files and Directories commands


3) File Search and Permission commands
4) Unix Shell Script Programs
5) Pattern Matching and file compression commands
6) Counting Lines, words and File size
7) Working with columns or fields ,sort and uniq commands
8) File Comparision , File Editing and Formatting commands
9) Date , bc and dc commands
10) Awk and sed commands
Aim : To Illustrate Unix Entering Commands

Entering Commands

COMMAND DESCRIPTION
Cal shows a calendar of the current month

Who provides login details of all the current Unix Users

Finger provides complete information about other users on the


Unix system.
passwd to change password

Write to send short message to other unix user

Talk an enhanced communication program

Mesg allows to accept or refuse messages sent via write and


talk
whoami provides current user name

Man to view manual page of a command

Who Finger Write Talk


Gives Basic Gives detailed Messages will get Talk splits the screen
Information information about overlapped with each other into upper and lower
about current current unix users havles
unix users
Aim : To Illustrate Unix Files and Directory Commands

Files and Directories commands

COMMAND DESCRIPTION

ls : lists the contents of the current directory


ls –f : lists the files with marks, which indicate the type of the file
ls –r : list all files recursively, descending down the directory tree from the given path.
ls ls –a : list all files including hidden files
command ls –l : list the files in long format
ls –s : list the files along with their size.
with its ls –t : sort the list by time of modification, with the newest at the top.
option ls –s : sort the list by size, with the largest at the top.
ls –r : reverse the sorting order.

cat cat > filename : To create a file


cat filename : To view the contents of the file
cat >>filename : To append the contents of the file

mkdir To Create a Directory

rmdir To remove an Empty Directory

cd To Move Around Directories


cd ~ : To Move to the Home Directory
cd .. : To come out of one directory
cd dir1 : To move the directory called dir1
mv To Move the file or directory from one location to another Location

cp To Copy a File or directory from one location to another location

rm To Remove an File or Directory


rm filename : To remove an ordinary file
rm –r directory_name : To Remove a directory with contents
pwd To view the present working directory
Aim : To Illustrate Unix File Searching Command and Permission commands

File Search and Permission commands

COMMAND DESCRIPTION

Find command is used to search and locate the list of files and directories
find based on conditions
To set or alter the file permissions of a particular file or files
chmod ugo+/- rwx filename : Relative Permission Setting
chmod 777 filename : Absolute Permission Setting

number permission
chmod
4 Read

2 write

1 execute

controls how file permissions are set for newly created files

If umask User Group Others


value set to permission permission permission
umask 000 All all all
007 All all none
027 All read / execute none
Aim : To write and execute basic shell script programs

Procedure

Step 1: Type vi filename in the $ prompt to go to vi editor

Step 2: Type the shell script program

Step 3: save the file by typing ESC :wq!

Step 4:set execute permission to the file by typing chmod +x filename in $ prompt

Step 5: execute the shell script program by typing ./ filename


Aim : To Perform Pattern Matching and File compression

Pattern Matching and file compression commands

COMMAND DESCRIPTION
Pattern Matching Commands
Lets to search for a target which may be one or more words or patterns
containing wild cards or regular expressions
grep
$ grep unix notes // This command searches the pattern called unix in the file
notes
Egrep  Egrep or grep -E is another version of grep or the Extended grep.
 This version of grep is efficient and fast when it comes to searching for a
regular expression pattern as it treats meta-characters as is and doesn’t
substitute them as strings like in grep, and hence you are freed from the
burden of escaping them as in grep.
 In case of egrep, even if you do not escape the meta-characters, it would treat
them as special characters and substitute them for their special meaning
instead of treating them as part of string.

$ egrep '(f|g)ile' check_file

Fgrep  Fgrep or the Fixed grep or grep -F is yet another version of grep which
is fast in searching when it comes to search for the entire string instead
of regular expression as it doesn’t recognize the regular expressions,
neither any meta-characters.
 For searching any direct string, this is the version of grep which should
be selected.
 Fgrep searches for complete string and doesn’t even recognize special
characters as part of regular expression even if escaped or not escaped.

$ fgrep -C 0 '(f|g)ile' check_file

$ fgrep -C 0 '\(f\|g\)ile' check_file

 Like, when meta-characters were not escaped, fgrep searched for the
complete string “(f|g)ile” in the file, and when the meta-characters were
escaped, then the fgrep command searched for “\(f\|g\)ile” all
characters as is in the file

File Compression Commands


pack Replaces a file with a compressed version. The original file is destroyed. Unpack
command to uncompress the file
compress Works much the same way as pack. Unlike pack, compress does not report after
compressing or uncompressing. uncompress is the command to uncompress the
file
gzip Does the same as pack and compress. Gunzip is the command to uncompress the
file that is compressed with gzip
tar To create a archive file. Tar command is used to “package” a group of files into a
single file
Aim : To demonstrate unix commands for counting words ,lines and file size

Counting Lines, words and File size

COMMAND DESCRIPTION
wc Prints the number of bytes, lines or words in a file

wc –c Displays size of the file in bytes

wc –w Displays number of words in a file

wc -l Displays number of lines in a file

wc –L Display the length of the longest line in the file

nl is used to number each line in a file


Aim : To demonstrate unix commands for working with columns and fields

Working with columns or fields ,sort and uniq commands

COMMAND DESCRIPTION
cut To extract specific fields and columns
cut –f : is used to extract specific field of a field structured file
cut –c : is used to extract specific columns of a field structures file
cut –d delimiter –f : to display specific fields with specified delimiter
colrm Deletes one or more columns from a file or set of files

Paste glues together columns or fields from existing file


join Merges information from one or more files
join –j : to specify the join field
sort Order or reorder the lines of a file
sort -f : ignore uppercase and lowercase
sort –n: sort by numeric value, in ascending order
sort –r: reversed order of output
sort –u : eliminates duplicate lines in output
sort –o: sends output to a file
sort –k : sorts based on specific field
uniq Filters or removes repeated lines from files
Aim : To Illustrate File Comparision commands in Unix

File Comparision , File Editing and Formatting commands

COMMAND DESCRIPTION
cmp Tells whether two files differ , and if they do, it reports the position in
the file where the first difference occurs
Comm. To compare two sorted files and sho lines that are the same or different.
Comm. Prints its output in 3 columns
 Lines unique to the first file
 Lines unique to the seconf file
 Lines found in both
diff Compares two files , line by line, and print out differences

Patch To Apply the difference to the original file

pr Used to add a header to every page of a file

fmt Used to control the width of the output

tr Replaces one set of characters with another set

spell Allows to check the spelling in a file


Aim : To demonstrate Unix bc and dc commands

Date , bc and dc commands

COMMAND DESCRIPTION
date Prints the date in a default format
Date –d : allows to specify a particular time or date to display
bc and dc Used as both calculator and mini-language for writing mathematical
programs
Aim : To illustrate unix awk and sed commands

Awk and sed commands

COMMAND DESCRIPTION
Awk Test for the pattern in every line of input, and to perform the
corresponding action whenever the pattern matches the input line
sed Takes a set of patterns and simple editing commands and applies them
to an input stream

You might also like