Useful Linux Commands

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> &gt; /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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值