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: [email protected], size=176, class=
0, nrcpts=1, msgid=<[email protected]>, [email protected]
Jan 12 00:10:34 PONTUS sendmail[5914]: [ID 801593 mail.info] t0BNA1Lo005727: [email protected], 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”命令可以列举不符合某种模式的文件:
举个例子:

[[email protected] 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个文件:
举个例子:

[[email protected] 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”命令可以用表格化方式显示输出:
举个例子:

[[email protected] /]# 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)
[[email protected] /]# 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)
[[email protected] /]#

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

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

 

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

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

[[email protected] 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命令把命令输出到多个日志文件:
举个例子:

[[email protected] 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
[[email protected] command]# ls
mem1.log  mem2.log
[[email protected] 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 ..
[[email protected] 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
[[email protected] 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