面向中级 Linux 用户的 20 个高级命令
您可能已经发现第一篇文章非常有用,本文是Linux 新手的 20 个有用命令的扩展。第一篇文章是针对新手的,而本文是针对中级用户和高级用户的。在这里,您将找到如何自定义搜索、了解正在运行的进程指南以终止它们、如何使您的Linux终端高效(这是一个重要方面)以及如何在 nix 中编译c、c++、java程序。
21. 命令:查找
在给定目录中搜索文件,按层次结构从父目录开始并移动到子目录。
root@example:~# 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@example:~# 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@example:~# 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 ”的文件。
阅读更多有关 Linux ' find ' 命令的示例,请参阅35 个 Linux 中的 Find 命令示例
22.命令:grep
' grep ' 命令在给定文件中搜索包含与给定字符串或单词匹配的行。在 ' /etc/passwd ' 中搜索 ' example ' 用户。
root@example:~# grep example /etc/passwd example:x:1000:1000:Example,,,:/home/example:/bin/bash
使用‘ -i ’选项忽略单词大小写和所有其他组合。
root@example:~# grep -i Example /etc/passwd example:x:1000:1000:Example,,,:/home/example:/bin/bash
递归搜索(-r),即读取每个目录下的所有文件以查找字符串“ 127.0.0.1 ”。
root@example:~# 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
注意:您可以将以下选项与grep一起使用。
- -w代表单词(egrep -w ' word1 | word2 '/path/to/file)。
- -c表示计数(即模式匹配的总次数)(grep -c ' word '/path/to/file)。
- –color用于彩色输出(grep –color server /etc/passwd)。
23. 命令:man
' man ' 是系统的手册页。Man 为命令的所有可能选项及其用法提供在线文档。几乎所有命令都附带相应的手册页。例如,
root@example:~# 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 ’(cat 命令的手册页)和‘ man ls ’(命令 ls的手册页)。
注意:手册页用于命令参考和学习。
24.命令:ps
ps(进程)通过一个称为PID的唯一ID来显示正在运行的进程的状态。
root@example:~# ps PID TTY TIME CMD 4170 pts/1 00:00:00 bash 9628 pts/1 00:00:00 ps
要列出所有进程的状态以及进程ID和PID,请使用选项“ -A ”。
root@example:~# 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来终止进程时,此命令非常有用。您可以将其与“ grep ”命令一起使用以查找自定义输出。例如,
root@example:~# ps -A | grep -i ssh 1500 ? 00:09:58 sshd 4317 ? 00:00:00 sshd
这里‘ ps ’与‘ grep ’命令一起用于查找我们需要的定制相关输出。
25. 命令:kill
好的,您可能已经从命令名称中了解了此命令的用途。此命令用于终止现在不相关或没有响应的进程。这是一个非常有用的命令,或者说是一个非常非常有用的命令。您可能熟悉频繁的 Windows 重启,因为大多数情况下无法终止正在运行的进程,并且如果被终止,则需要重新启动 Windows 以使更改生效,但在 Linux 世界中,没有这样的事情。在这里,您可以终止进程并启动它,而无需重新启动整个系统。
您需要一个进程的 pid (ps) 来终止它。
假设您想要终止可能没有响应的程序“ apache2 ”。运行“ ps -A ”以及grep命令。
root@example:~# ps -A | grep -i apache2 1285 ? 00:00:00 apache2
找到进程“ apache2 ”,记下它的pid并将其终止。例如,在我的情况下,“ apache2 ” pid 是“ 1285 ”。
root@example:~# kill 1285 (to kill the process apache2)
注意:每次重新运行某个进程或启动系统时,都会为每个进程生成一个新的pid ,您可以使用命令“ ps ”了解当前正在运行的进程及其pid。
终止同一进程的另一种方法是。
root@example:~# pkill apache2
注意:Kill 需要作业 ID /进程 ID来发送信号,而在pkill 中,您可以选择使用模式、指定进程所有者等。
26.命令:whereis
' whereis ' 命令用于查找命令的二进制文件、源代码和手册页。例如,查找命令 ' ls ' 和 ' kill ' 的二进制文件、源代码和手册页。
root@example:~# whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz
root@example:~# whereis kill kill: /bin/kill /usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz
注意:有时了解二进制文件的安装位置对于手动编辑很有用。
27. 命令:服务
' service ' 命令控制' service ' 的启动、停止或重启。此命令可以启动、重启或停止服务,而无需重启系统,以使更改生效。
在 Ubuntu 上启动 apache2 服务器
root@example:~# 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 ]
在 Ubuntu 上重新启动 apache2 服务器
root@example:~# 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 ]
在 Ubuntu 上停止 apache2 服务器
root@example:~# 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 ]
注意:所有进程脚本都位于“ /etc/init.d ”中,并且某些系统可能需要包含该路径,即,尽管运行“ service apache2 start ”,但系统仍会要求您运行“ /etc/init.d/apache2 start ”。
28. 命令:别名
别名是内置的 shell 命令,可让您为长命令或常用命令指定名称。
我经常使用 ' ls -l ' 命令,其中包括5 个字符(包括空格)。因此我为其创建了一个别名 ' l '。
root@example:~# alias l='ls -l'
检查它是否有效。
root@example:~# l total 36 drwxr-xr-x 3 example example 4096 May 10 11:14 Binary drwxr-xr-x 3 example example 4096 May 21 11:21 Desktop drwxr-xr-x 2 example example 4096 May 21 15:23 Documents drwxr-xr-x 8 example example 4096 May 20 14:56 Downloads drwxr-xr-x 2 example example 4096 May 7 16:58 Music drwxr-xr-x 2 example example 4096 May 20 16:17 Pictures drwxr-xr-x 2 example example 4096 May 7 16:58 Public drwxr-xr-x 2 example example 4096 May 7 16:58 Templates drwxr-xr-x 2 example example 4096 May 7 16:58 Videos
要删除别名“ l ”,请使用以下“ unalias ”命令。
root@example:~# unalias l
检查“ l ”是否仍然是别名。
root@example:~# 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 ”时,他得到的只是工作目录的位置。您可以稍后使用命令“ unalias ”删除别名,如上所述。
29. 命令:df
报告文件系统的磁盘使用情况。对于用户和系统管理员来说,跟踪磁盘使用情况很有用。' df ' 通过检查目录条目来工作,这些条目通常仅在文件关闭时更新。
root@example:~# 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 ”命令的更多示例,请阅读文章Linux 中的 12 个 df 命令示例。
30. 命令:du
估计文件空间使用情况。以层次结构(即递归方式)输出每个文件的磁盘使用情况摘要。
root@example:~# 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
Note: ‘df‘ only reports usage statistics on file systems, while ‘du‘, on the other hand, measures directory contents. For more ‘du‘ command examples and usage, read 10 du (Disk Usage) Commands.
31. Command: rm
The command ‘rm‘ stands for remove. rm is used to remove files (s) and directories.
Removing a directory
root@example:~# rm PassportApplicationForm_Main_English_V1.0 rm: cannot remove `PassportApplicationForm_Main_English_V1.0': Is a directory
The directory can’t be removed simply by ‘rm‘ command, you have to use ‘-rf‘ switch along with ‘rm‘.
root@example:~# rm -rf PassportApplicationForm_Main_English_V1.0
Warning: “rm -rf” command is a destructive command if accidently you make it to the wrong directory. Once you ‘rm -rf‘ a directory all the files and the directory itself is lost forever, all of a sudden. Use it with caution.
32. Command: echo
echo as the name suggest echoes a text on the standard output. It has nothing to do with shell, nor does shell reads the output of echo command. However in an interactive script, echo passes the message to the user through terminal. It is one of the command that is commonly used in scripting, interactive scripting.
root@example:~# echo "Example.com is a very good website" Example.com is a very good website
creating a small interactive script
1. create a file, named ‘interactive_shell.sh‘ on desktop. (Remember ‘.sh‘ extension is must).
2. copy and paste the below script, exactly same, as below.
#!/bin/bash echo "Please enter your name:" read name echo "Welcome to Linux $name"
Next, set execute permission and run the script.
root@example:~# chmod 777 interactive_shell.sh
root@example:~# ./interactive_shell.sh Please enter your name: Ravi Saive Welcome to Linux Ravi Saive
Note: ‘#!/bin/bash‘ tells the shell that it is an script an it is always a good idea to include it at the top of script. ‘read‘ reads the given input.
33. Command: passwd
This is an important command that is useful for changing own password in terminal. Obviously you need to know your current passowrd for Security reason.
root@example:~# passwd Changing password for example. (current) UNIX password: ******** Enter new UNIX password: ******** Retype new UNIX password: ******** Password unchanged [Here was passowrd remians unchanged, i.e., new password=old password] Enter new UNIX password: ##### Retype new UNIX password:#####
34. Command: lpr
This command print files named on command line, to named printer.
root@example:~# lpr -P deskjet-4620-series 1-final.pdf
Note: The ‘lpq‘ command lets you view the status of a printer (whether it’s up or not), and the jobs (files) waiting to be printed.
35. Command: cmp
compare two files of any type and writes the results to the standard output. By default, ‘cmp‘ Returns 0 if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported.
To provide examples for this command, lets consider two files:
file1.txt
root@example:~# cat file1.txt Hi My name is Example
file2.txt
root@example:~# cat file2.txt Hi My name is example [dot] com
Now, let’s compare two files and see output of the command.
root@example:~# cmp file1.txt file2.txt file1.txt file2.txt differ: byte 15, line 1
36. Command: wget
Wget is a free utility for non-interactive (i.e., can work in background) download of files from the Web. It supports HTTP, HTTPS, FTP protocols and HTTP proxies.
Download ffmpeg using wget
root@example:~# wget http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 --2013-05-22 18:54:52-- http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 216.34.181.59 Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80... connected. HTTP request sent, awaiting response... 302 Found Location: http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 [following] --2013-05-22 18:54:54-- http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 Resolving kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)... 92.46.53.163 Connecting to kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)|92.46.53.163|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 275557 (269K) [application/octet-stream] Saving to: ‘ffmpeg-php-0.6.0.tbz2’ 100%[===========================================================================>] 2,75,557 67.8KB/s in 4.0s 2013-05-22 18:55:00 (67.8 KB/s) - ‘ffmpeg-php-0.6.0.tbz2’ saved [275557/275557]
37. Command: mount
Mount is an important command which is used to mount a filesystem that don’t mount itself. You need root permission to mount a device.
First run ‘lsblk‘ after plugging-in your filesystem and identify your device and note down you device assigned name.
root@example:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 931.5G 0 disk ├─sda1 8:1 0 923.6G 0 part / ├─sda2 8:2 0 1K 0 part └─sda5 8:5 0 7.9G 0 part [SWAP] sr0 11:0 1 1024M 0 rom sdb 8:16 1 3.7G 0 disk └─sdb1 8:17 1 3.7G 0 part
From this screen it was clear that I plugged in a 4 GB pendrive thus ‘sdb1‘ is my filesystem to be mounted. Become a root to perform this operation and change to /dev directory where all the file system is mounted.
root@example:~# su Password:
root@example:~# cd /dev
Create a directory named anything but should be relevent for reference.
root@example:~# mkdir usb
Now mount filesystem ‘sdb1‘ to directory ‘usb‘.
root@example:~# mount /dev/sdb1 /dev/usb
Now you can navigate to /dev/usb from terminal or X-windows system and acess file from the mounted directory.
38. Command: gcc
gcc is the in-built compiler for ‘c‘ language in Linux Environment. A simple c program, save it on ur desktop as Hello.c (remember ‘.c‘ extension is must).
#include <stdio.h> int main() { printf("Hello world\n"); return 0; }
Compile it
root@example:~# gcc Hello.c
Run it
root@example:~# ./a.out Hello world
Note: On compiling a c program the output is automatically generated to a new file “a.out” and everytime you compile a c program same file “a.out” gets modified. Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.
Compile it this way
root@example:~# gcc -o Hello Hello.c
Here ‘-o‘ sends the output to ‘Hello‘ file and not ‘a.out‘. Run it again.
root@example:~# ./Hello Hello world
39. Command: g++
g++ is the in-built compiler for ‘C++‘ , the first object oriented programming language. A simple c++ program, save it on ur desktop as Add.cpp (remember ‘.cpp‘ extension is must).
#include <iostream> using namespace std; int main() { int a; int b; cout<<"Enter first number:\n"; cin >> a; cout <<"Enter the second number:\n"; cin>> b; cin.ignore(); int result = a + b; cout<<"Result is"<<" "<<result<<endl; cin.get(); return 0; }
Compile it
root@example:~# g++ Add.cpp
Run it
root@example:~# ./a.out Enter first number: ... ...
Note: On compiling a c++ program the output is automatically generated to a new file “a.out” and everytime you compile a c++ program same file “a.out” gets modified. Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.
Compile it this way
root@example:~# g++ -o Add Add.cpp
Run it
root@example:~# ./Add Enter first number: ... ...
40. Command: java
Java is one of the world’s highly used programming language and is considered fast, secure, and reliable. Most of the the web based service of today runs on java.
Create a simple java program by pasting the below test to a file, named example.java (remember ‘.java‘ extension is must).
class example { public static void main(String[] arguments) { System.out.println("Example "); } }
使用 javac 编译它
root@example:~# javac example.java
运行它
root@example:~# java example
注意:几乎每个发行版都附带了gcc 编译器,大多数发行版都内置了g++和java 编译器,而有些可能没有。您可以使用apt或yum获取所需的包。
别忘了在这里提及您的宝贵意见和您想看到的文章类型。我很快就会带着一个关于Linux鲜为人知的事实的有趣话题回来。