SlideShare a Scribd company logo
Junior Level Linux Certification
Exam Objectives
Key Knowledge Areas
Redirecting standard input, standard output and standard error.
Pipe the output of one command to the input of another command.
Use the output of one command as arguments to another command.
Send output to both stdout and a file.
Objective 3: GNU and Unix Commands
Use streams, pipes and redirects Weight: 4
Terms and Utilities
tee
xargs
2
Process text streams using filters
Rundown of commands
commands Overview
3
tee - send output to standard output and files
xargs - expand input to command line arguments
< - redirect standard input from a file
<< - redirect standard input from text in a script
> - send standard output to a file
>> - append standard output to a file
| - pipe a programs's standard output to standard input of another
` ` - capture the output of a command and use it in a script
Use streams, pipes and redirects
bash shell, and the other shells employed by Linux allow the user to specify what should
happen to the input and output of programs that run.
Input and output redirection
4
shell creates new processes as follows:
1.shell reads the commands to be executed
2.shell opens the files which will be needed (open(2) for files, pipe(2) for streams)
3.shell creates the new processes which will be executed (fork(2) each process)
4.Each new process is connected to the appropriate plumbing (dup2(2) each redirection)
5.Control of processes is passed to programs the user asked for (execve(2) each command)
Use streams, pipes and redirects
Input and output redirection
5
Redirect with pipes (|) are processed 1st
, other redirect (> and <) are processed left to right.
Redirection Effect of redirection
command1 | command2 Output from command1 is input for command2
command < file Command reads input from a file
command > file Output of command goes to file
command 2> file Errors from the command go to a file
command >> file Output of a command is added to a file
command > file 2>&1 Output and errors go to a file
command >& file
command &> file
$ ls > file
$ cat < file
$ ls -la | grep file
$ cat /etc/passwd | sed 's/:.*//'
$ badcommandorfilename 2> errorlog
$ grep pop3 /etc/* 2>/dev/null
$ grep pop3 /etc/* 2>&1 | less
$ less < errorlog
$ less errorlog
Ex:
Standard input redirection (<, <<EOF, |)
[jack@foo jack]$ wc
Hello. I typed this line. I typed and typed. I wondered, as I typed, how many words I was typing.
Tis wondrous that as I typed, bash piped.
<Ctrl+D>
4 28 143
Ex:
Standard input is the terminal, by default. The following redirections are possible:
6
Use streams, pipes and redirects
Read input from the terminal:
Read input from file with <. standard input anonymous: wc does not know file name which it is reading from.
[jack@foo jack]$ wc < /etc/passwd
46 62 2010
Ex:
Read input from a program using the pipe redirection. Here cat is piping its output to wc.Ex:
[jack@foo jack]$ cat /etc/passwd | wc
46 62 2010
Read input from script or cmdline. Operator << is traditionally followed by EOF, but any letters can be used.
When a line is found which consists only of EOF, that's the end of the input. This is called a “here document”.
Ex:
[jack@foo jack]$ wc << EOF
> Hello
> There are
> Four lines
> I think
> EOF
4 7 35
Standard output redirection (>, >>)
[jack@foo jack]$ uptime
5:09pm up 8:26, 6 users, load average: 0.22, 0.20, 0.20
Ex:
Standard output is the terminal, by default. The following redirections are possible:
7
Use streams, pipes and redirects
No redirection
Redirection to a file (“>” means “to”). If the file already exists, it is overwritten.
[jack@foo jack]$ uptime > Uptime-file
[jack@foo jack]$ ls -l Uptime-file
-rw-rw-r-- 1 jack jack 62 Apr 8 17:09 Uptime-file
[jack@foo jack]$ cat Uptime-file
5:09pm up 8:26, 6 users, load average: 0.13, 0.18, 0.19
Ex:
Redirection appending to a file (“>>” means “to the end of”)Ex:
[jack@foo jack]$ uptime >> Uptime-file
[jack@foo jack]$ cat Uptime-file
5:09pm up 8:26, 6 users, load average: 0.13, 0.18, 0.19
5:10pm up 8:27, 6 users, load average: 0.24, 0.21, 0.20
Redirecting to a program. Here the output of uptime is being piped as the input to wc.Ex:
jack@foo jack]$ uptime | wc
1 10 62
Standard error redirection (2>, 2>>, 2>&1)
By default, Linux processes produce two error streams.
Regular output is sent to stdout (standard output “1”) and errors are sent to stderr (standard error “2”).
8
Use streams, pipes and redirects
Ex: [jack@foo jack]$ tar cf sysconfig.tar /etc/sysconfig
tar: Removing leading `/' from member names
tar: /etc/sysconfig/rhn/systemid: Cannot open: Permission denied
tar: /etc/sysconfig/rhn/up2date-keyring.gpg: Cannot open: Permission
denied
tar: /etc/sysconfig/iptables: Cannot open: Permission denied
tar: /etc/sysconfig/static-routes: Cannot open: Permission denied
tar: Error exit delayed from previous errors
Output sent to stderr:
•Error messages (command 2>/dev/null)
•Warning messages (command 2>> warning.log)
•Debugging and progress indicators (command 2>&1 | less )
If stderr is not redirected, error output is displayed on the terminal.
Standard error redirection (2>, 2>>, 2>&1)
(command 2>/dev/null) sending the errors to a file. It is quite common to send error text to /dev/null
9
Use streams, pipes and redirects
Ex:
[jack@foo jack]$ tar cf sysconfig.tar /etc/sysconfig 2> /dev/null
[jack@foo jack]$ tar cf sysconfig.tar /etc/sysconfig 2> warning.log
append the errors to an existing file ...Ex:
[jack@foo jack]$ tar cf pam.tar /etc/pam.d/
tar: Removing leading `/' from member names
tar: /etc/pam.d/sshd: Cannot open: Permission denied
tar: Error exit delayed from previous errors
[jack@foo jack]$ tar cf pam.tar /etc/pam.d/ 2>>warning.log
[jack@foo jack]$ cat warning.log
tar: Removing leading `/' from member names
tar: /etc/sysconfig/rhn/systemid: Cannot open: Permission denied
tar: /etc/sysconfig/rhn/up2date-keyring.gpg: Cannot open: Permission denied
tar: /etc/sysconfig/iptables: Cannot open: Permission denied
tar: /etc/sysconfig/static-routes: Cannot open: Permission denied
tar: Error exit delayed from previous errors
tar: Removing leading `/' from member names
tar: /etc/pam.d/sshd: Cannot open: Permission denied
tar: Error exit delayed from previous errors
Standard error redirection (2>, 2>>, 2>&1)
The notation 2>&1 means “stderr(2) to copy of stdout(1)”.
The following all send standard error output to the same destination as standard output:
• command >file 2>&1
• command 2>file 1>&2
• command &> file
• command >& file (this is not the preferred method)
• command 2>&1 | anothercommand
10
Use streams, pipes and redirects
discard the output and the errors from the commands, sending them to /dev/null.Ex:
[jack@foo jack]$ tar vcf pam.tar /etc/sysconfig >/dev/null 2>&1
[jack@foo jack]$ nosuchcommandorfilename 2>/dev/null 1>&2
[jack@foo jack]$ ls -la /root 2>/dev/null 1>&2
[jack@foo jack]$ updatedb >& /dev/null &
[jack@foo jack]$ killall -HUP inetd sendmail &> /dev/null
[jack@foo jack]$ killall -HUP inetd sendmail &> /dev/null
commands send standard error output to the same destination standard output (a program).Ex:
[jack@foo jack]$ bash -x /etc/init.d/network restart 2>&1 | less
[jack@foo jack]$ tar -c / 2>&1 | less
[jack@foo jack]$ strace mailq 2>&1 | less
Command pipelines (|)
Pipeline – Ability to join input and output of processes together.
To set up a pipe use pipe symbol | between cmds that generates output.
Generally, data piped is text, but its possible to join far too many processes together.
11
Use streams, pipes and redirects
Counting how many unique words appear in the fortune cookie database.Ex:
$ ls /usr/share/games/fortune/* | grep -v '.' | xargs cat |
sed 's/[^a-zA-Z]+/ /g; s/^ *// ' | tr 'A-Z ' 'a-zn' |
sort | uniq | wc -l
28234
ls lists files in fortune cookie directory. grep v removes files that have a dot in the name.
xargs runs cat with all of the file names on its command line. cat prints the contents of the files.
sed edits the stream replacing anything that is not AZ and az with a space, then removes leading spaces.
tr converts everything to lowercase. sort sorts the words in alphabetical order.
uniq removes the duplicates. wc says how many lines there were in its input.
tee – divert a copy to a file
tee is like a teepiece for pipes (or T piece).
Allows to pipe to one or more files and simultaneously sending output to standard output.
By default tee is like redirection > and overwrites contents in the files specified. To apend use tee -a.
12
Use streams, pipes and redirects
tee can save the results of a command in a file while view them on the terminal.Ex:
# ifconfig | tee current-netconf
# route | tee current-routes
tee catches the output of grep in resultsfile, and wc counts it.Ex:
$ ls -lR /etc | grep host | tee resultsfile | wc -l
$ ls -lR /etc | grep host | tee /dev/tty | wc -l
tee -a does not overwrite current contents of a file. In scripts is useful for appending results to log files.Ex:
$ rpm -hiv ppp-2.4.1-7.i386.rpm 2>&1 | tee -a install-log networklog
Recording results of loading a kernel module by reading kernel message buffer with dmesg.
The results are also displayed on the terminal.
Ex:
# date | tee -a log-file
# dmesg -c > /dev/null
# modprobe ppp_generic | tee -a log-file
# dmesg -c | tee -a log-file
Command substitution – $(command) and `command`
Command substitution
- grab standard output of a command and put it on the command line of another command.
13
Use streams, pipes and redirects
grep and cut commands conspire here to find the name server which is in use.
This is then entered into an environment variable.
Ex:
$ cat /etc/resolv.conf
search example.com
nameserver 192.168.44.2
$ grep nameserver /etc/resolv.conf
nameserver 192.168.44.2
$ grep nameserver /etc/resolv.conf | cut -d ' ' -f 2
192.168.44.2
$ NAMESERVER=`grep nameserver /etc/resolv.conf | cut -d ' ' -f 2`
$ echo $NAMESERVER
192.168.44.2
Forms `command` and $(command) are equivalent.
Quote: `…` is backward quote –difficult to distinguish from regular quote (apostrophe), so some prefer the form $(...)
Rather than using /usr/bin/ftp we use `which ftp`Ex:
$ rpm -qif `which ftp`
$ basename `which ftp`
$ dirname `which ftp`
xargs
xargs - expands its standard input to be arguments to the command it is given.
14
Use streams, pipes and redirects
generate a list of files with grep, and then read files with less. The actual command run is less file1 file2 ...Ex:
grep -l hello /etc/* | xargs less
grep -l root /etc/* 2>/dev/null | xargs wc
The following commands are equivalent:Ex:
echo one two three | xargs rm
rm ` echo one two three `
rm one two three
xargs is often used to handle a list of file names generated by another command.
xargs is frequently used with find. find finds the files, and xargs starts up command that handles the file.
find regular files that have not been accessed for more than 30 days, and removes themEx:
find /tmp -type f -atime +30 | xargs rm
find /tmp -type f -atime +30 -print0 | xargs -0 rm
Fim de sessão
15

More Related Content

What's hot

Unix(introduction)
Unix(introduction)Unix(introduction)
Unix(introduction)
meashi
 

What's hot (19)

Unix Command Line Productivity Tips
Unix Command Line Productivity TipsUnix Command Line Productivity Tips
Unix Command Line Productivity Tips
 
Basic linux commands
Basic linux commands Basic linux commands
Basic linux commands
 
Linux commands
Linux commandsLinux commands
Linux commands
 
SGN Introduction to UNIX Command-line 2015 part 1
SGN Introduction to UNIX Command-line 2015 part 1SGN Introduction to UNIX Command-line 2015 part 1
SGN Introduction to UNIX Command-line 2015 part 1
 
Part 6 of "Introduction to linux for bioinformatics": Productivity tips
Part 6 of "Introduction to linux for bioinformatics": Productivity tipsPart 6 of "Introduction to linux for bioinformatics": Productivity tips
Part 6 of "Introduction to linux for bioinformatics": Productivity tips
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Unix slideshare
Unix slideshareUnix slideshare
Unix slideshare
 
Useful Linux and Unix commands handbook
Useful Linux and Unix commands handbookUseful Linux and Unix commands handbook
Useful Linux and Unix commands handbook
 
Unix(introduction)
Unix(introduction)Unix(introduction)
Unix(introduction)
 
3.2 process text streams using filters
3.2 process text streams using filters3.2 process text streams using filters
3.2 process text streams using filters
 
Basic Linux day 2
Basic Linux day 2Basic Linux day 2
Basic Linux day 2
 
50 most frequently used unix
50 most frequently used unix50 most frequently used unix
50 most frequently used unix
 
Basic Linux day 1
Basic Linux day 1Basic Linux day 1
Basic Linux day 1
 
50 Most Frequently Used UNIX Linux Commands -hmftj
50 Most Frequently Used UNIX  Linux Commands -hmftj50 Most Frequently Used UNIX  Linux Commands -hmftj
50 Most Frequently Used UNIX Linux Commands -hmftj
 
Linux command for beginners
Linux command for beginnersLinux command for beginners
Linux command for beginners
 
101 3.2 process text streams using filters
101 3.2 process text streams using filters101 3.2 process text streams using filters
101 3.2 process text streams using filters
 
101 3.2 process text streams using filters
101 3.2 process text streams using filters101 3.2 process text streams using filters
101 3.2 process text streams using filters
 
Unix Basics For Testers
Unix Basics For TestersUnix Basics For Testers
Unix Basics For Testers
 
Bozorgmeh os lab
Bozorgmeh os labBozorgmeh os lab
Bozorgmeh os lab
 

Similar to 101 3.4 use streams, pipes and redirects

Unit 7 standard i o
Unit 7 standard i oUnit 7 standard i o
Unit 7 standard i o
root_fibo
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
Dr.Ravi
 
Unit 10 investigating and managing
Unit 10 investigating and managingUnit 10 investigating and managing
Unit 10 investigating and managing
root_fibo
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 

Similar to 101 3.4 use streams, pipes and redirects (20)

Licão 08 system redirects
Licão 08 system redirectsLicão 08 system redirects
Licão 08 system redirects
 
101 3.4 use streams, pipes and redirects v2
101 3.4 use streams, pipes and redirects v2101 3.4 use streams, pipes and redirects v2
101 3.4 use streams, pipes and redirects v2
 
3.4 use streams, pipes and redirects v2
3.4 use streams, pipes and redirects v23.4 use streams, pipes and redirects v2
3.4 use streams, pipes and redirects v2
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
 
Unit 7 standard i o
Unit 7 standard i oUnit 7 standard i o
Unit 7 standard i o
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them All
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 
How to admin
How to adminHow to admin
How to admin
 
Unit 10 investigating and managing
Unit 10 investigating and managingUnit 10 investigating and managing
Unit 10 investigating and managing
 
Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
 
Linux
LinuxLinux
Linux
 
Linux And perl
Linux And perlLinux And perl
Linux And perl
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Odoo command line interface
Odoo command line interfaceOdoo command line interface
Odoo command line interface
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
 
linux_Commads
linux_Commadslinux_Commads
linux_Commads
 

More from Acácio Oliveira

More from Acácio Oliveira (20)

Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptxSecurity+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
 
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptxSecurity+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
 
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptxSecurity+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
 
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptxSecurity+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
 
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptxSecurity+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
 
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptxSecurity+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
 
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptxSecurity+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
 
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptxSecurity+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
 
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptxSecurity+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
 
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptxSecurity+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
 
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptxSecurity+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
 
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
 
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptxSecurity+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
 
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptxSecurity+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
 
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
 
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptxSecurity+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
 
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptxSecurity+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
 
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptxSecurity+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
 
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptxSecurity+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
 
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptxSecurity+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 

101 3.4 use streams, pipes and redirects

  • 1. Junior Level Linux Certification
  • 2. Exam Objectives Key Knowledge Areas Redirecting standard input, standard output and standard error. Pipe the output of one command to the input of another command. Use the output of one command as arguments to another command. Send output to both stdout and a file. Objective 3: GNU and Unix Commands Use streams, pipes and redirects Weight: 4 Terms and Utilities tee xargs 2
  • 3. Process text streams using filters Rundown of commands commands Overview 3 tee - send output to standard output and files xargs - expand input to command line arguments < - redirect standard input from a file << - redirect standard input from text in a script > - send standard output to a file >> - append standard output to a file | - pipe a programs's standard output to standard input of another ` ` - capture the output of a command and use it in a script
  • 4. Use streams, pipes and redirects bash shell, and the other shells employed by Linux allow the user to specify what should happen to the input and output of programs that run. Input and output redirection 4 shell creates new processes as follows: 1.shell reads the commands to be executed 2.shell opens the files which will be needed (open(2) for files, pipe(2) for streams) 3.shell creates the new processes which will be executed (fork(2) each process) 4.Each new process is connected to the appropriate plumbing (dup2(2) each redirection) 5.Control of processes is passed to programs the user asked for (execve(2) each command)
  • 5. Use streams, pipes and redirects Input and output redirection 5 Redirect with pipes (|) are processed 1st , other redirect (> and <) are processed left to right. Redirection Effect of redirection command1 | command2 Output from command1 is input for command2 command < file Command reads input from a file command > file Output of command goes to file command 2> file Errors from the command go to a file command >> file Output of a command is added to a file command > file 2>&1 Output and errors go to a file command >& file command &> file $ ls > file $ cat < file $ ls -la | grep file $ cat /etc/passwd | sed 's/:.*//' $ badcommandorfilename 2> errorlog $ grep pop3 /etc/* 2>/dev/null $ grep pop3 /etc/* 2>&1 | less $ less < errorlog $ less errorlog Ex:
  • 6. Standard input redirection (<, <<EOF, |) [jack@foo jack]$ wc Hello. I typed this line. I typed and typed. I wondered, as I typed, how many words I was typing. Tis wondrous that as I typed, bash piped. <Ctrl+D> 4 28 143 Ex: Standard input is the terminal, by default. The following redirections are possible: 6 Use streams, pipes and redirects Read input from the terminal: Read input from file with <. standard input anonymous: wc does not know file name which it is reading from. [jack@foo jack]$ wc < /etc/passwd 46 62 2010 Ex: Read input from a program using the pipe redirection. Here cat is piping its output to wc.Ex: [jack@foo jack]$ cat /etc/passwd | wc 46 62 2010 Read input from script or cmdline. Operator << is traditionally followed by EOF, but any letters can be used. When a line is found which consists only of EOF, that's the end of the input. This is called a “here document”. Ex: [jack@foo jack]$ wc << EOF > Hello > There are > Four lines > I think > EOF 4 7 35
  • 7. Standard output redirection (>, >>) [jack@foo jack]$ uptime 5:09pm up 8:26, 6 users, load average: 0.22, 0.20, 0.20 Ex: Standard output is the terminal, by default. The following redirections are possible: 7 Use streams, pipes and redirects No redirection Redirection to a file (“>” means “to”). If the file already exists, it is overwritten. [jack@foo jack]$ uptime > Uptime-file [jack@foo jack]$ ls -l Uptime-file -rw-rw-r-- 1 jack jack 62 Apr 8 17:09 Uptime-file [jack@foo jack]$ cat Uptime-file 5:09pm up 8:26, 6 users, load average: 0.13, 0.18, 0.19 Ex: Redirection appending to a file (“>>” means “to the end of”)Ex: [jack@foo jack]$ uptime >> Uptime-file [jack@foo jack]$ cat Uptime-file 5:09pm up 8:26, 6 users, load average: 0.13, 0.18, 0.19 5:10pm up 8:27, 6 users, load average: 0.24, 0.21, 0.20 Redirecting to a program. Here the output of uptime is being piped as the input to wc.Ex: jack@foo jack]$ uptime | wc 1 10 62
  • 8. Standard error redirection (2>, 2>>, 2>&1) By default, Linux processes produce two error streams. Regular output is sent to stdout (standard output “1”) and errors are sent to stderr (standard error “2”). 8 Use streams, pipes and redirects Ex: [jack@foo jack]$ tar cf sysconfig.tar /etc/sysconfig tar: Removing leading `/' from member names tar: /etc/sysconfig/rhn/systemid: Cannot open: Permission denied tar: /etc/sysconfig/rhn/up2date-keyring.gpg: Cannot open: Permission denied tar: /etc/sysconfig/iptables: Cannot open: Permission denied tar: /etc/sysconfig/static-routes: Cannot open: Permission denied tar: Error exit delayed from previous errors Output sent to stderr: •Error messages (command 2>/dev/null) •Warning messages (command 2>> warning.log) •Debugging and progress indicators (command 2>&1 | less ) If stderr is not redirected, error output is displayed on the terminal.
  • 9. Standard error redirection (2>, 2>>, 2>&1) (command 2>/dev/null) sending the errors to a file. It is quite common to send error text to /dev/null 9 Use streams, pipes and redirects Ex: [jack@foo jack]$ tar cf sysconfig.tar /etc/sysconfig 2> /dev/null [jack@foo jack]$ tar cf sysconfig.tar /etc/sysconfig 2> warning.log append the errors to an existing file ...Ex: [jack@foo jack]$ tar cf pam.tar /etc/pam.d/ tar: Removing leading `/' from member names tar: /etc/pam.d/sshd: Cannot open: Permission denied tar: Error exit delayed from previous errors [jack@foo jack]$ tar cf pam.tar /etc/pam.d/ 2>>warning.log [jack@foo jack]$ cat warning.log tar: Removing leading `/' from member names tar: /etc/sysconfig/rhn/systemid: Cannot open: Permission denied tar: /etc/sysconfig/rhn/up2date-keyring.gpg: Cannot open: Permission denied tar: /etc/sysconfig/iptables: Cannot open: Permission denied tar: /etc/sysconfig/static-routes: Cannot open: Permission denied tar: Error exit delayed from previous errors tar: Removing leading `/' from member names tar: /etc/pam.d/sshd: Cannot open: Permission denied tar: Error exit delayed from previous errors
  • 10. Standard error redirection (2>, 2>>, 2>&1) The notation 2>&1 means “stderr(2) to copy of stdout(1)”. The following all send standard error output to the same destination as standard output: • command >file 2>&1 • command 2>file 1>&2 • command &> file • command >& file (this is not the preferred method) • command 2>&1 | anothercommand 10 Use streams, pipes and redirects discard the output and the errors from the commands, sending them to /dev/null.Ex: [jack@foo jack]$ tar vcf pam.tar /etc/sysconfig >/dev/null 2>&1 [jack@foo jack]$ nosuchcommandorfilename 2>/dev/null 1>&2 [jack@foo jack]$ ls -la /root 2>/dev/null 1>&2 [jack@foo jack]$ updatedb >& /dev/null & [jack@foo jack]$ killall -HUP inetd sendmail &> /dev/null [jack@foo jack]$ killall -HUP inetd sendmail &> /dev/null commands send standard error output to the same destination standard output (a program).Ex: [jack@foo jack]$ bash -x /etc/init.d/network restart 2>&1 | less [jack@foo jack]$ tar -c / 2>&1 | less [jack@foo jack]$ strace mailq 2>&1 | less
  • 11. Command pipelines (|) Pipeline – Ability to join input and output of processes together. To set up a pipe use pipe symbol | between cmds that generates output. Generally, data piped is text, but its possible to join far too many processes together. 11 Use streams, pipes and redirects Counting how many unique words appear in the fortune cookie database.Ex: $ ls /usr/share/games/fortune/* | grep -v '.' | xargs cat | sed 's/[^a-zA-Z]+/ /g; s/^ *// ' | tr 'A-Z ' 'a-zn' | sort | uniq | wc -l 28234 ls lists files in fortune cookie directory. grep v removes files that have a dot in the name. xargs runs cat with all of the file names on its command line. cat prints the contents of the files. sed edits the stream replacing anything that is not AZ and az with a space, then removes leading spaces. tr converts everything to lowercase. sort sorts the words in alphabetical order. uniq removes the duplicates. wc says how many lines there were in its input.
  • 12. tee – divert a copy to a file tee is like a teepiece for pipes (or T piece). Allows to pipe to one or more files and simultaneously sending output to standard output. By default tee is like redirection > and overwrites contents in the files specified. To apend use tee -a. 12 Use streams, pipes and redirects tee can save the results of a command in a file while view them on the terminal.Ex: # ifconfig | tee current-netconf # route | tee current-routes tee catches the output of grep in resultsfile, and wc counts it.Ex: $ ls -lR /etc | grep host | tee resultsfile | wc -l $ ls -lR /etc | grep host | tee /dev/tty | wc -l tee -a does not overwrite current contents of a file. In scripts is useful for appending results to log files.Ex: $ rpm -hiv ppp-2.4.1-7.i386.rpm 2>&1 | tee -a install-log networklog Recording results of loading a kernel module by reading kernel message buffer with dmesg. The results are also displayed on the terminal. Ex: # date | tee -a log-file # dmesg -c > /dev/null # modprobe ppp_generic | tee -a log-file # dmesg -c | tee -a log-file
  • 13. Command substitution – $(command) and `command` Command substitution - grab standard output of a command and put it on the command line of another command. 13 Use streams, pipes and redirects grep and cut commands conspire here to find the name server which is in use. This is then entered into an environment variable. Ex: $ cat /etc/resolv.conf search example.com nameserver 192.168.44.2 $ grep nameserver /etc/resolv.conf nameserver 192.168.44.2 $ grep nameserver /etc/resolv.conf | cut -d ' ' -f 2 192.168.44.2 $ NAMESERVER=`grep nameserver /etc/resolv.conf | cut -d ' ' -f 2` $ echo $NAMESERVER 192.168.44.2 Forms `command` and $(command) are equivalent. Quote: `…` is backward quote –difficult to distinguish from regular quote (apostrophe), so some prefer the form $(...) Rather than using /usr/bin/ftp we use `which ftp`Ex: $ rpm -qif `which ftp` $ basename `which ftp` $ dirname `which ftp`
  • 14. xargs xargs - expands its standard input to be arguments to the command it is given. 14 Use streams, pipes and redirects generate a list of files with grep, and then read files with less. The actual command run is less file1 file2 ...Ex: grep -l hello /etc/* | xargs less grep -l root /etc/* 2>/dev/null | xargs wc The following commands are equivalent:Ex: echo one two three | xargs rm rm ` echo one two three ` rm one two three xargs is often used to handle a list of file names generated by another command. xargs is frequently used with find. find finds the files, and xargs starts up command that handles the file. find regular files that have not been accessed for more than 30 days, and removes themEx: find /tmp -type f -atime +30 | xargs rm find /tmp -type f -atime +30 -print0 | xargs -0 rm