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.
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.
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:
- cat : Display file contents.
- more and less : View large files with navigation.
- head and tail : View the start and end of files.
- grep : Search for patterns in a file or output
Great job getting through this lecture..I'll see you in the next one!
No comments yet. Add the first comment to start the discussion.