Unix/Linux命令行小技巧(10)- 度量执行一个命令花费的时间等参数

使用“/usr/bin/time -f 'rss=%Mk etime=%E user=%U sys=%S in=%I out=%O' command”命令可以度量执行一个命令花费的时间,占用的内存,文件I/O等参数.
举个例子:

[root@localhost command]# /usr/bin/time -f 'rss=%Mk etime=%E user=%U sys=%S in=%I out=%O'  find /
/data1/sun/tomcat7/webapps/metronic_v1.5.4/admin/resources/glyphicons_pro/glyphicons_pro/glyphicons/png/glyphicons_157_show_thumbnails_with_lines@2x.png
/data1/sun/tomcat7/webapps/metronic_v1.5.4/admin/resources/glyphicons_pro/glyphicons_pro/glyphicons/png/glyphicons_143_database_ban@2x.png
.....
rss=5440k etime=0:03.13 user=0.03 sys=0.08 in=0 out=0

可以看到最后会打印出执行“find /”命令占用的内存,花费的时间等值:

rss=5440k etime=0:03.13 user=0.03 sys=0.08 in=0 out=0

技巧出处:https://twitter.com/nixcraft/status/235491209743831040

 

Unix/Linux命令行小技巧(9)- 为搜索出来的字符串加上颜色

可以使用“--color”选项为grep查找出来的字符串着色:
举个例子:

[root@localhost command]# grep --color [Cat] <<<"nixCraft"

输出为:
nixCraft

也可使用egrep命令:

[root@localhost command]# egrep --color -i 'Mon' ./scan.txt  

输出为:
Nmap 6.46 scan initiated Mon Jan 12 10:58:32 2015 as: nmap -PR -oN scan.txt 192.168.1.0/24
Nmap done at Mon Jan 12 10:58:32 2015 — 256 IP addresses (0 hosts up) scanned in 0.04 seconds

技巧出处:https://twitter.com/nixcraft/status/231473119817261056

 

Unix/Linux命令行小技巧(8)- 只查看今天的日志

使用“grep "$(date +%b\ %e)" /var/log/syslog”命令只查看今天的日志信息:
举个例子:

-bash-3.2$ grep "$(date +%b\ %e)" /var/log/syslog
Jan 12 00:10:18 PONTUS sendmail[5727]: [ID 801593 mail.info] t0BNA1Lo005727: from=sa@xx.com, size=176, class=
0, nrcpts=1, msgid=<201501112310.t0BNA1Lo005727@PONTUS.xx.net>, relay=root@localhost
Jan 12 00:10:34 PONTUS sendmail[5914]: [ID 801593 mail.info] t0BNA1Lo005727: to=cc@xx.net, delay=00:00:33, xd
elay=00:00:16, mailer=relay, pri=120176, relay=mailhost.xx.net. [10.10.8.90], dsn=2.0.0, stat=Sent (t0BNAWRn0
00026 Message accepted for delivery)

技巧出处:https://twitter.com/nixcraft/status/231473119817261056

 

Unix/Linux命令行小技巧(7)- 列举不符合某种模式的文件

使用“ls -I”命令可以列举不符合某种模式的文件:
举个例子:

[root@localhost include]# ls -I 'z*'
acl                   execinfo.h           ldap.h          nspr4                sched.h
aio.h                 fcntl.h              ldap_schema.h   nss3                 scsi
aliases.h             features.h           ldap_utf8.h     nss.h                search.h
alloca.h              fenv.h               ldif.h          numpy                security
alsa                  fmtmsg.h             libart-2.0      obstack.h            selinux
ansidecl.h            fnmatch.h            libaudit.h      openssl              semaphore.h
antlr                 fontconfig           libcgroup       panel.h              sepol
a.out.h               form.h               libcroco-0.6    pango-1.0            setjmp.h
argp.h                fpu_control.h        libdrm          paths.h              sgtty.h
argz.h                freetype2            libelf.h        pcap-bpf.h           shadow.h
ar.h                  fstab.h              libexslt        pcap.h               signal.h
arpa                  ft2build.h           libgen.h        pcap-namedb.h        slapi-plugin.h
.....

显示不以字母z开头的文件名。

技巧出处:https://twitter.com/nixcraft/status/217200352267739137

 

Unix/Linux命令行小技巧(6)- 打印当前目录下占磁盘空间最多的文件

使用“du -hsx * | sort -rh | head -n”命令可以打印当前目录下占磁盘空间最多的n个文件:
举个例子:

[root@localhost include]# du -hsx * | sort -rh | head -5
63M     boost
7.3M    c++
5.9M    Qt
4.3M    QtGui
3.7M    pgsql

打印了占磁盘空间最多的5个文件(包含目录)。

技巧出处:https://twitter.com/nixcraft/status/201955934283632640

 

