OO Programming and Data Structures | CS 241

13 Prepare : Checkpoint A - Linux Commands

After completing (or while completing) the first section of the preparation material for this week, complete the following exercise.

Overview

This checkpoint is intended to help you practice the syntax of I/O redirection in Linux. Please read the following Learning the Shell with particular focus on the I/O Redirection section.

Instructions

For this assignment, you will not write and submit a program in Python. Instead, you need to practice using Linux shell commands to complete each of the following tasks. Then, report on your progress via the accompanying I-Learn quiz. For each task, check off if you were able to complete the command.

Hint: Be careful. It is easy to erase or ruin files that are important with a simple Linux command. I recommend creating a new empty directory and changing to that directory before practicing these commands.

  1. Echo to a file. From the command line, create a new file named hello.txt, that has the text, "Hello CS 241 World".

  2. "Cat" a file. Display the contents of your hello.txt file by using the "cat" command to display it to the screen.

  3. Append to a file. From the command line, append the text "More text here..." to your hello.txt file. Run your command three times and verify the output.

  4. Directory listing to a file. List out all the files and sub-directories in /home, and save this result to a file named: homeDirs.txt

  5. Get the first 20 lines. Use the "head" command to get the first 20 lines of the homeDirs.txt file and put them in a new file named: homeDirs_20.txt

  6. Search files. Use the "grep" command to search through all the .py files in your home directory (or in a cs241 subdirectory) and display all the files that use the term "format".

  7. Less. Repeat the grep command from the last step, but this time, pipe the result to "less" so you can scroll through it.

  8. Grep, Tail, and Redirect. Repeat the same grep command, but this time, get only the last 5 lines of the result and save them to a file. This means you'll want to pipe the result to "tail" and then redirect the output to a file.