Linux Text Management Commands

Common text management commands in Linux:

  1. cat: View, concatenate, and create files
    grep: Search for text content
    echo: Output text content
    tail: View the end of a file
    sed: Stream editor, used for text substitution and modification
    vi: Text editor, used for creating and editing files

Detailed Introduction to Text Management Commands

  1. cat (concatenate) command:

    • Used to view, concatenate, and create files
    • Syntax: cat [options] [file(s)]
    • Common options:
      • -n: Display line numbers
      • -E: Display end-of-line characters
    • Example: Merge file1.txt and file2.txt into combined_file.txt. cat file1.txt file2.txt > combined_file.txt
  2. grep (global regular expression print) command:

    • Used to search for text content
    • Syntax: grep [options] 'pattern' [file(s)]
    • Common options:
      • -i: Ignore case
      • -r: Recursively search directories
    • Example: Search for lines containing "error" (case-insensitive) in log_file.txt. grep -i "error" log_file.txt
  3. echo command:

    • Used to output text content
    • Syntax: echo [options] [string]
    • Common options:
      • -n: Output without a trailing newline
    • Example: Output "Hello, World!". echo "Hello, World!"
  4. tail command:

    • Used to view the end of a file
    • Syntax: tail [options] [file]
    • Common options:
      • -n: Display the last n lines
      • -f: Continuously monitor file changes
    • Example: View the last 5 lines of log_file.txt. tail -n 5 log_file.txt
  5. sed (stream editor) command:

    • Used for text substitution and modification
    • Syntax: sed [options] 'commands' [file(s)]
    • Common options:
      • -i: Modify the original file in place
    • Example: Replace all occurrences of "old_string" with "new_string" in file.txt. sed 's/old_string/new_string/g' file.txt
  6. vi (visual editor) command:

    • Used for creating and editing files
    • Basic operation modes:
      • Command mode: Input commands
      • Insert mode: Edit text
      • Last line mode: Save and exit
    • Example: Open file.txt for editing. vi file.txt