Buffl

Viewing Text and Redirecting Input/Output

as
by abdullah S.

Part 1: Text Viewing Commands


1. more

  • Purpose: File perusal filter for viewing text one screen at a time

  • Usage: more [filename]

  • Navigation:

    • Enter: Move line-by-line

    • Spacebar: Move page-by-page

    • q: Quit

  • Limitation: Older command, cannot scroll backwards

  • Example: more /var/log/messages

2. less

  • Purpose: Improved version of more with more features

  • Usage: less [filename]

  • Navigation:

    • Arrow keys: Up/down line-by-line

    • Page Up/Down: Move by pages

    • Enter/Spacebar: Forward movement

    • /pattern: Search forward

    • n: Next search result

    • Shift+N: Previous search result

    • q: Quit

  • Key advantage: Can scroll backwards, better searching

  • Remember: "Less is more" (less provides MORE features than more!)

  • Example: less /var/log/messages

3. head

  • Purpose: Display the first lines of a file

  • Default: Shows first 10 lines

  • Usage: head [filename]

  • Options:

    • -n [number]: Specify number of lines

  • Examples:

    • head /var/log/messages (shows first 10 lines)

    • head -n 20 /var/log/messages (shows first 20 lines)

4. tail

  • Purpose: Display the last lines of a file

  • Default: Shows last 10 lines

  • Usage: tail [filename]

  • Options:

    • -n [number]: Specify number of lines

    • -f: Follow mode - continuously display new lines as they're added (perfect for log files!)

  • Examples:

    • tail /var/log/messages (shows last 10 lines)

    • tail -n 2 /var/log/messages (shows last 2 lines)

    • tail -f /var/log/messages (follows log in real-time)

  • Exit follow mode: Ctrl+C

  • Use case: Monitoring live logs during troubleshooting

5. grep (Global Regular Expression Print)

  • Purpose: Search for and print lines matching a pattern/string

  • Usage: grep [options] [pattern] [file]

  • Important Options:

    • -i: Case-insensitive search

    • -R or -r: Recursive search through directories

  • Examples:

    • grep "cloud_user" /var/log/messages (find exact string)

    • grep -i "joe" names.txt (case-insensitive search for "joe", "JOE", "Joe", etc.)

    • grep -R "cloud_user" /var/log/ (search all files in directory recursively)

  • Key feature: Invaluable for finding configurations, troubleshooting, searching logs

6. sort

  • Purpose: Sort lines of text files

  • Default: Ascending alphabetical/character order

  • Usage: sort [options] [file]

  • Important Options:

    • -n: Sort numerically (not just by character)

    • -r: Reverse order (descending)

    • -o [filename]: Output to file

  • Examples:

    • sort names.txt (alphabetical sort)

    • sort -n numbers.txt (numerical sort: 14, 22, 100)

    • sort -nr numbers.txt (reverse numerical: 9000, 100, 22, 14)

    • sort names.txt numbers.txt (sorts multiple files together)

  • Note: Character sort vs numerical sort

    • Character: 100, 14, 22, 9000 (compares character by character)

    • Numerical: 14, 22, 100, 9000 (compares actual values)


Part 3: Input/Output Redirection


Redirection allows you to change where input comes from and where output goes (instead of keyboard/screen defaults).

Output Redirection

Single > (Redirect and Overwrite)

  • Symbol: >

  • Purpose: Send stdout to a file (overwrites existing content)

  • Syntax: command > file

  • Examples:

    • sort names.txt > sorted.txt (saves sorted output to file)

    • ls > filelist.txt (saves directory listing to file)

  • Warning: Overwrites file if it exists!

Double >> (Redirect and Append)

  • Symbol: >>

  • Purpose: Send stdout to a file (appends to existing content)

  • Syntax: command >> file

  • Examples:

    • sort numbers.txt >> sorted.txt (adds to existing file)

    • echo "new line" >> log.txt (appends text to file)

  • Safe: Doesn't overwrite, adds to end of file

Input Redirection

Single < (Input from File)

  • Symbol: <

  • Purpose: Feed file contents as input to command

  • Syntax: command < file

  • Example:

    • sort < numbers.txt (uses file as input)

    • sort < numbers.txt > sorted.txt (input from file, output to file)

Double << (Here Document)

  • Symbol: <<

  • Purpose: Feed multi-line input directly to command

  • Syntax: command << DELIMITER

  • Example:

sort << EOF

50

22

3

17

40

EOF

  • Type input until you type the delimiter (EOF)

  • Delimiter can be any word (EOF is common)

rror Redirection

Redirect stderr Only

  • Symbol: 2>

  • Purpose: Redirect only error messages (not regular output)

  • Syntax: command 2> errorfile

  • Examples:

    • Sort numbers.txt 2> error.log (captures "command not found" error)

    • ls /baddir 2> errors.txt (captures "no such directory" error)

Redirect stderr and Append

  • Symbol: 2>>

  • Purpose: Append error messages to file

  • Syntax: command 2>> errorfile

Merging stdout and stderr

Method 1: 2>&1

  • Symbol: 2>&1

  • Purpose: Makes stderr go to same place as stdout

  • Syntax: command > file 2>&1

  • Explanation:

    • > file redirects stdout to file

    • 2>&1 makes stderr (2) go to same target as stdout (1)

  • Example: ls /home /root /baddir > merge.log 2>&1

Method 2: &>

  • Symbol: &>

  • Purpose: Redirect both stdout and stderr to file (shorthand)

  • Syntax: command &> file

  • Example: ls /home /root /baddir &> merge.log

  • Note: Simpler syntax, same result as method 1




Author

abdullah S.

Information

Last changed