module 6 – redirections, pipes and power tools.. stdin 0 stdout 1 stderr 2 redirections

23
Module 6 – Redirections, Pipes and Power Tools.

Upload: abraham-hudson

Post on 16-Jan-2016

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

Module 6 – Redirections, Pipes and Power Tools.

Page 2: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

•STDin 0•STDout 1•STDerr 2

Redirections

Page 3: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

•Capture STDout with 1> or simply >•Append STDout with >>

# ls > list.txt

Redirections

Page 4: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

•Capture STDerr with 2>•Append STDerr with 2>>

# ls bogos.txt 2> errors.txt

Redirections

Page 5: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

Merging Standard Output With Standard Error

•Merge standard output and standard error in to the same file with 2>&1 – binding the streams together

# ls ort.txt bogos.txt 2>&1 both.txt

Page 6: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

Regular Expressions (RegEx)

. Matches any single character

[list] Matches any single character in the list

[range] Matches any single character in range

[^ ] Matches any single character not in list or range

* Matches previous character 0 or more times

\{n\} Matches previous character n times

\{n,\} Matches previous character at least n times

\{n,m\} Matches previous character between n and m times

^ Matches regular expressions at the start of the line only

$ Matches regular expressions at the end of the line only

\ Quote recognition of special characters

Page 7: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• A pipe ( | ) takes the output of the command and passes it as an input into the following command.

• A pipe will continue to pass STDout even if there is an error. he will simply drop STDerr

# ls -l /etc | wc -l

Pipes

Page 8: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines.

# grep ro /etc/passwd

Or

# cat /etc/passwd | grep ro

-i, --ignore-case

# cat /etc/httpd/conf/httpd.conf | grep -i server

The grep command

Page 9: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

Combining grep with regex

# ls -l /etc | grep "^d“

# ls -l /etc | grep "d$“

# grep '50[0-9]' /etc/passwd

# ifconfig | grep -o '[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]‘

# ifconfig | grep -o '[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}'

Page 10: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• The cut command can cut fields.• We need to define the delimiter and we need to tell

the cut command which field to cut:

# cat /etc/passwd | cut –d”:” -f1

!!! Don’t forget to sort the spaces for cut with the tr command.

The cut command

Page 11: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• The cut command can cut fields.

# ls -l /tmp | cut –d” ” -f1,5

!!! Don’t forget to sort the spaces for cut with the tr command.

The cut command

Page 12: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

The tr command

• The tr is used to squeeze multiple spaces into one space, helping the cut command in cutting the rows:

# ls -l | tr -s “ “ | cut -d” “ –f1,4

!!! note

the tr command “need” a space after the –s option

Page 13: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• By default the sort command sort files in alphabetical order.

• We can sort by numbers with -n

# ls -l /etc | sort

!!! The sort command usually comes before the uniq command.

The sort command

Page 14: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• The uniq command can identify uniq entries, duplicate entries and can also count those entries.

# uniq

# uniq -d (duplicates)

# uniq -dc (duplicates and count)

The uniq command

Page 15: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• Although awk is a complete pattern scanning and processing language, it is most commonly used as a Unix command-line filter to reformat the output of other commands.

• Basic awk example:

# cat /etc/passwd | awk ‘{print $1}’

awk

Page 16: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• sed is a steam editor • We used sed earlier when we learned Search&Replace in VI.

Remember VI ?

:1,$s/old/new/g

And in the shell:

# sed –i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

# sed -n '/ort/p' passwd > ort.txt

sed

Page 17: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• A process is considered to have completed correctly in Linux if its exit status was 0.

• Any other result greater then 0 is considered an error.

To find the exit status:

# echo $?

Unix/Linux Exit Status

Page 18: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

• ; Run multiple commands in one run, sequentially.

# ls ; echo hello ; tail -2 /etc/passwd

• & Sends process background(run in background paralleling )

# ./script.sh &

• && What the double-ampersands are telling the system is "wait for the first command to successfully complete. If it does complete successfully, run the next command".

Chaining Commands

Page 19: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

Search Commands

• updatedb + locate

• which and whereis

• The find command (as root)

# find / -name passwd

Page 20: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

Exercise 1. Use redirection to save the ls command to a file.

2. Generate an error with ls and save the errors in a file

3. Generate both “good” STDout and “bad” STDerr and save them both in one file.

4. Count the directories under /home

5. Count all the users in the system

6. print only the permissions and the file-name fields using the cut command

7. Use awk to print the first and last field from /etc/passwd

8. use the find command to search for all files and directories with the name of passwd

Page 21: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

Exercise 9. Log-in to the instructor server and find how many

users are logged-in more then once and how many times are those users logged-in.

10.Where is the binary of the fdisk tool ?

11. find all the files and directories in the system with full permissions (777)

12.Find all the files in the system bigger then 2MB

13.Use the df command to print only the directory name (mount point) and the size

14. Use the fdisk tool to print only the disks without the partitions.

15.Copy /etc/passwd to your home dir and replace the shell from bash to sh

Page 22: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections

<Insert Picture Here>

Page 23: Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections