Difference between revisions of "CSC231 Bash Tutorial 4"
Line 105: | Line 105: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | =Redirecting Stderr= | ||
+ | <br /> | ||
+ | * Run the '''du''' command another time, and pay attention to the lines that contain a "Permission denied" message: | ||
+ | |||
+ | du /etc | ||
+ | |||
+ | :When I run it, I get these messages interspersed here and there: | ||
+ | |||
+ | 28 /etc/pm | ||
+ | 4 /etc/ndiswrapper | ||
+ | 8 /etc/rc4.d | ||
+ | 4 /etc/mate-settings-daemon/xrandr | ||
+ | 8 /etc/mate-settings-daemon | ||
+ | du: <font color="magenta">cannot read directory ‘/etc/chatscripts’: Permission denied</font> | ||
+ | 4 /etc/chatscripts | ||
+ | 28088 /etc | ||
Line 118: | Line 134: | ||
<br /> | <br /> | ||
<showafterdate after="2017024 11:30" before="20170601 00:00"> | <showafterdate after="2017024 11:30" before="20170601 00:00"> | ||
+ | =Challenges Solutions= | ||
+ | <br /> | ||
;Challenge 1 | ;Challenge 1 | ||
Revision as of 16:34, 22 February 2017
--D. Thiebaut (talk) 14:13, 22 February 2017 (EST)
<showafterdate after="20170224 13:00" before="20170601 00:00">
Contents
Using the Vi Editor
Redirection
- We have seen in the previous lab how to use pipes to take the output of a program and feed it to the input of another.
- Redirection is similar, except that the input or the output is fed from, or fed to a file, respectively.
Example 1: storing the output of a command into a file
- Try this:
last
- this will list the last login times of all the users on Aurora. If you want to target a particular user, say 231b, you can try this:
last | grep "231b " (make sure there's a space after the b)
- Assume you want to store these login time into a file, so that you can share it with somebody else, or send as an attachment. You simply use the > sign to store the output of the last command into a file:
last | grep "231b " > logins231b.txt
- You won't see any output, as it is being redirected. Look at the files in your directory and see that you have a new file called logins231b.txt.
- Display the contents of this file with the cat or with the less command.
You now have a new way of storing the output of a program into a file.
Challenge #1: |
Create a text file called "231Students.txt" that contains their last login information, but make sure the information from User 231b (your instructor) is not stored in the file.
Side-step: Sorting text
Linux sports a command to sort text. It's called "sort." To see how it works, try this:
last | sort
- If you had wanted the output in reverse order, you could have typed:
last | sort -r
- Sometimes the information returned by a linux command contains numbers at the beginning of the first line. du, which returns the "disk usage" of a directory, i.e. the amount of disk space taken by each file, is such a command. Let's try it:
du /etc
- You get a list of all the files in the /etc directory, with their size at the beginning of each line.
- Try to sort this list:
du /etc | sort
- See the problem? Sorting numbers alphabetically does not yield what we expect. Instead we can tell sort to sort numerically:
du /etc | sort -n
Challenge #2: |
Create a text file called "231Students.txt" using redirection, and make it contain the login information for 231 students, sorted in alphabetical order, but make sure the information from User 231b (your instructor) is not stored in the file.
Challenge #3: |
Create a text file using redirection that contains only the 10 largest files found in the /etc/ directory. The file should be called top10etcFiles.txt. Be careful that the last line of the output of sort is the total of all the files, and shouldn't be included in the top10etcFiles.txt file.
Redirecting Stderr
- Run the du command another time, and pay attention to the lines that contain a "Permission denied" message:
du /etc
- When I run it, I get these messages interspersed here and there:
28 /etc/pm 4 /etc/ndiswrapper 8 /etc/rc4.d 4 /etc/mate-settings-daemon/xrandr 8 /etc/mate-settings-daemon du: cannot read directory ‘/etc/chatscripts’: Permission denied 4 /etc/chatscripts 28088 /etc
</showafterdate>
<showafterdate after="2017024 11:30" before="20170601 00:00">
Challenges Solutions
- Challenge 1
last | grep "231b" | grep -v "231b " > 231students.txt
- Challenge 2
last | grep "231b" | grep -v "231b " | sort > 231students.txt
- Challenge 3
du /etc | sort -n | tail -n 11 | head -n 10 > top10filesInEtc.txt
</showafterdate>