0% found this document useful (0 votes)
14 views25 pages

Dms 65e2944a60017AdvancedCommand

The document discusses I/O redirection and piping in Unix/Linux operating systems. It describes how to redirect standard input/output to files or other programs. It also covers piping where the output of one command can serve as input to another command. Basic Unix commands for file manipulation like head, tail, sort, grep are also explained.

Uploaded by

Zónè Noßrù
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views25 pages

Dms 65e2944a60017AdvancedCommand

The document discusses I/O redirection and piping in Unix/Linux operating systems. It describes how to redirect standard input/output to files or other programs. It also covers piping where the output of one command can serve as input to another command. Basic Unix commands for file manipulation like head, tail, sort, grep are also explained.

Uploaded by

Zónè Noßrù
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

I/O redirection and piping

• In all OS there is a standard I/P device and a


standard O/P device
• In Unix Standard Input is Keyboard and value
is 0 and Standard Output is screen and value is
1
• Standard error is screen and value is 2
• If not instructed Unix commands/programs
get their input from standard input device and
send their output to standard output device
I/O redirection
• Sometimes it is necessary to redirect the input
or output to a file or program
• Symbol < is used to redirect input
• Symbol > is used to redirect output
• Symbol >> adds output to the end of the file
• Example
cat file1 > file2
now the result of cat file1 is sent to
‘file2’ not to screen!
Redirection: more examples
• ls > filelist
• (date;who) > logfile
• cat file1 >> file2
• cat > file1 < file2
• 1 > &2 : redirect standard output
to standard error (use with echo)
• sortProg < 10 9 40 : where
sortProg is a program to sort 3
number taken from standard input.
Piping
• Sometimes it may not be possible to solve a
problem using a single command.
• Piping lets us connect command to other
commands i.e to send output of one command
as input for another
• Syntax is cmd1 | cmd2
• Example: ls | wc –l > countFile
Basic file utilities
• head –n files
– Print the first n lines (default is 10) of one or more
files
• tail –n files
– Print the last n lines (default is 10) of one or more
files
• less –n files
– Print the last n lines (default is 10) of one or more
files
Basic file utilities
• sort : displays contents of a file after sorting
• Syntax: sort <myFile>
• sort <file1> <file2> <file3>
sorts several files
• sort –o <res> <file1>
sorted result of file1 is saved in res
• sort –m <file1> <file2> …
merging files already sorted
Basic file utilities
• diff : compares two text files
Syntax: diff <file1> <file2>
• uniq : removes duplicate lines from a sorted file
Syntax: uniq <file1>
• grep: global regular expression print
• The command searches the specified file(s) for a
match with the supplied pattern and displays it.
• Syntax: grep <search string> <file>
grep picture file
search the word ‘picture’ in file
Basic file utilities
• grep picture –i –n file
i: case insensitive
n: line no
• grep [Rr]ebecca file
all occurrences of rebecca as well
as rebecca in file
• grep [b??k] file
all 4 letter words whose first letter
is b and last letter is k (book, back, etc…)
Basic file utilities
• cut: it cuts or picks up a given number of
character or fields from the file
• cut –f 2,7 file: displays field
2 and field 7
• cut –f 2-7 file: displays fields
2 through 7
• It assumes default delimiter as tab/space
• cut –f 2,7 –d “:” file
delimiter is now “:”
• cut –c 1-15 file
Locating commands
• These command help to find a command whose
name you have forgotten or whose location you
do not know
• which <command name>
searches from path and displays the first
occurrence with full path name to the file
which tar O/P: /bin/tar
• whereis tar
searches for copies by looking in a few
standard locations instead of using search path
Locating commands
• apropos: the utility searches for the keyword
in all of the man pages and displays those that
contain a match (partial)
Example: apropos sort
• whatis is utility is similar to apropos but finds
only complete word matches on the name of
the utility
• slocate/locate: is used to search for any file
• find : used to search a file
Compressing and archiving
• gzip: compresses a file by analyzing it and
recording it more efficiently.
• gzip <file1> O/P: compressed file as file1.gz
• gunzip <file1.gz> : uncompress a file
• tar: short for tape archive
– Is used to create a single file out of multiple files or
directories containing any level of hierarchy
tar –cvf exp.tar <f1> <f2> <f3> . . .
c: create, v: verbose f: write/read to/from a file
Miscellaneous commands
• du: displays information on disk utility
• It reports how much disk space is occupied by
a directory or a file
du –m –s <dir name>
• m: in megabites, s: total size
• df: displays disk space usage
df O/P: information about all file
• ps: displays process status
ps –a :all processes, kill <pid>: to kill a
process
Some VI Commands
Deleting Text
• First move the cursor so that it covers the first character
of the group you want to delete, then type the desired
command as below
• x Delete only the current character
• D Delete to the end of the line
• db Delete from the current character to the beginning
of the current word
• de Delete from the current character to the end of the
current word
• dd Delete the current line
• dw Delete from the current character to the beginning
of the next word
Copy/Cut and Paste

