As a backend developer, viewing file contents in Linux is basic. In particular, log files are often analyzed to troubleshoot problems, so how do we open log files correctly? For me, the first response is cat, tail, vi (or vim), and yes, I’ve used the Vim editor many times to look at log files.
Never use the VI command to view the contents of large files, especially those tens of gigabytes. Since vi is only an editor (a notepad in Windows), using the vi command will load all the contents of the file into memory. If the memory is not large enough, the server may crash.
In order to generate test data, the author put together a shell script to generate test files, which is convenient for the command demonstration below, and can be copied to the Linux command line for execution.
#Generate 10 lines of test data (modify as required)
for ((i=1; i <= 10; i++));
do
Echo "$I line" >> test.txt
if [[ `expr $i % 2` -eq 0 ]]
then
echo -e >> test.txt
fi
done
Copy the code
View file contents directly
There are three commands to view the contents of the entire file: cat/tac/nl.
cat [-AbEnTv]
Options and parameters: -a: lists special characters instead of whitespace. -b: lists line numbers, and displays non-whitespace lines without marking. -e: displays the end newline character $. Print the travel number, which also has a line number along with blank lines, unlike the -b option. -t: displays the [TAB] key as ^I. -V: lists special characters that are not visible
Example 1: View the contents of the test.txt file
cat test.txt
Example 2: View the contents of the test.txt file and display the line numbers
cat -n test.txt
Example 3: Using CAT to view large files is not recommended
cat -n test.txt
Cat is only good for viewing files with a small number of lines. If the file is large, it doesn’t make much sense and the file will quickly scroll to the last line. If there are special symbols in the file, such as [Tab], newline, etc., to display, you must add options like -a. Of course, CAT can also be used with more or less through the pipe character to achieve better results.
Tac (in reverse order of CAT printing)
After learning TAC, the author has never applied it in practice. Because it is seldom used, it is enough for everyone to know. This command is interesting, however, because it is spelled the opposite of cat, so they print the last line as the first line in reverse order. Example 1: View the contents of the file backwards
Page to view
more
More command to understand the line, too few functions, I generally use less command.
Key/command Space: Scroll down Enter: Scroll down Line/String: Scroll down the string in the current display (page progress position) Keyword :f: display the file name and line number of the current position immediately Q: : Exit the browsing of the current file b or CTRL + B: Page back Example 1: View the line number after turning the line
less
The less command is more flexible than the more command, allowing you to scroll back and forth and search not only up but also down. Key/command [pagedown] : pagedown [pageup] : pageup/string: in the current display (page turning progress position), look down the string keyword? String: Lookup up string n: repeat previous lookup, with/or? For example, what was the previous command? In this case, n will search up. N: the previous search is repeated in reverse. G: to jump to the first line of the current file data
Examples demonstrate
Data capture
head
Using the head command, you can extract the first n lines of a file. Generally, the -n option is used. When the specified number of rows is negative -x, all data is printed except for the x rows that follow. Example 1: Look at the first 10 rows
Example 2 (total 10000 lines, no blank lines): head-n-9989 test.txt
tail
Intercepts data from the end of the file. Tail is also the most commonly used command at work because you can always refresh to get the latest data at the end of a file using the -f option.
Options and Parameters -n: view the data in the last n line. If the value of n is marked with a + sign, it indicates that the data starts from the x line. For example, tail -n +1000 test. TXT -f: display the data in the last 5 lines.
Example 2: View the data at the end of the file and refresh the data in real time
Example 3: Look at the bottom 5 lines of the file and refresh the data in real time
General command
Pipeline: Shell and a kind of function, is the ability to two or more commands (or process) together, the output of a command as the input of the next command connection of two or more orders in this way to form the pipe (pipe), piping is indicated by the symbol “|”.
Example: Look at the first 10 lines of ll command output
ll | head -n 3
The grep: command is used to search for qualified strings in a file. These two commands are commonly used in Linux and are used together to view log files.
Example: See which lines in the file contain ‘999’
cat -n test.txt | grep '999'
Echo “$I line” >> test.txt
Example: Copy the last 10 lines of a file into helloworld.txt
tail -n 10 >> helloworld.txt
Wc [-clw] [file…] -c or –bytes or –chars displays only the number of bytes. -l or –lines displays only the number of lines. -wor –words displays only the word count. Example: Check the number of file lines wC-l
Case of actual combat
Case 1: Print lines 11 through 20 in the log file. Get the first 20 lines, then get the last 10 lines, using the pipe command. head -n 20 text.txt | tail -n 10
cat -n test.txt | head -n 20 | tail -n 10
(Line number if required)
conclusion
Linux command is too much, for the development of the use of a lot of, but I think the first to know whether there is a command, and then classification master the most commonly used, when necessary, then look up the table. There’s no need to worry about remembering commands because they don’t determine your upper limit.
In addition to the above commands, Linux has a lot of strange tricks to view the contents of log files, such as the sed command. What commands do you use most often? Welcome to write friends stay (: