Level 1
0 / 100 XP

Viewing Files

Reading File Contents

cat (Concatenate and Display)

The cat command is used to display the contents of a file. It can be used to view the entire content of a file at once.

cat filename.txt

more and less (Pager Commands)

more and less are pager commands that allow you to view large files one screen at a time. They provide navigation options for reading through files.

Text
more filename.txt less filename.txt

head and tail (View Start/End of Files)

head and tail are used to view the beginning and end of files, respectively. They are often used to display a portion of a file.

Text
head -n 10 filename.txt # Display the first 10 lines tail -n 20 filename.txt # Display the last 20 lines

Adding the -f option will cause the tail command continuously displays the latest content of a file in real-time. It's particularly useful for monitoring log files as they receive new entries.

tail -f /var/log/syslog

Press Ctrl + C to exit the command.

grep (Search Text in Files)

The grep command is used to search for specific text patterns in files. It is a powerful tool for text-based searching.

grep "pattern" filename.txt

Below is an example of searching the syslog file in /var/log for all events that happened at 14:17:

grep "14:17:" /var/log/syslog

In this lesson, you learned how to manipulate files on a Linux/Unix system, covering essential commands for creating, reading, and modifying files. We covered a lot of commands so this is a short recap of what we covered:

  1. cat : Display file contents.
  2. more and less : View large files with navigation.
  3. head and tail : View the start and end of files.
  4. grep : Search for patterns in a file or output

Great job getting through this lecture..I'll see you in the next one!