Type To:
transpose two characters (two commands, x
xp
followed by p)
yank (i.e. copy) one line into a general buffer (5yy
yy
to yank 5 lines)
p paste after the current line

P Paste before the current line


Copy/Cut and Paste using markers

– Markers let you specify a range of text for cutting


or copying without having to count words or lines.
– Move the cursor to one end of the selection.
– Type mletter to set a marker. (letter: a-z)
– Move the cursor to the other end.
– Type d or y to delete or yank text, followed by.
– the same letter from the marker, type `letter to
delete or yank the text between the marker and the
cursor.
Search and Replace
• As files become longer, you may need
assistance locating a particular instance of text.
• Simple search
– /<search string> enter
– n: next forward, N: next backward
• Search-Exclusions/wild-card search
• The special characters are: $ . * [ ] ^ \.
The slash (/) starts a forward search. And (?) to
search backwards.
Wild-Card Search
Search and Replace
• finding instances of a given string and replacing them
with a new string. It has the following
• : line1, line2s/ oldstring / newstring
• You specify the range of text for the search-and-
replace command with the line numbers line1 and
• line2. If you only give line1 and omit the comma,
then the command only affects that line. If you
• give no line numbers, the command affects the
current line. The search-and-replace command only
finds the first occurrence of oldstring on the line.
Search and Replace
• You can repeat a search-and-replace on the
current line by typing an ampersand (&). To
repeat it on a different line or group of lines, type:
• : linenumber& or
• : line1,line2&
• The command g (for global) can be used with s
(substitute) to find and replace every occurrence
of a string pattern in an entire file.
• The syntax of the global command is:
• :g/string/commands
Search and Replace
• The global command finds each line in the file that has
string in it and then applies the commands to it. You can
combine the global and substitute it in the following
manner:
• :g/oldstring/s//newstring/g
• You don’t need to put oldstring in the search string part
of the substitute command because the editor already has
the pattern from the global command.
• When specifying oldstring, you can use the special
characters as explained in the section Special Characters.
vi does not recognize special characters in newstring,
since it is performing a replacement, not a search.
Variables in VI
• vi maintains several variables that control different
aspects of its appearance.
• Two types of variables 1. toggle variables and 2. numeric
variables.
• Toggle variables turn an option on or off , while numeric
variables take a number as their argument.
• You turn on a toggle variable with :set variable
• and you turn it off by typing :set novariable
• Numeric variables are set with an equals sign (=) and the
corresponding value. To set tab stops every 6 spaces, you
would enter :set tabstop=6
Variables in VI
Miscellaneous
• u - undo last change
• U - undo all changes to the current line
• J - join current line and the line below it
• . - repeat the last command
• :! - command invoke an external "command"
• :%s/frog/toad/ -Replace the first occurrence of
"frog" with "toad" for all lines in the file.
• dG - delete to end of the file
• 1G - move to the beginning of file . . .

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy