Worksheet 3 - Redirection commands

From Sensors in Schools
Jump to navigation Jump to search

cat command (>, >>, <, <<)

Learn shell command behavior, and understand the power of redirection:

Create a new folder /home/pi/redirection so that you can practise the examples in this worksheet.

1. Creating a Simple File Using >

Activity: Introduce students to using cat with > to create a new file or overwrite an existing file.

Challenge: Have students create a file with some text and observe what happens when they run the command again with different content. Ask them to explain how > works by overwriting the existing file.

cat > ~/redirection/test_file.txt << EOF
This is the first line.
EOF
  • >: This is a redirection operator in bash. It redirects the output of the command (in this case, cat) to a file. Here, it tells the shell to create or overwrite the file ~/redirection/test_file.txt with the content that follows.
  • << EOF: This is known as a "here document." It allows you to provide multiline input directly in the terminal. Everything between << EOF and the closing EOF is treated as input to the cat command. The shell will continue reading input until it encounters the string EOF on a line by itself, at which point it stops and processes the input.

2. Appending to a File Using >>

Activity: Teach students how to use cat with >> to append content to an existing file instead of overwriting it.

Challenge: First create a file using cat >, then append additional lines using cat >>. Ask students to check the content of the file using cat ~/redirection/test_file.txt after each operation to confirm that the content is being appended.

cat >> ~/redirection/test_file.txt << EOF
This is an additional line.
EOF

3. Input Redirection Using <

Activity: Show students how to use input redirection (<) to feed the content of one file into a command. This is useful when working with files and input streams.

Challenge: Create a file with some text, then use the < operator to print its content using a command like cat.

cat < ~/redirection/test_file.txt

4. Redirecting Command Output to Multiple Files Using tee

Activity: Introduce the tee command, which splits output to multiple files or files and the terminal at the same time. Teach students how this can be combined with redirection.

Challenge: Use tee to write output to a file while also displaying the output in the terminal. Use both the > and >> versions to show the difference between overwrite and append modes.

echo "Hello World!" | tee ~/redirection/test_file.txt
echo "Appending..." | tee -a ~/redirection/test_file.txt


5. Using << (Heredoc) to Input Multiple Lines into a File

Activity: Show students how to use << to create a heredoc, which allows the input of multiple lines into a file.

Challenge: Create a file using heredoc with multiple lines and then ask students to check if the content was written correctly. Explain how the heredoc works with a custom delimiter (EOF or any other word).

cat > ~/redirection/test_file.txt << EOF
Line 1: This is a test.
Line 2: Writing multiple lines.
EOF

6. Input and Output Redirection Together (< and >)

Activity: Teach students how to combine input (<) and output (>) redirection to read from one file and write to another.

Challenge: Create a file with some content, then copy the content to a new file using a combination of redirection operators.

cat < ~/redirection/test_file.txt > ~/redirection/new_test_file.txt

7. Appending File Content Using a Loop and >>

Activity: Teach students how to append data from multiple files into one using a loop and >>. This can be useful for combining logs or other data files.

Challenge: Create several files and write a script to loop through them, appending their content to one final file.

for file in file1.txt file2.txt file3.txt; do
  cat $file >> combined.txt
done

8. Using cat with /proc Filesystem

Activity: Show students how cat can be used to view system information from special files in /proc.

Challenge: Have students use cat to check CPU information from /proc/cpuinfo and memory information from /proc/meminfo. Ask them to explain the output.

cat /proc/cpuinfo
cat /proc/meminfo

9. Input Redirection to a Command Using Pipes (|)

Activity: Teach students how to use pipes (|) to redirect the output of one command to another, much like input redirection.

Challenge: Use cat to output the contents of a file, then pass the output to another command like grep to search for a word in the file.

cat ~/redirection/test_file.txt | grep "word"

10. Combining cat, sort, and Redirection

Activity: Teach students how to combine cat and sort commands using redirection to sort the contents of a file.

Challenge: Create a file with unsorted content, then use cat and sort to sort the content and output it to a new file.

cat ~/redirection/test_file.txt | sort > ~/redirection/sorted_test_file.txt