Linux 环境下的一些常用命令(三)

转载自 http://www.oschina.net/translate/20-advanced-commands-for-middle-level-linux-users

21. 命令: Find 

搜索指定目录下的文件,从开始于父目录,然后搜索子目录。 

[plain] view plain copy

root@tecmint:~# find -name *.sh  


./Desktop/load.sh  


./Desktop/test.sh  


./Desktop/shutdown.sh  

注意: `-name‘选项是搜索大小写敏感。可以使用`-iname‘选项,这样在搜索中可以忽略大小写。(*是通配符,可以搜索所有的文件;‘.sh‘你可以使用文件名或者文件名的一部分来制定输出结果)

[plain] view plain copy

root@tecmint:~# find -iname *.SH ( find -iname *.Sh /  find -iname *.sH)  



./Desktop/load.sh  


./Desktop/test.sh  


./Desktop/shutdown.sh  

[plain] view plain copy

root@tecmint:~# find -name *.tar.gz  


/var/www/modules/update/tests/aaa_update_test.tar.gz  


./var/cache/flashplugin-nonfree/install_flash_player_11_linux.i386.tar.gz  

22. 命令: grep

grep‘命令搜索指定文件中包含给定字符串或者单词的行。举例搜索‘/etc/passwd‘文件中的‘tecmint'

[plain] view plain copy

root@tecmint:~# grep tecmint /etc/passwd  


tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash  

使用’-i'选项将忽略大小写。 

[plain] view plain copy

root@tecmint:~# grep -i TECMINT /etc/passwd  


tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash  

使用’-r'选项递归搜索所有自目录下包含字符串 “127.0.0.1“.的行。 

[plain] view plain copy

root@tecmint:~# grep -r "127.0.0.1" /etc/  


/etc/vlc/lua/http/.hosts:127.0.0.1  


/etc/speech-dispatcher/modules/ivona.conf:#IvonaServerHost "127.0.0.1"  


/etc/mysql/my.cnf:bind-address      = 127.0.0.1  

注意:您还可以使用以下选项:

-w 搜索单词 (egrep -w ‘word1|word2‘ /path/to/file).

-c 用于统计满足要求的行 (i.e., total number of times the pattern matched) (grep -c ‘word‘ /path/to/file).

–color 彩色输出 (grep –color server /etc/passwd).

23. 命令: man 

man‘是系统帮助页。Man提供命令所有选项及用法的在线文档。几乎所有的命令都有它们的帮助页,例如: 

[plain] view plain copy

root@tecmint:~# man man  

注意:系统帮助页是为了命令的使用和学习而设计的。

24. 命令: ps 

ps命令给出正在运行的某个进程的状态,每个进程有特定的id成为PID。 

[plain] view plain copy

root@tecmint:~# ps  


PID TTY          TIME CMD  


4170 pts/1    00:00:00bash  


9628 pts/1    00:00:00ps  

使用‘-A‘选项可以列出所有的进程及其PID。 

[plain] view plain copy

root@tecmint:~# ps -A  




  PID TTY          TIME CMD  


   1  ?        00:00:01 init  


   2  ?        00:00:00 kthreadd  


   3  ?        00:00:01 ksoftirqd/0  


   5  ?        00:00:00 kworker/0:0H  


   7  ?        00:00:00 kworker/u:0H  


   8  ?        00:00:00 migration/0  


   9  ?        00:00:00 rcu_bh  


....  

注意:当你要知道有哪些进程在运行或者需要知道想杀死的进程PID时ps命令很管用。你可以把它与‘grep‘合用来查询指定的输出结果,例如:

[plain] view plain copy

root@tecmint:~# ps -A | grep -i ssh  


1500 ?        00:09:58 sshd  


4317 ?        00:00:00 sshd  

ps命令与grep命令用管道线分割可以得到我们想要的结果。 

25. 命令: kill 

kill是用来杀死已经无关紧要或者没有响应的进程.

假设你想杀死已经没有响应的‘apache2'进程,运行如下命令: 

[plain] view plain copy

root@tecmint:~# ps -A | grep -i apache2  


1285 ?        00:00:00 apache2  

搜索‘apache2'进程,找到PID并杀掉它.例如:在本例中‘apache2'进程的PID是1285..

[plain] view plain copy

root@tecmint:~# kill 1285 (to kill the process apache2)  

注意:每次你重新运行一个进程或者启动系统,每个进程都会生成一个新的PID.你可以使用ps命令获得当前运行进程的PID.

另一个杀死进程的方法是:

[plain] view plain copy

root@tecmint:~# pkill apache2  

注意:kill需要PID作为参数,pkill可以选择应用的方式,比如指定进程的所有者等.

26. 命令: whereis 

whereis的作用是用来定位命令的二进制文件\资源\或者帮助页.举例来说,获得ls和kill命令的二进制文件/资源以及帮助页: 

[plain] view plain copy

root@tecmint:~# whereis ls  


ls: /bin/ls/usr/share/man/man1/ls.1.gz  

[plain] view plain copy

root@tecmint:~# whereis kill  


kill: /bin/kill/usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz  

注意:当需要知道二进制文件保存位置时有用.

27. 命令: service 

service‘命令控制服务的启动、停止和重启,它让你能够不重启整个系统就可以让配置生效以开启、停止或者重启某个服务。 

在Ubuntu上启动apache2 server: 

[plain] view plain copy

root@tecmint:~# service apache2 start  


 * Starting web server apache2                                                                                                                                   

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 forServerName  


httpd (pid 1285) already running                        [ OK ]  

重启apache2 server:

[plain] view plain copy

root@tecmint:~# service apache2 restart  


* Restarting web server apache2                                                                                                                                 

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName  


 ... waiting .  

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1forServerName  [ OK ]  

停止apache2 server: 

[plain] view plain copy

root@tecmint:~# service apache2 stop  


 * Stopping web server apache2                                                                                                                                   

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 forServerName  


 ... waiting                                                                [ OK ]  

注意:要想使用service命令,进程的脚本必须放在‘/etc/init.d‘,并且路径必须在指定的位置。

如果要运行“service apache2 start”实际上实在执行“service /etc/init.d/apache2 start”.

28. 命令: alias 

alias是一个系统自建的shell命令,允许你为名字比较长的或者经常使用的命令指定别名。

我经常用ls -l‘命令,它有五个字符(包括空格)。于是我为它创建了一个别名‘l'。 

[plain] view plain copy

root@tecmint:~# alias l='ls -l'  

试试它是否能用: 

[plain] view plain copy

root@tecmint:~# l  


total 36  


drwxr-xr-x 3 tecmint tecmint 4096 May 10 11:14 Binary  


drwxr-xr-x 3 tecmint tecmint 4096 May 21 11:21 Desktop  

去掉’l'别名,要使用unalias命令:

[plain] view plain copy

root@tecmint:~# unalias l  

再试试: 

[plain] view plain copy

root@tecmint:~# l  


bash: l: command not found  

29.命令: df

报告系统的磁盘使用情况。在跟踪磁盘使用情况方面对于普通用户和系统管理员都很有用。 ‘df‘ 通过检查目录大小工作,但这一数值仅当文件关闭时才得到更新。

[plain] view plain copy

root@tecmint:~# df  

df’命令的更多例子请参阅 12 df Command Examples in Linux.

30. 命令: du 

估计文件的空间占用。 逐层统计文件(例如以递归方式)并输出摘要。 

[plain] view plain copy

root@tecmint:~# du  

注意: ‘df‘ 只显示文件系统的使用统计,但‘du‘统计目录内容。‘du‘命令的更详细信息请参阅10 du (Disk Usage) Commands.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容