UNIT 9(Commands Set2)
UNIT 9(Commands Set2)
121
wc Command Example
122
wc Command Example
wc -l Prints the number of lines in a file
wc -w Prints the number of words in a file
wc -c Displays the count of bytes in a file
123
ln Command
• ln creates links between files.
• ln creates hard links by default, or symbolic links if the -s (--
symbolic) option is specified. When creating hard links, each
TARGET must exist.
▪ Syntax :
ln [OPTION]… [-T] TARGET LINK_NAME
Optio Use
n ln -f If the destination file or files already exist, overwrite them
124
ln Command Example
125
ln Command Example
ln -s Make symbolic links instead of hard links
f2.txt f6.txt
(hello good morning) (hello good morning)
126
nl Command
• nl command numbers the lines in a file.
▪ Syntax :
nl [OPTION]... [FILE]...
▪ Example :
Optio Use
n nl -i Line number increment at each line
127
nl Command Example
128
nl Command Example
nl -i Line number increment at each line
129
nl Command Example
nl -s Add STRING after (possible) line number
130
nl Command Example
nl -w Use NUMBER columns for line numbers
131
head Command
• head makes it easy to output the first part (10 lines by default) of
files.
▪ Syntax :
head [OPTION]... [FILE]...
▪ Example :
Optio Use
Print the first n lines instead of the first 10; with the leading '-',
n print all but the last n lines of each file
head -
n
head -c Print the first n bytes of each file; with a leading '-', print all but
the last n bytes of each file
head -q Never print headers identifying file names
132
135
head Command Example
head -q Never print headers identifying file names
136
tail Command
• tail is a command which prints the last few number of lines (10
lines by default) of a certain file, then terminates.
▪ Syntax :
tail [OPTION]... [FILE]...
Optio Use
n tail - Output the last num lines, instead of the default (10)
n
tail -c Output the last num bytes of each file
tail -q Never output headers
137
tail Command Example
138
139
sort Command
• sort command is used to sort a file, arranging the records in a
particular order.
• By default, the sort command sorts file assuming the contents are
ASCII. Using options in sort command, it can also be used to sort
numerically.
141
find Command
• find command searches for files in a directory hierarchy.
▪ Syntax :
find [option] [path...] [expression]
Option Use
find -name filename Search for files that are specified by ‘filename’
find -newer filename Search for files that were modified/created after
‘filename’
find -user name Search for files owned by user name or ID ‘name’
find -size +N/-N Search for files of ‘N’ blocks; ‘N’ followed by ‘c’ can
be used to measure size in characters
find -empty Search for empty files and directories
find -perm octal Search for the file if permission is ‘octal’
147
uniq Command
• uniq reports or filters out repeated lines in a file.
• It can remove duplicates, show a count of occurrences, show only
repeated lines, ignore certain characters and compare on specific
fields.
▪ Syntax :
uniq [OPTION]... [INPUT [OUTPUT]]
Optio Use
n uniq Prints only unique lines
-u
uniq -d Only print duplicated lines
uniq -D Print all duplicate lines
uniq -c Prefix lines with a number representing how many times they
occurred
uniq -i Ignore case when comparing
153
grep Command
• The grep filter searches a file for a particular pattern of characters,
and displays all lines that contain that pattern.
• The pattern that is searched in the file is referred to as the regular
expression.
• grep stands for globally search for regular expression and print out.
▪ Syntax :
grep [options] pattern [files]
Optio Use
n grep Prints only a count of the lines that match a pattern
-c
grep -h Display the matched lines, but do not display the filenames
grep -l Displays list of a filenames only
grep -i Ignores, case for matching
157
▪ Syntax :
command_1 | command_2 | command_3 | .... | command_N…
165
tr(translate) Command
• The tr command in UNIX is a command line utility for translating or
deleting characters.
• It supports a range of transformations including uppercase to
lowercase, squeezing repeating characters, deleting specific
characters and basic find and replace.
• It can be used with UNIX pipes to support more complex
translation.
• tr stands for translate.
▪ Syntax :
tr [OPTION] SET1 [SET2]
169
tr(translate) Command
▪ POSIX Character set supported by tr command :
• [:digit:] Only the digits 0 to 9.
• [:alnum:] Any alphanumeric character.
• [:alpha:] Any alpha character A to Z or a to z.
• [:blank:] Space and TAB characters only.
• [:xdigit:] Hexadecimal notation 0-9, A-F, a-f.
• [:upper:] Any alpha character A to Z.
• [:lower:] Any alpha character a to z..
Option Use
tr -s Replaces repeated characters listed in the set1 with single occurrence
tr -d Delete characters in string1 from the input
tr -c complements the set of characters in string. i.e., operations apply to
characters not in the given set
tr -cd Remove all characters except digits
170
tr Command Example
171
tr Command Example
172
tr Command Example
tr -s Replaces repeated characters listed in the set1 with single occurrence
173
tr Command Example
tr -d Delete characters in string1 from the input
174
tr Command Example
tr -c complements the set of characters in string.
i.e., operations apply to characters not in the given set
175
tr Command Example
tr -cd Remove all characters except digits
176
history Command
• history command is used to view the previously executed
command.
▪ Syntax :
history
▪ Example :
177
178
write Command
• write sends a message to another user.
▪ Syntax :
write user [ttyname]
▪ Example
Optio Use
n user The user to write to
tty The specific terminal to write to, if the user is logged in to more
than one session
179
180
wall Command
• wall send a message to everybody’s terminal.
• wall sends a message to everybody logged in with their mesg
permission set to yes.
▪ Syntax :
wall [-n] [-t TIMEOUT] [file]
▪ Example
Optio Use
n wall --nobanner Suppress banner
-n
wall -t --timeout TIMEOUT Write timeout to terminals in seconds.
TIMEOUT must be positive integer. Default value is 300 seconds,
which is a legacy from time when people ran terminals over
wall Command Example
• Open four different terminal, execute command on first terminal, message will display on
everybody’s terminal.
182