cd命令小技巧

(1)从任一目录回到用户的home目录:“cd”或“cd ~”。举例如下:

[root@localhost ~]# cd /home/iso/
[root@localhost iso]# pwd
/home/iso
[root@localhost iso]# cd
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home/iso/
[root@localhost iso]# pwd
/home/iso
[root@localhost iso]# cd ~
[root@localhost ~]# pwd
/root

(2)回到之前的工作目录:“cd -”。举例如下:

[root@localhost ~]# pwd
/root
[root@localhost ~]# cd -
/home/iso
[root@localhost iso]# pwd
/home/iso

Bash shell内置wait命令简介

Bash shell内置了wait命令,官方文档对wait解释如下:

wait

     wait [-n] [jobspec or pid …]

Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero. If the -n option is supplied, wait waits for any job to terminate and returns its exit status. If neither jobspec nor pid specifies an active child process of the shell, the return status is 127.

wait命令可以使当前shell进程挂起,等待所指定的由当前shell产生的子进程退出后,wait命令才返回。wait命令的参数可以是进程ID或是job specification。举例如下:

root# sleep 10 &
[3] 876
root# wait 876
[3]+  Done                    sleep 10
root# sleep 20 &
[1] 877
root# wait %1
[1]+  Done                    sleep 20 

wait命令一个很重要用途就是在Bash shell的并行编程中,可以在Bash shell脚本中启动多个后台进程(使用&),然后调用wait命令,等待所有后台进程都运行完毕,Bash shell脚本再继续向下执行。像下面这样:

command1 &
command2 &
wait

Bash shell还有一个内置变量:$!,用来记录最后一个被创建的后台进程。

root# sleep 20 &
[1] 874
root# sleep 10 &
[2] 875
root# echo $!
875

echo $!输出结果是875,是第二个执行的sleep命令。

参考资料:
Does bash script wait for one process to finish before executing another?

在VirtualBox上安装Solaris 11

为了更方便地研究DTrace,花了一下午时间装了个Solaris 11的虚拟机。基本就是靠着google,解决了所有问题。在这里简单列举一下步骤,希望可以帮到有需求的朋友:

(1)在VirtualBox上安装Solaris 11,基本可以参考这篇文章;
(2)由于VirtualBox不支持滚动条,所以我习惯于用ssh客户端登陆上去进行操作。而Solaris默认不支持root用户直接ssh登陆。修改方法参照这篇文章
(3)Solaris 11安装包默认没有gcc,可参考stackoverflow这篇帖子下载安装gcc。期间如果涉及到如何配置Solaris 11DNS服务,可参考这篇帖子

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