2

Shell 命令组合集[2019.04.05更新]

 2 years ago
source link: https://shockerli.net/post/shell-practical-command-collection/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Shell 命令组合集[2019.04.05更新]

2019-04-05 约 1393 字 预计阅读 3 分钟

本文档收集了常用的 Shell 命令组合,依然在不定期更新中…

统计独立 IP 数量

awk '{print $1}' access.log | sort -n | uniq | wc -l

查看某一时间段的 IP 访问量

grep "05/Apr/2019:0[1-9]" access.log | awk '{print $1}' | sort | uniq -c| sort -nr | wc -l

查看访问最频繁的前 100 个 IP

awk '{print $1}' access.log | sort -n | uniq -c | sort -rn | head -n 100

查看访问 100 次以上的 IP

awk '{print $1}' access.log | sort -n | uniq -c | awk '{if($1 > 100) print $0}' | sort -rn

查询某个 IP 的详细访问情况,按访问频率排序

grep '127.0.0.1' access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head -n 100

统计 URL 访问量排行

awk '{url[$7]++} END {for (k in url) {print url[k],k}}' nginx.access.log | sort -rn

使用 awk 从 Nginx 日志中逐行统计 URL 访问计数,然后使用 sort 对结果进行排名

访问最频繁的 URL

awk '{print $7}' access.log | sort | uniq -c | sort -rn | head -n 100
awk '{url[$7]++} END {for (k in url) {print url[k],k}}' access.log | sort -rn | head -n 100

除了 .php 以外,访问最频繁的 URL

grep -v ".php" access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head -n 100

URL 访问次数超过 100 次的页面

awk '{print $7}' access.log | sort -n | uniq -c | sort -rn | head -n 100

查看最近1000条记录,访问量最高的 URL

tail -1000 access.log | awk '{print $7}' | sort | uniq -c | sort -rn | less

统计每秒的请求数,TOP100的时间点(精确到秒)

awk '{print $4}' access.log | cut -c 14-21 | sort | uniq -c | sort -rn | head -n 100

统计每小时的请求数,TOP100的时间点(精确到小时)

awk '{print $4}' access.log | cut -c 14-15 | sort | uniq -c | sort -rn | head -n 100

列出传输时间超过3秒的页面,并统计其出现的次数,显示前20条

在 Nginx log 最后一个字段加入 $request_time

cat access.log | awk '($NF > 3){print $7}' | sort -n | uniq -c | sort -rn | head -20

列出PHP页面请求时间超过3秒的页面,并统计其出现的次数,显示前100条

在 Nginx log 最后一个字段加入 $request_time

cat access.log | awk '($NF > 1 && $7~/\.php/){print $7}' | sort -n | uniq -c | sort -rn | head -100

列出当前目录下的所有文件(包括隐藏文件)的绝对路径

find $PWD -maxdepth 1 | xargs ls -ld

递归列出当前目录下的所有文件(包括隐藏文件)的绝对路径

find $PWD | xargs ls -ld

在每行记录的开头加上当前路径

ls | sed "s:^:`pwd`/:"

删除指定时间之前的文件

find /path/to/dir -mtime +30 -type f | xargs rm -f
  • /path/to/dir 设置查找的目录
  • --mtime +30 设置时间为30天前
  • -type f 指定查找的类型为文件

删除文件前/后N行

删除了前2行。先用tail把从第3行开始的所有内容输出到新文件,然后再重命名文件。

tail -n +3 old_file > new_file 
mv new_file old_file

仅保留最后3行。

tail -n -3 old_file > new_file 
mv new_file old_file

如果写定时任务,那可放置到一行。

0 0 * * * tail -n -3 old_file > new_file && mv -f new_file old_file

统计网卡的流量数据

sar -n DEV 1 5
    
平均时间:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均时间:        lo      2.21      2.21      0.18      0.18      0.00      0.00      0.00
平均时间:      eth0      4.62      3.82      0.37      1.90      0.00      0.00      0.00

命令中 1 5 表示每一秒钟取 1 次值,一共取 5 次。

命令执行后会列出每个网卡这 5 次取值的平均数据,根据实际情况来确定带宽跑满的网卡名称,默认情况下 eth0 为内网网卡,eth1 为外网网卡。

查询占用端口的进程/程序

netstat -tunlp | grep ':80'
    
tcp        0      0 0.0.0.0:80      0.0.0.0:*    LISTEN      26655/nginx

