Unix/Linux命令行小技巧(20)- 按目录size大小列举目录

使用“du --block-size=kB | sort -n”或“du --block-size=kB | sort -nr”命令可以按目录size从小到大或从大到小列举目录。
举个例子:

[root@localhost /]$ du --block-size=kB | sort -n
 0kB    ./dev/bsg
 0kB    ./dev/bus
......
[root@localhost /]$ du --block-size=kB | sort -nr
 1179418kB    .
 937862kB    ./usr
......

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

Unix/Linux命令行小技巧(19)- 显示系统的内存使用

使用“ps -A --sort -rss -o pid,comm,pmem,rss | less”命令可以显示系统的内存使用。
举个例子:

[root@home]$ ps -A --sort -rss -o pid,comm,pmem,rss | less
 PID COMMANDMEM  RSS
1386 abrtd           0.1   4824
1223 hald            0.0   3888
1423 login           0.0   3420
......

可以看到打印了每个进程占用的内存百分比,以及RSS的大小。
技巧出处:https://twitter.com/nixcraft/status/288158831551332353

Unix/Linux命令行小技巧(18)- ls命令按文件size大小列举文件

使用“ls -l -S *.d”命令按文件size从大到小列举文件,而“ls -l -S *.d | sort -k 5 -n”按文件size从小到大列举文件:
举个例子:

Nans-MacBook-Pro:Proc nanxiao$ ls -l -S *.d
-rwxr-xr-x@ 1 nanxiao  staff  4988 Dec  7 11:51 crash.d
-rwxr-xr-x@ 1 nanxiao  staff  3067 Dec  7 11:51 shortlived.d
-rwxr-xr-x@ 1 nanxiao  staff  2491 Dec  7 11:51 rwbytype.d
-rwxr-xr-x@ 1 nanxiao  staff  2452 Dec  7 11:51 pathopens.d
-rwxr-xr-x@ 1 nanxiao  staff  2263 Dec  7 11:51 stacksize.d
-rwxr-xr-x@ 1 nanxiao  staff  1684 Dec  7 11:51 kill.d
-rwxr-xr-x@ 1 nanxiao  staff  1656 Dec  7 11:51 sigdist.d
-rwxr-xr-x@ 1 nanxiao  staff  1651 Dec  7 11:51 threaded.d
-rwxr-xr-x@ 1 nanxiao  staff  1583 Dec  7 11:51 mmapfiles.d
-rwxr-xr-x@ 1 nanxiao  staff  1502 Dec  7 11:51 rwbbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1494 Dec  7 11:51 rwbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1476 Dec  7 11:51 sysbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1331 Dec  7 11:51 pidpersec.d
-rwxr-xr-x@ 1 nanxiao  staff  1331 Dec  7 11:51 syscallbypid.d
-rwxr-xr-x@ 1 nanxiao  staff   283 Dec  7 11:51 filebyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   281 Dec  7 11:51 creatbyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   268 Dec  7 11:51 writedist.d
-rwxr-xr-x@ 1 nanxiao  staff   267 Dec  7 11:51 newproc.d
-rwxr-xr-x@ 1 nanxiao  staff   267 Dec  7 11:51 syscallbyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   264 Dec  7 11:51 readdist.d
-rwxr-xr-x@ 1 nanxiao  staff   259 Dec  7 11:51 writebytes.d
-rwxr-xr-x@ 1 nanxiao  staff   255 Dec  7 11:51 readbytes.d

Nans-MacBook-Pro:Proc nanxiao$ ls -l -S *.d | sort -k 5 -n
-rwxr-xr-x@ 1 nanxiao  staff   255 Dec  7 11:51 readbytes.d
-rwxr-xr-x@ 1 nanxiao  staff   259 Dec  7 11:51 writebytes.d
-rwxr-xr-x@ 1 nanxiao  staff   264 Dec  7 11:51 readdist.d
-rwxr-xr-x@ 1 nanxiao  staff   267 Dec  7 11:51 newproc.d
-rwxr-xr-x@ 1 nanxiao  staff   267 Dec  7 11:51 syscallbyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   268 Dec  7 11:51 writedist.d
-rwxr-xr-x@ 1 nanxiao  staff   281 Dec  7 11:51 creatbyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   283 Dec  7 11:51 filebyproc.d
-rwxr-xr-x@ 1 nanxiao  staff  1331 Dec  7 11:51 pidpersec.d
-rwxr-xr-x@ 1 nanxiao  staff  1331 Dec  7 11:51 syscallbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1476 Dec  7 11:51 sysbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1494 Dec  7 11:51 rwbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1502 Dec  7 11:51 rwbbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1583 Dec  7 11:51 mmapfiles.d
-rwxr-xr-x@ 1 nanxiao  staff  1651 Dec  7 11:51 threaded.d
-rwxr-xr-x@ 1 nanxiao  staff  1656 Dec  7 11:51 sigdist.d
-rwxr-xr-x@ 1 nanxiao  staff  1684 Dec  7 11:51 kill.d
-rwxr-xr-x@ 1 nanxiao  staff  2263 Dec  7 11:51 stacksize.d
-rwxr-xr-x@ 1 nanxiao  staff  2452 Dec  7 11:51 pathopens.d
-rwxr-xr-x@ 1 nanxiao  staff  2491 Dec  7 11:51 rwbytype.d
-rwxr-xr-x@ 1 nanxiao  staff  3067 Dec  7 11:51 shortlived.d
-rwxr-xr-x@ 1 nanxiao  staff  4988 Dec  7 11:51 crash.d

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

Unix/Linux命令行小技巧(17)- 打印进程打开的文件数目

使用“for p in $(pidof process); do echo "PID # $p has $(lsof -n -a -p $p|wc -l) fd opened."; done”命令打印process进程打开的文件数目。
举个例子:

[root@localhost /]# for p in $(pidof java); do echo "PID # $p has $(lsof -n -a -p $p|wc -l) fd opened."; done
PID # 37747 has 139 fd opened.
PID # 30279 has 117 fd opened.
PID # 28033 has 204 fd opened.
PID # 10501 has 211 fd opened.
PID # 4998 has 442 fd opened.

显示了所有java进程打开的文件数目。
技巧出处:https://twitter.com/nixcraft/status/281010856706334720

 

Unix/Linux命令行小技巧(16)- ls命令显示以大写字母作为文件名开头的文件

使用“ls /path/to/[[:upper:]]*”命令可以显示以大写字母作为文件名开头的文件。
举个例子:

[root@localhost /]# ls /usr/bin/[[:upper:]]*
/usr/bin/AtoB            /usr/bin/GenIssuerAltNameExt   /usr/bin/PKCS12Export
/usr/bin/AuditVerify     /usr/bin/GenSubjectAltNameExt  /usr/bin/POST
/usr/bin/BtoA            /usr/bin/GET                   /usr/bin/PrettyPrintCert
/usr/bin/CMCEnroll       /usr/bin/HEAD                  /usr/bin/PrettyPrintCrl
/usr/bin/CMCRequest      /usr/bin/HtFileType            /usr/bin/RSA_SecurID_getpasswd
/usr/bin/CMCResponse     /usr/bin/HttpClient            /usr/bin/RunSimTest
/usr/bin/CMCRevoke       /usr/bin/IBMgtSim              /usr/bin/TokenInfo
/usr/bin/CRMFPopClient   /usr/bin/Mail                  /usr/bin/X
/usr/bin/ExtJoiner       /usr/bin/OCSPClient            /usr/bin/Xorg
/usr/bin/GenExtKeyUsage  /usr/bin/PKCS10Client

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

 

Unix/Linux命令行小技巧(15)- 显示端口的状态及原因

使用“nmap --reason ip-address”命令可以显示当前IP地址上端口的状态及原因。
举个例子:

[root@localhost nan]# nmap --reason 192.168.23.150

Starting Nmap 6.46 ( http://nmap.org ) at 2015-01-13 15:17 CST
Nmap scan report for localhost.localdomain (192.168.23.150)
Host is up, received localhost-response (0.0000030s latency).
Not shown: 995 closed ports
Reason: 995 resets
PORT     STATE SERVICE REASON
22/tcp   open  ssh     syn-ack
23/tcp   open  telnet  syn-ack
111/tcp  open  rpcbind syn-ack
3306/tcp open  mysql   syn-ack
6699/tcp open  napster syn-ack

Nmap done: 1 IP address (1 host up) scanned in 0.09 seconds

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

 

Unix/Linux命令行小技巧(14)- 显示指定网络协议的所有连接

使用“ss -o state established '( dport = :protocol or sport = :protocol )'”命令可以显示所有网络协议为protocol的连接。
举个例子:

[root@localhost nan]# ss -o state established '( dport = :ssh or sport = :ssh )'
Recv-Q Send-Q                        Local Address:Port                            Peer Address:Port
0      0                            192.168.23.150:ssh                            10.228.155.38:49631    timer:(keepalive,87min,0)

把协议换成http

[root@localhost nan]# ss -o state established '( dport = :http or sport = :http )'
Recv-Q Send-Q                        Local Address:Port                            Peer Address:Port

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

 

Unix/Linux命令行小技巧(13)- 计算每个用户打开的文件数

使用“lsof -u user | wc -l”命令可以显示user用户打开的文件数。
举个例子:

[root@localhost nan]# lsof -u root | wc -l
5293

显示root用户打开了5293个文件。

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

 

Unix/Linux命令行小技巧(12)- 根据文件修改时间查找文件

使用“find folder -type f -ctime -n -print”命令可以显示folder文件夹下最近n天修改过的文件。
举个例子:

[root@localhost nan]# find /etc -type f -ctime -5 -print
/etc/adjtime
/etc/prelink.cache

也可以显示详细信息:

[root@localhost nan]# find /etc -type f -ctime -5 -exec ls -l {} \;
-rw-r--r--. 1 root root 45 Jan 13 14:09 /etc/adjtime
-rw-r--r-- 1 root root 646080 Jan 10 03:27 /etc/prelink.cache

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

 

Unix/Linux命令行小技巧(11)- ls命令只显示目录

使用“ls -ltrF --color | grep ^d”命令可以只显示一个文件夹下面的目录。
举个例子:

[root@localhost nan]# ls -ltrF --color | grep ^d
drwxr-xr-x  8 1000 1000      4096 Jun 16  2014 x86_64-gdcproject-linux-gnu/
drwxr-xr-x  8 1000 1000      4096 Oct 16 13:31 bashdb-4.1-0.5/
drwxrwxr-x 20 1000 1000      4096 Nov 26 14:37 unixODBC-2.3.2/
drwxrwxr-x 14 1000 1000      4096 Dec 10 09:32 expat-2.1.0/
drwxr-xr-x 14 root root      4096 Dec 11 09:42 cmake-3.0.2/
drwxr-xr-x  8 root root      4096 Dec 11 09:43 build_sysdig/
drwxrwxr-x  6 root root      4096 Dec 16 16:18 redis-2.8.19/
drwxr-xr-x  2 root root      4096 Dec 23 17:48 odbc_test/
drwxr-xr-x 15 root root      4096 Dec 24 14:22 gdb-7.8.1/
drwxr-xr-x 11 root root      4096 Dec 24 14:23 build_gdb/
drwxr-xr-x  4 root root      4096 Jan  8 10:59 hiredis-master/
drwxr-xr-x  2 root root      4096 Jan 13 10:25 command/

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