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

 

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