Unix/Linux命令行小技巧(5)- 以表格方式显示输出

使用“column -t”命令可以用表格化方式显示输出:
举个例子:

[root@localhost /]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sda8 on /data1 type ext4 (rw)
/dev/sdb1 on /data2 type ext4 (rw)
/dev/sdc1 on /data3 type ext4 (rw)
/dev/sda2 on /home type ext4 (rw)
/dev/sda5 on /usr type ext4 (rw)
/dev/sda6 on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
none on /sys/kernel/config type configfs (rw)
[root@localhost /]# mount | column -t
/dev/sda3  on  /                         type  ext4         (rw)
proc       on  /proc                     type  proc         (rw)
sysfs      on  /sys                      type  sysfs        (rw)
devpts     on  /dev/pts                  type  devpts       (rw,gid=5,mode=620)
tmpfs      on  /dev/shm                  type  tmpfs        (rw)
/dev/sda1  on  /boot                     type  ext4         (rw)
/dev/sda8  on  /data1                    type  ext4         (rw)
/dev/sdb1  on  /data2                    type  ext4         (rw)
/dev/sdc1  on  /data3                    type  ext4         (rw)
/dev/sda2  on  /home                     type  ext4         (rw)
/dev/sda5  on  /usr                      type  ext4         (rw)
/dev/sda6  on  /var                      type  ext4         (rw)
none       on  /proc/sys/fs/binfmt_misc  type  binfmt_misc  (rw)
none       on  /sys/kernel/config        type  configfs     (rw)
[root@localhost /]#

对比之后可以发现“mount | column -t”命令输出很清晰。

技巧出处:https://twitter.com/nixcraft/status/190898232228978688

 

Unix/Linux命令行小技巧(4)- 显示文本文件内容的行号

使用cat -n /file命令可以显示文本文件内容的行号:
举个例子:

[root@localhost command]# cat -n ./mem1.log
 1               total       used       free     shared    buffers     cached
 2  Mem:         32062       9922      22140          0        906       3451
 3  -/+ buffers/cache:       5563      26498
 4  Swap:        31999       9313      22686

技巧出处:https://twitter.com/nixcraft/status/188694159203373056

Unix/Linux命令行小技巧(3)- 把命令输出到多个日志文件

可以使用tee命令把命令输出到多个日志文件:
举个例子:

[root@localhost command]# free -m | tee mem1.log mem2.log
         total       used       free     shared    buffers     cached
Mem:         32062       9922      22140          0        906       3451
-/+ buffers/cache:       5563      26498
Swap:        31999       9313      22686
[root@localhost command]# ls
mem1.log  mem2.log
[root@localhost command]# ls -alt
total 16
drwxr-xr-x  2 root root 4096 Jan 12 10:48 .
-rw-r--r--  1 root root  230 Jan 12 10:48 mem1.log
-rw-r--r--  1 root root  230 Jan 12 10:48 mem2.log
drwxrwxrwx 15 root root 4096 Jan 12 10:47 ..
[root@localhost command]# cat mem1.log
             total       used       free     shared    buffers     cached
Mem:         32062       9922      22140          0        906       3451
-/+ buffers/cache:       5563      26498
Swap:        31999       9313      22686
[root@localhost command]# cat mem2.log
             total       used       free     shared    buffers     cached
Mem:         32062       9922      22140          0        906       3451
-/+ buffers/cache:       5563      26498
Swap:        31999       9313      22686

可以看到,mem1.logmem2.log文件都保存了日志。
技巧出处:https://twitter.com/nixcraft/status/163974717311692801

 

Unix/Linux命令行小技巧(2)- Linux上kill占用某端口的进程

Linux平台上kill占用某端口进程命令:

fuser -k port/tcp(udp)

举个例子:

[root@localhost redis-2.8.19]# ps -ef | grep redis
root     10049 35207  0 17:33 pts/7    00:00:00 ./src/redis-server *:6379
root     10116 35207  0 17:33 pts/7    00:00:00 grep redis             
[root@localhost redis-2.8.19]# fuser -k 6379/tcp
6379/tcp:            10049
[1]+  Killed                  ./src/redis-server

可以看到,占用6379端口的进程被杀死了。
技巧出处:https://twitter.com/nixcraft/status/162914646125330432

Unix/Linux命令行小技巧(1)- 显示运行进程的PID

Bash中定义如下函数(函数参数为程序名):

findpid() { ps axc|awk "{if (\$5==\"$1\") print \$1}"; }

执行效果:

[root@localhost ~]# findpid() { ps axc|awk "{if (\$5==\"$1\") print \$1}"; }
[root@localhost ~]# findpid tail
16049
25206
40701
48132

技巧出处:https://twitter.com/nixcraft/status/158835574508240896