10 Ways To View Logs In Linux/Unix 

View Logs

Log viewing is one of the most frequently used command lines in our daily use of Linux/Unix systems. Do you know how to efficiently query logs?

1. Use the cat command to output all log content.

This method is suitable for situations where the log file is small and has a limited number of lines. It is not suitable for large files with many lines.

2. When a keyword is known, use the grep command to search.

This is the most commonly used method in our daily work, which allows for a quick search based on the keyword in the log. However, this method also has a drawback: it may retrieve a large amount of unexpected content, which requires the use of the following method.

3. When a keyword is known and there is a lot of confusing content, use the grep command to search and use grep -v to filter out the confusing content.

This method allows us to more accurately find the content we want to search for and reduce the interference of unexpected content.

4. When we know a certain keyword exists but are unsure in which file or directory, use grep -R.

In this situation, we can also use the grep command with the -R option to search all files and directories under the current directory.

5. When we need to search for a known keyword in a specific type of file, we can use grep -R with wildcards.

When we need to find all files of a certain type that contain a specific keyword in the current directory, we can use the grep command combined with wildcards and pipes.

6. View a fixed number of lines at the beginning of the log file with head -n.

When we need to display the content at the beginning of the file, we can use the head command, which can return a specified number of lines from the beginning of the file.

7. View a fixed number of lines at the end of the log file with tail -n.

Corresponding to head, using the tail command allows us to view a specified number of lines from the end of the file.

8. View real-time logs with tail -f.

When we need to view logs in real time, we can use the tail command with the -f option, which can display the content being written to the log file in real time.

9. Query real-time logs with tail -f and only display content with specific keywords.

In this case, we need to use the tail -f command and use pipes and the grep command to display the lines with keywords that are being written to the log file in real time.

10. Translate the log viewing commands more and less.

Both commands support page-by-page viewing, with less being more powerful, supporting forward, backward, and search functions.

99. Query the commands used with history.

We can use the history command to find the commands we have used in the past; at the same time, we can also use the grep command in combination to find the specified commands that have been used.

    This entry was posted in Grep Command, File & Directory and tagged , , , . Bookmark the permalink.