Module #06 - The UNIX Way
This module is going to dig further into the CLI usage. We are now going to talk about glue. Nothing to do with uGlue though. We are just going to talk about the manner in which almost all of the good old fashioned Unix tools link easily their inputs and outputs to one another. This is what we refer to as the "UNIX way"; each tool does one thing, does it well, and can be combined with other tools to do much more.
Topics Studied in this Module
- T1 - Redirections
- It is possible, and useful, to direct the text output of a given commandd line tool so that it is accepted as input of another. There "redirections" are part of what makes command line useful to automate tasks.
- T2 - Filters
- For the above to be really leveraged, we need tools which are designed to work line per line on the data fed to them from the keyboard, a file, or another tool output. Enter the filters....
Learning Activities
This module features the following learning activities; DF, PA, GQ
Linux+ Exams Objectives
This module deals with material related to the following objectives from the CompTIA Linux+ and LPI certifications exams.
- Exam LX0-101 - Objective 103.4 - Use streams, pipes & redirects
To this end, we are going to use one of Ryan Chadwick Linux tutorials. We will specify which sections are assigned to read for each of the topics below.
T1 - Redirections
The first topic for this module will introduce the syntax we need to redirect the output and input of arbitrary processes, e.g. tools or script. This will enable us to use files interchangeably as source or destinations for the input and output of processes. this will also enable us to connect multiple tools with one another in a "data pipeline" where the output of a tool will be used as the input of the next.
- Reading Assignments
- URL Link: Ryans Chadwick's Linux Tutorial
- The following section(s) of the tutorial are assigned for this module;
Once you have studied the above sections of the tutorial, watch the videos below to get an idea of how these techniques are applied.
- Simple Redirections
- Video Link: YouTube
- We start with the simple redirections allowing us to replace STDIN, STDOUT and STDERR, which are by default respectively mapped to the keyboard and the terminal screen, by files.
- Appending instead of Overwritting
- Video Link: YouTube
- The simple redirections assume that we want to redirect to a new file the output of a process. In this video, we learn how to redirect multiple times to the same file without loosing the contents which have been dumped into it during our previous redirections.
- Merging the Streams
- Video Link: YouTube
- Sometimes, you want both STDOUT & STDERR to end up in the same file. This video explore the syntax to merge two output streams into the same target, along with bugs which most Linux users bump into when first experimenting with such advanced redirections techniques.
- Swapping STDOUT with STDERR
- Video Link: YouTube
- This is an extension of the previous video where we apply what we have learned to an interesting use-case.
- Piping
- Video Link: YouTube
- Now we are ready to leverage what we have learned so far in order to assemble "data pipelines" where multiple simple tools each take their data from STDIN and produce their results on their STDOUT which is then linked to the STDIN of the next process in the chain which needs to process this data
T2 - Filters
Now that you have seen what redirections have to offer to the Linux user, let us turn our attention to the tools which have been designed from the get go to be leveraged in pipelines; i.e. filters.
- Reading Assignments
- URL Link: Ryans Chadwick's Linux Tutorial
- The following section(s) of the tutorial are assigned for this module;
Once you have studied the above sections of the tutorial, watch the videos below to get an idea of how these techniques are applied hands-on, get more details about some of the filters mentioned in the above-tutorial, and discover new ones.
- cut & paste
- Video Link: YouTube Readings: ManPage for cut Readings: ManPage for paste
- Exciting news! We are going to study cut and paste! On the plus side, we study a way-cooler version than just using keyboard shortcuts in a text editor :)
The Discussion Forum for this module will require you to install some software on your Ubuntu virtual machine, test it thoroughly, and post a review for other students.
Topic - Review of Linux Multimedia Software
For this DF assignment, we will focus on multimedia software.
The following are suggested software packages to test, pick one that you will be able to use this week in order to establish whether it would be a suitable replacement for the tools you are using right now on other operating systems.
- The Gimp as replacement for photoshop or similar pictures editting software
- SMPlayer as replacement for another media player
- VLC as replacement for another media player
- VLC as replacement for screen-casting software
- DarkTable as replacement for Digital Camera management
- DigiKam as replacement for Digital Camera management
- Fotoxx as replacement for Digital Camera management
- Audacity as replacement for audio editting software
- Amarok as replacement for music players
- ...
Requirements for your Posts
As you download and install one of these tools, post a new thread with its name as title. Your review should address the following question with cut and past of actual commands or screenshots;
- Installation with a package manager of your preference
- Features availables
- Demonstration of an interesting and non-trivial feature
Participation credit will be given based on the effort and level of details you put into your review. Minimal effort will earn you minimal grades.
The following exercises will help you practice piping, redirections and a few of the filters we discussed in the study material.
Exercise #1 - Timestamps
We want to write a command line that allows us to append information about the current time/date into a text file.
We are going to use the date tool and redirections to append the current date and time into a text file named timestamps.log which we will assume is in the working directory. We will not be assuming the file exists but we will make sure that if we use our solution multiple times, then new lines are appended at the end of the file without overwriting previous information.
Submit a screenshot of the command you created, and do a ls to indicate that the file was created successfully by the command. As usual, make sure your full name appears in the terminal for your screenshots.
Exercise #2 - Improved Timestamps
Let us get fancy with the format with which the information will be recorded on each line of our text file; Instead of just having the date, we need to have the following information on each line;
YEAR MONTH DATE DAY HH:MM:SS
An example of an entry would be;
2011 February 18 Friday 19:42:00
Hint: man date is your friend
Submit a screenshot showing the command that you used, as well as the contents of the file after running the command a few times. As usual, make sure your full name appears in the terminal for your screenshots.
Exercise #3 - Even Better Timestamps
What about improving our previous command line so that we are able to also append the user’s name at the end of the line. The previous entry would now read instead;
2011 February 18 Friday 19:42:00 smith
Submit a screenshot showing the command that you used, as well as the contents of the file after running the command a few times. As usual, make sure your full name appears in the terminal for your screenshots.
Exercise #4 - Joining with other files
We want to use join to combine our timestamps.log file with another file that is simply listing a bunch of user names on our system (not necessarily all of them), followed by a space, and then the full name.
Let us call this file usernames.log. A typical entry would look like;
mulder Fox Mulder scully Dana Scully murph Murphy Cooper dale Dale Cooper
Start by sorting both files based on the user names and thus create two new versions named timestamps-sorted.log and usernames-sorted.log.
Once you have these, use them in your join operation and dump the result in a file named result-sorted.log.
The end result of our combination should show all lines from timestamps.log with, at the end, the full name of the user (when available). Please note the following;
- The order will not be chronologic anymore
- join will put the username first on each line
Make sure that you test your command thoroughly to ensure that it accomplishes the above functionalit. Submit a screenshot of the command you used, as well as the contents of your files. As usual, make sure your full name appears in the terminal for your screenshots.
This tab provides you with optional resources meant for those who want to learn a bit more about the topics covered in this module.
Harley Hahn's Guide to UNIX and Linux
If you are interested in learning more about the Linux File System, I recommend you look for Harley Hahn's Guide to UNIX and Linux. It is unfortunately out of print but is a great (and entertaining) read about the topic.
The following sections of the above are particularly relevant to this module;
- Section 15: Standard I/O, Redirections and Pipes
- Section 16: Filters: Introduction and Basic Operations
- Section 17: Filters Comparing & Extracting
- Section 19: Filters: Selecting, Sorting, Comparing and Changing