或者使用 lsof 命令:

lsof -i :80

查看流量占用情况

iftop -P

查看程序流量排行

nethogs

进程/程序

grep 程序并杀死

ps -ef | grep process_name | grep -v grep | cut -c 9-15 | xargs kill -s 9

查看指定进程的具体占用内存

cat /proc/[pid]/status
Name:	memcached
State:	S (sleeping)
Tgid:	1954
Pid:	1954
PPid:	1
TracerPid:	0
Uid:	500	500	500	500
Gid:	500	500	500	500
Utrace:	0
FDSize:	128
Groups:
VmPeak:	  413792 kB
VmSize:	  360544 kB
VmLck:	       0 kB
VmHWM:	   29704 kB
VmRSS:	   29376 kB
VmData:	  341768 kB
VmStk:	    2132 kB
VmExe:	      80 kB
VmLib:	    2152 kB
VmPTE:	     164 kB
VmSwap:	       0 kB
Threads:	6
...

其中,VmRSS项表示实际占用内存值。

或者,用ps命令

ps aux | grep <pid>
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
jxcdn     1954  0.0  0.1 360544 29376 ?        Ssl  Apr13   7:56 memcached -m 128 -p 11211

其中RSS列表示实际使用内存(单位: KB)。可以看出,与/proc/[pid]/status的值是一致的。

获取脚本文件所在目录

script_path=$(cd `dirname $0`; pwd)

获取脚本文件的上级目录

script_path=$(cd `dirname $0`; pwd)
root_path=$(cd `dirname "$script_path"`; pwd)

格式化当前时间

datetime=$(date +"%Y-%m-%d %H:%M:%S")

去除文本中的颜色转义符

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

来源: https://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed


Recommend

  • 53
    • down.51cto.com 6 years ago
    • Cache

    shell编程和unix命令

    shell编程和unix命令,基于unix的shell脚本开发的学习与研究

  • 37
    • 微信 mp.weixin.qq.com 4 years ago
    • Cache

    Linux shell命令总结

  • 48
    • 微信 mp.weixin.qq.com 4 years ago
    • Cache

    HBase Shell 命令

    HBase 提供了一个非常方便的命令行交互工具 HBase Shell。通过 HBase Shell 可以创建表,也可以增删查数据,同时集群的管理、状态查看等也可以通过 HBase shell 实现。 HBase Shell 用法: 确保用 HBase Shell...

  • 17
    • 微信 mp.weixin.qq.com 4 years ago
    • Cache

    Linux shell 一些命令

    点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,麻烦点个在看或点个赞,感谢~ 程序环境: ubuntu16.04  x64 虚拟机 以一个实际的需求为例进行说明: 获取固定网卡的ip地址 一、 例子拆解 1.  ifconfig效果...

  • 12
    • blog.7rule.com 3 years ago
    • Cache

    gobox中的编解码和执行shell命令

    gobox中的编解码和执行shell命令 May 11, 2018 今天来说下gobox中的encoding和shell两个box。 encoding encoding的主要作用是完成编解码工作,目前支持了base64编解码。

  • 6

    linux下的C语言开发19,使用C语言执行shell命令 发表于 2019-01-07 18:01:11...

  • 15
    • zhiqiang.org 3 years ago
    • Cache

    Excel VBA 调用 Shell 命令

    某些时候,我们需要在 Excel 中调用命令行或者 Bash 脚本, VBA 可以通过 Shell 函数很方便地做到这一点。以下用ipconfig /all来举例,这条命令行语句用来获得机器的网路配置信息,包括 IP、网关等信息。它可以替换成任何一个 bash 脚本和命令行代...

  • 1
    • lulalap.com 3 years ago
    • Cache

    短诗合集(2019)

    短诗合集(2019) 发表于 2020-01-02 ...

  • 8
    • lulalap.com 3 years ago
    • Cache

    Philo的脑洞合集(2019)

    你常常发呆吗?你发呆的时候都在想些什么? 人类冬眠了会怎么样?如何实现长生不老?如何变快乐?人类要是也有了叶绿体会怎么样?有没有外星人?为什么人要睡觉?什么是记忆?什么是性别?人类天生就会走路吗?为什么有人喜欢跑马拉松…… 这些想法...

  • 3

    《命令与征服 : 最终合集》Steam上架:包含“红警2”在内17款作品,现售78元

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK