Skip to content

Practice Exercise: Exploring Text Manipulation with cat and echo

Objective

Gain hands-on experience with text manipulation in Linux using the cat and echo commands.

Task 1: Viewing File Contents with cat

  • Open a terminal window.
  • Create a new text file named sample.txt using a text editor (e.g., nano or vim) and add some text to it.
  • Use the cat command to view the contents of the sample.txt file.
  • Experiment with options like -n to display line numbers and -b to number non-empty lines only.
    [intern@intern-a1t-inf-lnx1 ~]$ vim sample.txt
    [intern@intern-a1t-inf-lnx1 ~]$ cat sample.txt
    This is not an empty line
    
    Another not empty line
    
    [intern@intern-a1t-inf-lnx1 ~]$ cat sample.txt -n
         1  This is not an empty line
         2
         3  Another not empty line
         4
    [intern@intern-a1t-inf-lnx1 ~]$ cat sample.txt -b
         1  This is not an empty line
    
         2  Another not empty line
    

Task 2: Creating and Appending to Text Files with cat

  • Create a new text file named mynotes.txt using the cat command and add some notes to it directly (e.g., cat > mynotes.txt).
  • Append additional text to the mynotes.txt file using the cat command and the >> operator.
  • Use cat to display the updated contents of the mynotes.txt file.
    [intern@intern-a1t-inf-lnx1 ~]$ cat > mynotest.txt
    This is a test
    Another line
    # Press ctrl + d to exit
    [intern@intern-a1t-inf-lnx1 ~]$ cat mynotest.txt
    This is a test
    Another line
    [intern@intern-a1t-inf-lnx1 ~]$ cat >> mynotest.txt
    Appending another text
    # Press ctrl + d to exit
    [intern@intern-a1t-inf-lnx1 ~]$ cat mynotest.txt
    This is a test
    Another line
    Appending another text
    

Task 3: Using echo for Simple Text Output

  • Open a terminal.
  • Use echo to print a simple message to the screen (e.g., echo "Hello, Linux!").
  • Experiment with escape sequences, such as \n for a new line and \t for a tab.
  • Redirect the output of echo to create a new text file named greeting.txt.
    [intern@intern-a1t-inf-lnx1 ~]$ echo "Hello, Linux!"
    Hello, Linux!
    [intern@intern-a1t-inf-lnx1 ~]$ echo -e "Hello, Linux! \n"
    Hello, Linux!
    
    [intern@intern-a1t-inf-lnx1 ~]$ echo -e "\tHello, Linux! \n"
        Hello, Linux!
    
    [intern@intern-a1t-inf-lnx1 ~]$ echo -e "\tHello, Linux! \n" > greeting.txt
    [intern@intern-a1t-inf-lnx1 ~]$ cat greeting.txt
        Hello, Linux!
    

Task 4: Combining echo and cat

  • Use echo to generate some text (e.g., a list of numbers or names).
  • Pipe the output of echo into a new text file (e.g., echo "1\n2\n3\n4" > numbers.txt).
  • Use cat to view the contents of the numbers.txt file.
  • Practice appending additional text to the same file using echo and the >> operator.
    [intern@intern-a1t-inf-lnx1 ~]$ echo -e "1\n2\n3\n4" > numbers.txt
    [intern@intern-a1t-inf-lnx1 ~]$ cat numbers.txt
    1
    2
    3
    4
    [intern@intern-a1t-inf-lnx1 ~]$ echo -e "\n5" >> numbers.txt
    1
    2
    3
    4
    
    5
    

Task 5: Creating and Using Shell Scripts

  • Create a new shell script file named greet.sh using a text editor.
  • Inside the script, use echo to print a personalized greeting to the screen.
  • Save the script and make it executable using the chmod command.
  • Execute the script to see your custom greeting.
    [intern@intern-a1t-inf-lnx1 ~]$ vim greet.sh
    [intern@intern-a1t-inf-lnx1 ~]$ cat greet.sh
    #!/bin/bash
    
    echo "Hello $USER!"
    [intern@intern-a1t-inf-lnx1 ~]$ chmod +x greet.sh
    [intern@intern-a1t-inf-lnx1 ~]$ ./greet.sh
    Hello intern!
    

Conclusion

In this lab exercise, you've learned how to manipulate text using the cat and echo commands. You've viewed file contents, created and appended text to files, used echo for basic text output, combined echo and cat to create text files, and even created and executed a simple shell script. These skills are fundamental for working with text data in Linux and shell scripting.