也许你已经发现第一篇文章非常的有用,这篇文章是继对初级Linux用户非常有用的20个命令 的一个延伸。 第一篇文章的目的是为新手准备的而这篇文章则是为了Linux的中高级用户。在这里你将学会如何进行自定义搜索,知道正在进行的进程和停掉进程,如何使用Linux的强势功能和如何在系统内编译C,C++和JAVA程序。
21. 命令: Find
搜索指定目录下的文件,从开始于父目录,然后搜索子目录。
root@tecmint:~# find -name *.sh
./Desktop/load.sh
./Desktop/test.sh
./Desktop/shutdown.sh
./Binary/firefox/run-mozilla.sh
./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh
./Downloads/kdewebdev-3.5.8/admin/doxygen.sh
./Downloads/kdewebdev-3.5.8/admin/cvs.sh
./Downloads/kdewebdev-3.5.8/admin/ltmain.sh
./Downloads/wheezy-nv-install.sh
注意: `-name‘选项是搜索大小写敏感。可以使用`-iname‘选项,这样在搜索中可以忽略大小写。(*是通配符,可以搜索所有的文件;‘.sh‘你可以使用文件名或者文件名的一部分来制定输出结果)
root@tecmint:~# find -iname *.SH ( find -iname *.Sh / find -iname *.sH)
./Desktop/load.sh
./Desktop/test.sh
./Desktop/shutdown.sh
./Binary/firefox/run-mozilla.sh
./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh
./Downloads/kdewebdev-3.5.8/admin/doxygen.sh
./Downloads/kdewebdev-3.5.8/admin/cvs.sh
./Downloads/kdewebdev-3.5.8/admin/ltmain.sh
./Downloads/wheezy-nv-install.sh
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
./home/server/Downloads/drupal-7.22.tar.gz
./home/server/Downloads/smtp-7.x-1.0.tar.gz
./home/server/Downloads/noreqnewpass-7.x-1.2.tar.gz
./usr/share/gettext/archive.git.tar.gz
./usr/share/doc/apg/php.tar.gz
./usr/share/doc/festival/examples/speech_pm_1.0.tar.gz
./usr/share/doc/argyll/examples/spyder2.tar.gz
./usr/share/usb_modeswitch/configPack.tar.gz
注意:以上命令查找根目录下和所有文件夹以及加载的设备的子目录下的所有包含‘tar.gz'的文件。
’find'命令的更详细信息请参考35 Find Command Examples in Linux
22. 命令: grep
‘grep‘命令搜索指定文件中包含给定字符串或者单词的行。举例搜索‘/etc/passwd‘文件中的‘tecmint'
root@tecmint:~# grep tecmint /etc/passwd
tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash
使用’-i'选项将忽略大小写。
root@tecmint:~# grep -i TECMINT /etc/passwd
tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash
使用’-r'选项递归搜索所有自目录下包含字符串 “127.0.0.1“.的行。
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
/etc/apache2/mods-available/status.conf: Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/ldap.conf: Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/info.conf: Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/proxy_balancer.conf:# Allow from 127.0.0.1 ::1
/etc/security/access.conf:#+ : root : 127.0.0.1
/etc/dhcp/dhclient.conf:#prepend domain-name-servers 127.0.0.1;
/etc/dhcp/dhclient.conf:# option domain-name-servers 127.0.0.1;
/etc/init/network-interface.conf: ifconfig lo 127.0.0.1 up || true
/etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).
/etc/java-6-openjdk/net.properties:# http.nonProxyHosts=localhost|127.0.0.1
/etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).
/etc/java-6-openjdk/net.properties:# ftp.nonProxyHosts=localhost|127.0.0.1
/etc/hosts:127.0.0.1 localhost
注意:您还可以使用以下选项:
- -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提供命令所有选项及用法的在线文档。几乎所有的命令都有它们的帮助页,例如:
root@tecmint:~# man man
MAN(1) Manual pager utils MAN(1)
NAME
man - an interface to the on-line reference manuals
SYNOPSIS
man [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-m system[,...]] [-M path] [-S list] [-e extension] [-i|-I]
[--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager] [-r prompt] [-7] [-E encoding] [--no-hyphenation] [--no-justification] [-p
string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
man -k [apropos options] regexp ...
man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
man -f [whatis options] page ...
man -l [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-P pager] [-r prompt] [-7] [-E encoding] [-p string] [-t] [-T[device]]
[-H[browser]] [-X[dpi]] [-Z] file ...
man -w|-W [-C file] [-d] [-D] page ...
man -c [-C file] [-d] [-D] page ...
man [-hV]
上面是man命令的系统帮助页,类似的有cat和ls的帮助页。
注意:系统帮助页是为了命令的使用和学习而设计的。
24. 命令: ps
ps命令给出正在运行的某个进程的状态,每个进程有特定的id成为PID。
root@tecmint:~# ps
PID TTY TIME CMD
4170 pts/1 00:00:00 bash
9628 pts/1 00:00:00 ps
使用‘-A‘选项可以列出所有的进程及其PID。
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‘合用来查询指定的输出结果,例如:
root@tecmint:~# ps -A | grep -i ssh
1500 ? 00:09:58 sshd
4317 ? 00:00:00 sshd
ps命令与grep命令用管道线分割可以得到我们想要的结果。
25. 命令: kill
也许你从命令的名字已经猜出是做什么的了,kill是用来杀死已经无关紧要或者没有响应的进程.它是一个非常有用的命令,而不是非常非常有用.你可能很熟悉Windows下要杀死进程可能需要频繁重启机器因为一个在运行的进程大部分情况下不能够杀死,即使杀死了进程也需要重新启动操作系统才能生效.但在linux环境下,事情不是这样的.你可以杀死一个进程并且重启它而不是重启整个操作系统.
杀死一个进程需要知道进程的PID.
假设你想杀死已经没有响应的‘apache2'进程,运行如下命令:
root@tecmint:~# ps -A | grep -i apache2
1285 ? 00:00:00 apache2
搜索‘apache2'进程,找到PID并杀掉它.例如:在本例中‘apache2'进程的PID是1285..
root@tecmint:~# kill 1285 (to kill the process apache2)
注意:每次你重新运行一个进程或者启动系统,每个进程都会生成一个新的PID.你可以使用ps命令获得当前运行进程的PID.
另一个杀死进程的方法是:
root@tecmint:~# pkill apache2
注意:kill需要PID作为参数,pkill可以选择应用的方式,比如指定进程的所有者等.
27. 命令: service
‘service‘命令控制服务的启动、停止和重启,它让你能够不重启整个系统就可以让配置生效以开启、停止或者重启某个服务。
在Ubuntu上启动apache2 server:
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 for ServerName
httpd (pid 1285) already running [ OK ]
重启apache2 server:
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.1 for ServerName [ OK ]
停止apache2 server:
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 for ServerName
... waiting [ OK ]
注意:要想使用service命令,进程的脚本必须放在‘/etc/init.d‘,并且路径必须在指定的位置。
如果要运行“service apache2 start”实际上实在执行“service /etc/init.d/apache2 start”.
28. 命令: alias
alias是一个系统自建的shell命令,允许你为名字比较长的或者经常使用的命令指定别名。
我经常用‘ls -l‘命令,它有五个字符(包括空格)。于是我为它创建了一个别名‘l'。
root@tecmint:~# alias l='ls -l'
试试它是否能用:
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
drwxr-xr-x 2 tecmint tecmint 4096 May 21 15:23 Documents
drwxr-xr-x 8 tecmint tecmint 4096 May 20 14:56 Downloads
drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Music
drwxr-xr-x 2 tecmint tecmint 4096 May 20 16:17 Pictures
drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Public
drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Templates
drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Videos
去掉’l'别名,要使用unalias命令:
root@tecmint:~# unalias l
再试试:
root@tecmint:~# l
bash: l: command not found
开个玩笑,把一个重要命令的别名指定为另一个重要命令:
alias cd='ls -l' (set alias of ls -l to cd)
alias su='pwd' (set alias of pwd to su)
....
(You can create your own)
....
想想多么有趣,现在如果你的朋友敲入‘cd'命令,当他看到的是目录文件列表而不是改变目录;当他试图用’su‘命令时,他会进入当前目录。你可以随后去掉别名,向他解释以上情况。
29.命令: df
报告系统的磁盘使用情况。在跟踪磁盘使用情况方面对于普通用户和系统管理员都很有用。 ‘df‘ 通过检查目录大小工作,但这一数值仅当文件关闭时才得到更新。
root@tecmint:~# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 47929224 7811908 37675948 18% /
none 4 0 4 0% /sys/fs/cgroup
udev 1005916 4 1005912 1% /dev
tmpfs 202824 816 202008 1% /run
none 5120 0 5120 0% /run/lock
none 1014120 628 1013492 1% /run/shm
none 102400 44 102356 1% /run/user
/dev/sda5 184307 79852 94727 46% /boot
/dev/sda7 95989516 61104 91045676 1% /data
/dev/sda8 91953192 57032 87218528 1% /personal
‘df’命令的更多例子请参阅 12 df Command Examples in Linux.
30. 命令: du
估计文件的空间占用。 逐层统计文件(例如以递归方式)并输出摘要。
root@tecmint:~# du
8 ./Daily Pics/wp-polls/images/default_gradient
8 ./Daily Pics/wp-polls/images/default
32 ./Daily Pics/wp-polls/images
8 ./Daily Pics/wp-polls/tinymce/plugins/polls/langs
8 ./Daily Pics/wp-polls/tinymce/plugins/polls/img
28 ./Daily Pics/wp-polls/tinymce/plugins/polls
32 ./Daily Pics/wp-polls/tinymce/plugins
36 ./Daily Pics/wp-polls/tinymce
580 ./Daily Pics/wp-polls
1456 ./Daily Pics
36 ./Plugins/wordpress-author-box
16180 ./Plugins
12 ./May Articles 2013/Xtreme Download Manager
4632 ./May Articles 2013/XCache
注意: ‘df‘ 只显示文件系统的使用统计,但‘du‘统计目录内容。‘du‘命令的更详细信息请参阅10 du (Disk Usage) Commands.