Comprehensive Guide to Redirection, Pipelines, and Patterns in Linux Command Line

The Linux
Command Line
Chapter 6
Redirection
Prepared by Dr. Reyes, New York City College of Technology
Outline
Redirection
Pipelines
Patterns
Expansions
Shell Arithmetic
Brace Expansion
Escape
 Characters
Redirection
stdout 
– default output stream
To redirect output from 
stdout 
use >
o
ls -l /usr/bin > ls-output.txt
o
This will create a file named ls-output.txt containing the output of the
command at the left hand side of >
To redirect and append use >>
o
ls -l /usr/bin >> ls-output.txt
o
This will append the output of the command at the left hand side of >> to
the file ls-output.txt, create it if it does not exist.
Redirection
cat
 – reads one or more files and copies them to
standard output
o
Syntax
o
Example
To redirect 
input
 use <
o
Example
Pipelines
Pipelines allows you to redirect the output of one
command and pipe it as input to another
The operator is the vertical bar |
o
Usage: command1 | command2 | … | commandN
o
ls -l /usr/bin 
|
 less
Filters
sort
 – sorts the input
o
ls /bin /usr/bin | 
sort
 | less
uniq
 – removes duplicates
o
ls /bin /usr/bin | sort | 
uniq 
| less
o
ls /bin /usr/bin | sort | 
uniq 
-d | less
wc
 - 
command used to display the number of lines,
words, and bytes contained in files.
o
wc 
ls-output.txt
Patterns
grep
 – a powerful program that allows you to find text patterns
within files. When grep finds the pattern, it prints out the lines
containing it.
o
ls /bin /usr/bin | sort | uniq | 
grep 
zip
head
 - prints the first 10 lines of a file
o
head 
ls-output.txt
o
head 
-n 5 ls-output.txt
tail
 - prints the last 10 lines of a file
o
tail 
ls-output.txt
o
tail 
-n 5 ls-output.txt
o
tail 
-f /var/log/messages
Special command used for real-time monitoring, exit with ctrl+C
tee
 - reads standard input and copies it to both standard output
and to one or more files
o
ls /usr/bin | tee ls.txt
o
ls /usr/bin | tee ls.txt | grep zip
Slide Note
Embed
Share

This comprehensive guide covers the use of redirection, pipelines, filters, and patterns in the Linux command line. Learn how to redirect output, use pipelines to pass output between commands, apply filters like sort and uniq, search for patterns using grep, and more. Gain a deeper understanding of these essential concepts for efficient command line usage.

  • Linux
  • Command Line
  • Redirection
  • Pipelines
  • Patterns

Uploaded on Oct 06, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. The Linux Command Line Chapter 6 Redirection Prepared by Dr. Reyes, New York City College of Technology

  2. Outline Redirection Pipelines Patterns Expansions Shell Arithmetic Brace Expansion Escape Characters

  3. Redirection stdout default output stream To redirect output from stdout use > o ls -l /usr/bin > ls-output.txt o This will create a file named ls-output.txt containing the output of the command at the left hand side of > To redirect and append use >> o ls -l /usr/bin >> ls-output.txt o This will append the output of the command at the left hand side of >> to the file ls-output.txt, create it if it does not exist.

  4. Redirection cat reads one or more files and copies them to standard output o Syntax o Example To redirect input use < o Example

  5. Pipelines Pipelines allows you to redirect the output of one command and pipe it as input to another The operator is the vertical bar | o Usage: command1 | command2 | | commandN o ls -l /usr/bin | less

  6. Filters sort sorts the input o ls /bin /usr/bin | sort | less uniq removes duplicates o ls /bin /usr/bin | sort | uniq | less o ls /bin /usr/bin | sort | uniq -d | less wc - command used to display the number of lines, words, and bytes contained in files. o wc ls-output.txt

  7. Patterns grep a powerful program that allows you to find text patterns within files. When grep finds the pattern, it prints out the lines containing it. o ls /bin /usr/bin | sort | uniq | grep zip head - prints the first 10 lines of a file o head ls-output.txt o head -n 5 ls-output.txt tail - prints the last 10 lines of a file o tail ls-output.txt o tail -n 5 ls-output.txt o tail -f /var/log/messages Special command used for real-time monitoring, exit with ctrl+C tee - reads standard input and copies it to both standard output and to one or more files o ls /usr/bin | tee ls.txt o ls /usr/bin | tee ls.txt | grep zip

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#