How to View Logs in Linux

1. cat Command:

     Function: 1) Display the entire file.

                    Example: $ cat fileName

              2) Concatenate files and send to standard output, such as merging several files into one or outputting to the screen.

                   Example: $ cat file1 file2 > file

     Description: Concatenate files and send to standard output (screen or redirect with > fileName to another file)
     Detailed cat Parameters:
     -n or –number Number all output lines starting from 1
     -b or –number-nonblank Similar to -n, but does not number blank lines
     -s or –squeeze-blank Replace consecutive blank lines with a single blank line
     -v or –show-nonprinting

2. more Command:

     View logs in a percentage-based view.    

 

3. less Command:

     Functionality is similar to more, except less supports scrolling both forward and backward through a file.

 

4. head Command:

     Function: View from the beginning of a text file. The head command is used to view the beginning portion of a text file.

     Examples:
     head example.txt Displays the first ten lines of the file example.txt;
     head -n 20 example.txt Displays the first twenty lines of the file example.txt;
     Detailed head Explanation:
     -n      Specify the number of lines you want to display.
     -n number     This option must be a decimal integer, determining the position in the file by line.
     -c number     This option must be a decimal integer, determining the position in the file by byte.

5. tail Command:

     Function: The tail command is used to display the last few lines of a text file.

     Examples:

     tail example.txt Displays the last ten lines of the file example.txt;
     tail -n 20 example.txt Displays the last twenty lines of the file example.txt;
     tail -f example.txt Displays the last ten lines of the file example.txt and automatically displays new lines as they are added to the file.

     tail -n 50 -f example.txt Displays the last 50 lines of the file example.txt and automatically displays new lines as they are added to the file.
     Note:
     The last command is very useful, especially when monitoring log files, as it continuously displays newly added log information on the screen.

     Detailed tail Explanation:
     -b Number Starts reading the specified file from the 512-byte block position indicated by the Number variable. 
     -c Number Starts reading the specified file from the byte position indicated by the Number variable. 
     -f If the input file is a regular file or if the File parameter specifies a FIFO (First-In, First-Out),
     then the tail command does not terminate after copying the last specified unit of the input file, but continues
     to read and copy additional units from the input file as they become available. If no File parameter is specified,
     and standard input is a pipe, the -f flag is ignored. The tail -f command can be used to monitor the growth of a file being written by another process. 
     -k Number Starts reading the specified file from the 1KB block position indicated by the Number variable. 
     -m Number Starts reading the specified file from the multi-byte character position indicated by the Number variable. Use this flag to provide consistent results in single-byte and double-byte character code set environments. 
      -n Number Reads the specified file from the beginning or end line position, indicated by the sign (+ or – or none) of the Number variable, offset by the line number Number. 
       -r Displays output in reverse order from the end of the file. The default for the -r flag is to display the entire file in reverse. If the file is larger than 20,480 bytes, the -r flag displays only the last 20,480 bytes. The -r flag is only
   valid when used with the -n flag. Otherwise, it is ignored.

Leave a Comment

Your email address will not be published.