I/O Redirection with Examples
- Sometimes you need to use the output from a command more than once. To accomplish this, you can redirect the output of commands using some neat command – line tricks.
- There are also a few characters you can use to direct or redirect output of commands. These characters are
> ----> Directs output to a file or device (override if the file exists)
< ----> Directs Input from the file or device.
>> ----> Appends output or text to a file (creates if the file doesn’t exist)
| ----> pipes the output of one command to another.
&& ----> combines commands .
STDIN -- File Description (FD) -- 0
STDOUT -- File Description (FD) -- 1
STDERR -- File Description (FD) -- 2
Examples: 1) $cat file > backup
$cat backup ---> To verify
2) $cat > test2 < test2 ---> Input from the test1
$cat test2 ---> To verify
3) $cat test1 test2 test3 2> Error
---> File not exist
$ cat Error ---> To verify
4) $cat sample test test3 > out –file 2>> error
---> File not exist
$ cat out- file & $ cat Error ----> To verify
- Echo : Outputs or displays a string
EX: $echo “this is some sample text”
- To output some text to a file:
$echo “this is some sample text” > file_example
$cat file_example To verify
- CUT: Divides a string or output.
Syn: cut [option] file
--- d Specifies a delimiter
--- f displays a particular field
--- c displays a character.
- Displays the third field of the text using space as a delimiter:
$cut –d “” -f3 file_example
- Displays third & fourth fields:
$cut f3,4 file_example
- Displays 1st to 5th characters:
$cut - c 1-5 file_example
- Past: To join two or more files horizontally by using delimiters
- To join two files horizontally:
$ paste states capitals
- To join two files using delimiter:
$ paste - d “ : ”States capitals
AP : HYD
MP : Bopal
KN : Bangalore
$Paste --d “ : ” states capitals > example
- WC: Provides a word or line count
Syn: WC [options] file
--- l lines
--- w words
--- c characters
$WC Example Displays lines words and characters
$WC - l Example only lines
$WC - w Example only words.
- Diff: Displays different lines between two files
$diff file1 file2
- Cmp: It compares two files character by character.
$ cmp file1 file2
Note: If files are same it doesn’t return any output otherwise it displays line numbers and character position
- tr: It translate character by character.
$tr “aeiou” “AEIOU” <sample
$tr “a-z” “A-Z” <sample Translate lower to upper.
$ tr “A-Z” “a-z” < sample Upper to lower
$tr - s “ “ <sample
$tr -d “aeiou” <sample To delete aeiou
$tr “,” “\t” < sample Replaced with tab space.
- A spell check: To check the spelling mistakes but not grammatical mistakes.
$a spell check sample
$a spell check test
- Head: Displays top 10 lines of the file
$ head sample
$ head -5 sample Top 5 lines
- Tail: Displays last 10 lines of the file
$ tail sample
$ tail -5 sample last 5 lines
$tail - f sample file is open continuously
- Piping(|): Combine the two or more commands in to a single line.
- Here the first command output is taken as the next command input.
$ ls - l/ wc – l
$cat example / cut –d “ “ - f3 file _ example
$ cat example / head -20
- &&: combines commands
$ echo “ this is text file” > file_example && cut –f3 example
$ cat file_example To verify
$ echo “ My original text: >> file _example && cat file_example
- More: To see the contents of a file in the form of page wise.
$ more example
- Less: To display file contents in page wise. But we can go to all directions.
$ Less example
Options: f ----> forward direction
B ----> Backward direction
V ----> vi editor mode
q ----> To quit
à Tee: It is used to write the data into the files as well as on the screen.
$cat sample / tee file1 file2 file3
$cat file1 To verify
$cat file 2
- Sort: Sorts the output of a command or file.
Syn: Sort [options} FILE
--- R ----> sorts in reverse order
--- B ----> Ignores leading blanks
--- n ----> compares according to numerical storing value
$ sort example
$ sort –r example Reverse order
$ sort –n example Display numeric
$ sort –u example unique lines
$ sort --f Example Ignores case
Inclined to build a profession as Linux Developer? Then here is the blog post on Linux Training Online.
- Uniq: Lists all the unique lines in a file or command output.
$ uniq example
$ Uniq –u Example Displays non duplicated lines
$ Uniq –d Example Displays only duplicated lines
$ Uniq file_ Example > uniq_file && cat uniq – file
- In above command go view uniq lines in the sample file, create a new file based on the output, and view the contents of this new file,
- Sed (Stream editor): To search and replace strings or patterns in the given file
- Sed is a multipurpose filter command
Syn: sed “s/old string name/new string name/g” <filename>
S ---> substitution
G à global occurrence in every line.
$sed “s/unix/linux/g” sample
$sed “s/unix/linux/gi” sample Ignore case
$sed “s/unix/linux/” sample
$sed “s/˄unix/linux/gi sample
$sed “s/unix//gi sample Delete a word from a file
$sed –e “s/unix/sas/gi” -e “s/linux/dba/gi” sample
$sed –n “2p” sample To print 2nd row
$sed –n “3,5p” sample To print 3rd, 4th, 5th rows
$sed –n “IP
>$p” sample point 1st and last rows
$ sed ‘3d’ sample Delete 3rd row
$sed ‘2,5d’ sample Delete 2 to 5 lines
$ sed ‘2,5 w file’ sample It copies 2nd to 5th rows from sample file to file
$ sed ‘=’ sample To get line numbers.
- Regular Expressions (or) Regex (Grep):
- Globally research a regular expression & print
- To search a string or regular expression in a file(s)
Syn: grep [options] PATTERN FILE (S)
$ grep root sample
$ grep root sample example backup
$grep root * search all files in a current directory
$grep --i root sample Ignore case
$grep --c root sample COUNTS NO. OF LINES
$grep –n root sample print the lines along with line no’s
$grep –l root sample list file names only he given pattern
$grep –r root * search the pattern recursively
$grep –v root sample prints non matching lines
$grep –o root sample prints only the given pattern.
$grep root sample -- color displays output in color
$grep “it technology” sample
$grep “exam*” sample Prints start with exam pattern.
$grep “b[aeiou] ll” sample
$grep “b..d” sample
Note: “.” & “*” are wild card characters, it matcher any single character.
$grep c[on] example
$grep [0-9] example
\< /> Þ word boundary
\< Þ starting of the word
\< Þ Ending of the word
$grep “/<root/>” sample
$grep “\,root” sample
$grep “root/>” sample
$ grep “\<[0-9][0-9][0-9][0-9]>” sample
For indepth understanding of Linux click on