Log
If we have a log file foo.log
:
Some of the most frequently used commands are:
cat foo.log
tail -f foo.log
vim foo.log
If we want to find some record from the file by using vim
:
-
vim foo.log
-
Press
G
jump to the end of the file -
Press
?
+ keyword to search the corresponding record -
Press
n
to search from bottom to top,N
to search from top to bottom -
tail -f foo.log
can check if there is any change of the log file.
For large files, it’s better to use grep
. If we want to check a user’s log file by searching the user’s ID, we can use:
cat foo.log | grep <UserID>
Then we can find all the records containing UserID
from the file.
If we want to find the line number of the info, we can add -n
parameter, which the previous command line becomes cat -n foo.log | grep <UserID>
If we want to check lines before and after for the current line:
-
sed -n "1888,1888p" foo.log
: start search from line 1888 and end the serach at the end of line 1888 -
cat -n foo.log | tail -n +1888 | head -n 50
: start from line 1888 and push foward 20 lines.
If we do not have an exact query word, we can use more
to browse or output to an file:
cat foo.log | grep <keyword> | more
cat foo.log | grep <keyword> > /home/ghostian/tmpfiles/output.txt
Sometimes if we want to cauculate how many lines does the log file have, we can use:
cat foo.log | wc -l
Process and Prot
There are two commands to see the current process:
ps -ef
ps aux
By using |
pipeline and grep
to filter out the process we need, for example:
ps -ef | grep java
If we want to kill the procee, we can use:
kill -9 <ProcessID>
To check the port, we can use netstat -lntup
To find the information of a specific port: lsof -i:<port>
System Status
top
to view process stat in real time