shell 命令

Table of Contents

1 进程

1.1 ps

1.2 pgrep

2 文本处理

2.1 fgrep、egrep 和 grep

fgrep 只匹配字符,egrep 使用了扩展正则表达式,现在都整合到了 grep 里,分别通过 -F、-E 参数区别。

2.2 fmt格式化输出

如下文本:

All material created by James Mohr is copyrighted 1994-2003 by James Mohr and is licensed under a modified GNU Free Documentation License. Reproduction and distribution of material copyrighted by James Mohr or derivative of such material in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder. Distribution of substantively modified versions of material copyrighted by James Mohr is prohibited without the explicit permission of the copyright holder. Distribution of material copyrighted by James Mohr in a commercial product is prohibited without the explicitpermission of the copyright holder. Use of material copyrighted by James Mohr in any commercial endeavour is prohibited without the explicit permission of the copyright holder. A "commercial endeavour" includes, but is not limited to training or other educational courses for which a fee is required. Public education institutions (such a state universities and colleges, community colleges and similar) are except from this requirement and need only

执行后:

All material created by James Mohr is copyrighted 1994-2003 by
James Mohr and is licensed under a modified GNU Free Documentation
License. Reproduction and distribution of material copyrighted by James
Mohr or derivative of such material in any standard (paper) book form
is prohibited unless prior permission is obtained from the copyright
holder. Distribution of substantively modified versions of material
copyrighted by James Mohr is prohibited without the explicit permission
of the copyright holder. Distribution of material copyrighted by James
Mohr in a commercial product is prohibited without the explicitpermission
of the copyright holder. Use of material copyrighted by James Mohr in any
commercial endeavour is prohibited without the explicit permission of the
copyright holder. A "commercial endeavour" includes, but is not limited to
training or other educational courses for which a fee is required. Public
education institutions (such a state universities and colleges, community
colleges and similar) are except from this requirement and need only

可以指定 -w 参数设置每行行宽。

2.3 expr

执行表达式,包括正则匹配、四则运算等。

例1,字符串按下标取子字符串

$  expr substr "abcd" 1 2
ab

例2,使用正则判断 IP 地址

$  expr match "192.168.1.1" "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$"
11

使用正则时要注意“{”、“}”、“(”、“)”需要转义,详细见:info expr

使用小括号配对时,打印匹配的值:

$  expr match "192.168.1.1" "^\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)$"
192.168.1.1

3 文件操作

3.1 查找文件位置

which:显示命令的绝对路径

例:

$ which cd
/usr/bin/cd

对于 alias,which 也能显示出来:

$ which ls
ls='ls --color=auto -F'
	/usr/bin/ls

whereis:查找文件、man和源码的路径

$ whereis cd
cd: /usr/bin/cd /usr/share/man/mann/cd.n.gz /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

3.2 cat

输出行号:

cat -n /etc/passwd

3.3 find

找出最近创建的文件

使用 find 命令可以找出最近创建的文件:

find ./ -mtime 0

参数 mtime,后面跟数字 n,表示最近 n×24 消失修改过的文件

查找 SUID 的程序

find 目录 -perm -4000

-perm 指定权限位,如 -perm -777

找出指定日期之后创建的文件

find . -name '*.doc' -newermt 2017-04-20

删除文件名乱码文件 有时删粗文件时,遇到文件名乱码,并且shell无法补全的情况下,就可借助find命令来删除。

找到文件的inode:ls -i

然后:find . -inum inode号 -exec rm {} \;

4 目录

4.1 cd

切换目录。

小技巧:CDPATH 环境变量

设置常用路径,使用 cd 命令时,可以不用写完整路径。

4.2 rm

删除指定目录所有文件,并排除某些文件:

rm -rf `ls | grep -v 文件名`
# 或者
rm -rf `ls | grep -v '[1|2|3].php'`

4.3 mktemp

新建临时文件,随机生成临时文件名。

4.4 解决“-”开头的文件名

方法1:如果对文件名使用了“-”开头使用 cp、mv、rm 命令,用“–”参数即可:

rm -f -- -exec # 删除-exec文件
mv -- -exec new
cp -- -exec new

方法2:可以用 ls -i 获得 i 节点号,然后用 find 命令操作。

方法3:最简单,直接带上路径,如:mv ./-exec a,即可。

4.5 dd

命令非 Unix 风格,dd 的命令风格来自JCL(https://en.wikipedia.org/wiki/Job_Control_Language

磁盘写入速度测试:

sync; dd if=/dev/zero of=test_file bs=1M count=1024; sync

5 系统相关

5.1 arch,查看处理器架构

$ arch
x86_64