Linux view log command

Today, the instructor asked to use linux to view the log. Here, I first learned about the linux of the ubuntu kernel, and now review the statement to view the log.

1. tail

View real-time logs

tail -f

You can add grep keywords to filter log keywords

tail -F dpkg.log | grep --line-buffer '关键字'

ctrl + c to end log printing

When the file is relatively large, get the last 1000 rows of data in the log

tail -n 1000 文件名 > 本地文件

If you want to monitor logs continuously

tail -f 文件名

Note here: If you use the vi command, it cannot be monitored
Reason: The principle of vi is to delete the file first and then create it.

If you want to monitor it, you can use the big F

tail -f 文件名

Two, head

Look at the first 100 lines of the log

head -100 文件名

Three, cat

View log files directly

cat 文件名

Four, more

more 文件名

insert image description here

space to scroll down
b key to scroll up

Starting from 1000 lines, turn 20 lines each time

more +1000 -20 文件名

insert image description here

qquit

Five, view

view 文件名

Then you can use the following command to search

/关键字

5.1 Search from top to bottom

Press n to find the next one
insert image description here

5.2 Search from bottom to top

Enter the following command to come to the last line

:$

then type

/关键字

At this time, press n to search up.

5.3 Exit

:q

6. Search

6.1 Fuzzy search

If you only know a certain keyword in the log, now you want to search globally

grep ‘关键字’ *.log

6.2 File location

I only know a certain keyword in the log log, but I don't know where the file is

find . -name "*.log" | xargs grep -r "关键字"

insert image description here

7. Frequently asked questions

7.1 The newly installed ubuntu does not set the root password

sudo passwd root

First enter the password of the currently logged in user, then enter the password of the root user twice
insert image description here
and then switch to the root user

su root

7.2 Command not found

insert image description here
Generally, there will be prompts, just follow the prompts

sudo apt install tree

8. Other common commands

8.1 echo input to the file

echo "文字" >> 文件名

insert image description here

8.2 pwd

pwd:print working directory

pwd

insert image description here

8.3 ls

list
view directory

ls

insert image description here

8.4 cd

change directory

cd /var/log

insert image description here

Guess you like

Origin blog.csdn.net/sinat_38316216/article/details/132052483
Recommended