你打算从Windows换到Linux上来,还是你刚好换到Linux上来?哎哟!!!我说什么呢,是什么原因你就出现我的世界里了。从我以往的经验来说,当我刚使用Linux,命令,终端啊什么的,吓了我一跳。我担心该记住多少命令,来帮助我完成所有任务。毫无疑问,在线文档,书籍,man pages以及社区帮了我一个大忙,但是我还是坚信有那么一篇文章记录了如何简单学习和理解命令的秘籍。这激发了我掌握Linux和使它容易使用的积极性。本文就是通往那里的阶梯。
1. ls命令
ls命令是列出目录内容(List Directory Contents)的意思。运行它就是列出文件夹里的内容,可能是文件也可能是文件夹。
1
2
3
4
5
6
7
|
root@tecmint:~
# ls
Android-Games Music
Pictures Public
Desktop Tecmint.com
Documents TecMint-Sync
Downloads Templates
|
“ls -l”命令已详情模式(long listing fashion)列出文件夹的内容。
1
2
3
4
5
6
7
8
9
10
11
|
root@tecmint:~
# ls -l
total 40588
drwxrwxr-x 2 ravisaive ravisaive 4096 May 8 01:06 Android Games
drwxr-xr-x 2 ravisaive ravisaive 4096 May 15 10:50 Desktop
drwxr-xr-x 2 ravisaive ravisaive 4096 May 16 16:45 Documents
drwxr-xr-x 6 ravisaive ravisaive 4096 May 16 14:34 Downloads
drwxr-xr-x 2 ravisaive ravisaive 4096 Apr 30 20:50 Music
drwxr-xr-x 2 ravisaive ravisaive 4096 May 9 17:54 Pictures
drwxrwxr-x 5 ravisaive ravisaive 4096 May 3 18:44 Tecmint.com
drwxr-xr-x 2 ravisaive ravisaive 4096 Apr 30 20:50 Templates
|
“ls -a”命令会列出文件夹里的所有内容,包括以”.”开头的隐藏文件。
1
2
3
4
5
6
7
8
9
10
11
12
|
root@tecmint:~
# ls -a
. .gnupg .dbus .goutputstream-PI5VVW .mission-control
.adobe deja-dup .grsync .mozilla .themes
.gstreamer-0.10 .mtpaint .thumbnails .gtk-bookmarks .thunderbird
.HotShots .mysql_history .htaccess .apport-ignore.xml .ICEauthority
.profile .bash_history .icons .bash_logout .fbmessenger
.jedit .pulse .bashrc .liferea_1.8 .pulse-cookie
.Xauthority .gconf .
local
.Xauthority.HGHVWW .cache
.gftp .macromedia .remmina .cinnamon .gimp-2.8
.
ssh
.xsession-errors .compiz .gnome teamviewer_linux.deb
.xsession-errors.old .config .gnome2 .zoncolor
|
注意:在Linux中,文件以“.”开头的就是隐藏文件,并且每个文件,文件夹,设备或者命令都是以文件对待。ls -l 命令输出:
- d (代表了是目录).
- rwxr-xr-x 是文件或者目录对所属用户,同一组用户和其它用户的权限。
- 上面例子中第一个ravisaive 代表了文件文件属于用户ravisaive
- 上面例子中的第二个ravisaive代表了文件文件属于用户组ravisaive
- 4096 代表了文件大小为4096字节.
- May 8 01:06 代表了文件最后一次修改的日期和时间.
- 最后面的就是文件/文件夹的名字
更多”ls”例子请查看 15 linux中ls命令实例
2. lsblk命令
“lsblk”就是列出块设备。除了RAM外,以标准的树状输出格式,整齐地显示块设备。
1
2
3
4
5
6
7
8
9
10
11
|
root@tecmint:~
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 232.9G 0 disk
├─sda1 8:1 0 46.6G 0 part /
├─sda2 8:2 0 1K 0 part
├─sda5 8:5 0 190M 0 part
/boot
├─sda6 8:6 0 3.7G 0 part [SWAP]
├─sda7 8:7 0 93.1G 0 part
/data
└─sda8 8:8 0 89.2G 0 part
/personal
sr0 11:0 1 1024M 0 rom
|
“lsblk -l”命令以列表格式显示块设备(而不是树状格式)。
1
2
3
4
5
6
7
8
9
10
11
|
root@tecmint:~
# lsblk -l
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 232.9G 0 disk
sda1 8:1 0 46.6G 0 part /
sda2 8:2 0 1K 0 part
sda5 8:5 0 190M 0 part
/boot
sda6 8:6 0 3.7G 0 part [SWAP]
sda7 8:7 0 93.1G 0 part
/data
sda8 8:8 0 89.2G 0 part
/personal
sr0 11:0 1 1024M 0 rom
|
注意:lsblk是最有用和最简单的方式来了解新插入的USB设备的名字,特别是当你在终端上处理磁盘/块设备时。
3. md5sum命令
“md5sum”就是计算和检验MD5信息签名。md5 checksum(通常叫做哈希)使用匹配或者验证文件的文件的完整性,因为文件可能因为传输错误,磁盘错误或者无恶意的干扰等原因而发生改变。
1
2
3
|
root@tecmint:~
# md5sum teamviewer_linux.deb
47790ed345a7b7970fc1f2ac50c97002 teamviewer_linux.deb
|
注意:用户可以使用官方提供的和md5sum生成签名信息匹对以此检测文件是否改变。Md5sum没有sha1sum安全,这点我们稍后讨论。
4. dd命令
“dd”命令代表了转换和复制文件。可以用来转换和复制文件,大多数时间是用来复制iso文件(或任何其它文件)到一个usb设备(或任何其它地方)中去,所以可以用来制作USB启动器。
1
|
root@tecmint:~
# dd if=/home/user/Downloads/debian.iso of=/dev/sdb1 bs=512M; sync
|
注意:在上面的例子中,usb设备就是sdb1(你应该使用lsblk命令验证它,否则你会重写你的磁盘或者系统),请慎重使用磁盘的名,切忌。
dd 命令在执行中会根据文件的大小和类型 以及 usb设备的读写速度,消耗几秒到几分钟不等。
5. uname命令
“uname”命令就是Unix Name的简写。显示机器名,操作系统和内核的详细信息。
1
2
3
|
root@tecmint:~
# uname -a
Linux tecmint 3.8.0-19-generic
#30-Ubuntu SMP Wed May 1 16:36:13 UTC 2013 i686 i686 i686 GNU/Linux
|
注意: uname显示内核类别, uname -a显示详细信息。上面的输出详细说明了uname -a
- “Linux“: 机器的内核名
- “tecmint“: 机器的分支名
- “3.8.0-19-generic“: 内核发布版本
- “#30-Ubuntu SMP“: 内核版本
- “i686“: 处理器架构
- “GNU/Linux“: 操作系统名
6. history命令
“history”命令就是历史记录。它显示了在终端中所执行过的所有命令的历史。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
root@tecmint:~
# history
1
sudo
add-apt-repository ppa:tualatrix
/ppa
2
sudo
apt-get update
3
sudo
apt-get
install
ubuntu-tweak
4
sudo
add-apt-repository ppa:diesch
/testing
5
sudo
apt-get update
6
sudo
apt-get
install
indicator-privacy
7
sudo
add-apt-repository ppa:atareao
/atareao
8
sudo
apt-get update
9
sudo
apt-get
install
my-weather-indicator
10
pwd
11
cd
&&
sudo
cp
-r unity
/6
/usr/share/unity/
12
cd
/usr/share/unity/icons/
13
cd
/usr/share/unity
|
注意:按住“CTRL + R”就可以搜索已经执行过的命令,它可以你写命令时自动补全。
1
|
(reverse-i-search)`
if
':
ifconfig
|
7. sudo命令
“sudo”(super user do)命令允许授权用户执行超级用户或者其它用户的命令。通过在sudoers列表的安全策略来指定。
1
|
root@tecmint:~# sudo add-apt-repository ppa:tualatrix/ppa
|
注意:sudo 允许用户借用超级用户的权限,然而”su”命令实际上是允许用户以超级用户登录。所以sudo比su更安全。
并不建议使用sudo或者su来处理日常用途,因为它可能导致严重的错误如果你意外的做错了事,这就是为什么在linux社区流行一句话:
“To err is human, but to really foul up everything, you need root password.”
“人非圣贤孰能无过,但是拥有root密码就真的万劫不复了。”
8. mkdir命令
“mkdir”(Make directory)命令在命名路径下创建新的目录。然而如果目录已经存在了,那么它就会返回一个错误信息”不能创建文件夹,文件夹已经存在了”(“cannot create folder, folder already exists”)
1
|
root@tecmint:~
# mkdir tecmint
|
注意:目录只能在用户拥有写权限的目录下才能创建。mkdir:不能创建目录`tecmint`,因为文件已经存在了。(上面的输出中不要被文件迷惑了,你应该记住我开头所说的-在linux中,文件,文件夹,驱动,命令,脚本都视为文件)
9. touch 命令
“touch”命令代表了将文件的访问和修改时间更新为当前时间。touch命令只会在文件不存在的时候才会创建它。如果文件已经存在了,它会更新时间戳,但是并不会改变文件的内容。
1
|
root@tecmint:~
# touch tecmintfile
|
注意:touch 可以用来在用户拥有写权限的目录下创建不存在的文件。
10. chmod 命令
“chmod”命令就是改变文件的模式位。chmod会根据要求的模式来改变每个所给的文件,文件夹,脚本等等的文件模式(权限)。
在文件(文件夹或者其它,为了简单起见,我们就使用文件)中存在3中类型的权限
1
2
3
|
Read (r)=4
Write(w)=2
Execute(x)=1
|
所以如果你想给文件只读权限,就设置为’4′;只写权限,设置权限为’2′;只执行权限,设置为1; 读写权限,就是4+2 = 6, 以此类推。
现在需要设置3种用户和用户组权限。第一个是拥有者,然后是用户所在的组,最后是其它用户。
1
|
rwxr-x--x abc.sh
|
这里root的权限是 rwx(读写和执行权限),
所属用户组权限是 r-x (只有读写权限, 没有写权限),
对于其它用户权限是 -x(只有只执行权限)
为了改变它的权限,为拥有者,用户所在组和其它用户提供读,写,执行权限。
1
|
root@tecmint:~
# chmod 777 abc.sh
|
三种都只有读写权限
1
|
root@tecmint:~
# chmod 666 abc.sh
|
拥有者用户有读写和执行权限,用户所在的组和其它用户只有可执行权限
1
|
root@tecmint:~
# chmod 711 abc.sh
|
注意:对于系统管理员和用户来说,这个命令是最有用的命令之一了。在多用户环境或者服务器上,对于某个用户,如果设置了文件不可访问,那么这个命令就可以解决,如果设置了错误的权限,那么也就提供了为授权的访问。
11. chown命令
“chown”命令就是改变文件拥有者和所在用户组。每个文件都属于一个用户组和一个用户。在你的目录下,使用”ls -l”,你就会看到像这样的东西。
1
2
3
4
|
root@tecmint:~
# ls -l
drwxr-xr-x 3 server root 4096 May 10 11:14 Binary
drwxr-xr-x 2 server server 4096 May 13 09:42 Desktop
|
在这里,目录Binary属于用户”server”,和用户组”root”,而目录”Desktop”属于用户“server”和用户组”server”
“chown”命令用来改变文件的所有权,所以仅仅用来管理和提供文件的用户和用户组授权。
1
2
3
4
|
root@tecmint:~# chown server:server Binary
drwxr-xr-x
3
server server
4096
May
10
11
:
14
Binary
drwxr-xr-x
2
server server
4096
May
13
09
:
42
Desktop
|
注意:“chown”所给的文件改变用户和组的所有权到新的拥有者或者已经存在的用户或者用户组。
12. apt命令
Debian系列以“apt”命令为基础,“apt”代表了Advanced Package Tool。APT是一个为Debian系列系统(Ubuntu,Kubuntu等等)开发的高级包管理器,在Gnu/Linux系统上,它会为包自动地,智能地搜索,安装,升级以及解决依赖。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
root@tecmint:~
# apt-get install mplayer
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
java-wrappers
Use
'apt-get autoremove'
to remove it.
The following extra packages will be installed:
esound-common libaudiofile1 libesd0 libopenal-data libopenal1 libsvga1 libvdpau1 libxvidcore4
Suggested packages:
pulseaudio-esound-compat libroar-compat2 nvidia-vdpau-driver vdpau-driver mplayer-doc netselect fping
The following NEW packages will be installed:
esound-common libaudiofile1 libesd0 libopenal-data libopenal1 libsvga1 libvdpau1 libxvidcore4 mplayer
0 upgraded, 9 newly installed, 0 to remove and 8 not upgraded.
Need to get 3,567 kB of archives.
After this operation, 7,772 kB of additional disk space will be used.
Do you want to
continue
[Y
/n
]? y
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
root@tecmint:~
# apt-get update
Hit http:
//ppa
.launchpad.net raring Release.gpg
Hit http:
//ppa
.launchpad.net raring Release.gpg
Hit http:
//ppa
.launchpad.net raring Release.gpg
Hit http:
//ppa
.launchpad.net raring Release.gpg
Get:1 http:
//security
.ubuntu.com raring-security Release.gpg [933 B]
Hit http:
//in
.archive.ubuntu.com raring Release.gpg
Hit http:
//ppa
.launchpad.net raring Release.gpg
Get:2 http:
//security
.ubuntu.com raring-security Release [40.8 kB]
Ign http:
//ppa
.launchpad.net raring Release.gpg
Get:3 http:
//in
.archive.ubuntu.com raring-updates Release.gpg [933 B]
Hit http:
//ppa
.launchpad.net raring Release.gpg
Hit http:
//in
.archive.ubuntu.com raring-backports Release.gpg
|
注意:上面的命令会导致系统整体的改变,所以需要root密码(查看提示符为”#”,而不是“$”).和yum命令相比,Apt更高级和智能。
见名知义,apt-cache用来搜索包中是否包含子包mplayer, apt-get用来安装,升级所有的已安装的包到最新版。
关于apt-get 和 apt-cache命令更多信息,请查看 25 APT-GET和APT-CACHE命令
13. tar命令
“tar”命令是磁带归档(Tape Archive),对创建一些文件的的归档和它们的解压很有用。
1
|
root@tecmint:~# tar -zxvf abc.tar.gz (记住
'z'
代表了.tar.gz)
|
1
|
root@tecmint:~# tar -jxvf abc.tar.bz2 (记住
'j'
代表了.tar.bz2)
|
1
|
root@tecmint:~# tar -cvf archieve.tar.gz(.bz2) /path/to/folder/abc
|
注意: “tar.gz“代表了使用gzip归档,“bar.bz2”使用bzip压缩的,它压缩的更好但是也更慢。
了解更多”tar 命令”的例子,请查看 18 Tar命名例子
14. cal 命令
“cal”(Calender),它用来显示当前月份或者未来或者过去任何年份中的月份。
1
2
3
4
5
6
7
8
9
|
root@tecmint:~
# cal
May 2013
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
|
显示已经过去的月份,1835年2月
1
2
3
4
5
6
7
8
|
root@tecmint:~# cal
02
1835
February
1835
Su Mo Tu We Th Fr Sa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
显示未来的月份,2145年7月。
1
2
3
4
5
6
7
8
9
|
root@tecmint:~# cal
07
2145
July
2145
Su Mo Tu We Th Fr Sa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
注意: 你不需要往回调整日历50年,既不用复杂的数据计算你出生那天,也不用计算你的生日在哪天到来,[因为它的最小单位是月,而不是日]。
15. date命令
“date”命令使用标准的输出打印当前的日期和时间,也可以深入设置。
1
2
3
|
root@tecmint:~# date
Fri May
17
14
:
13
:
29
IST
2013
|
1
2
3
|
root@tecmint:~# date --
set
=
'14 may 2013 13:57'
Mon May
13
13
:
57
:
00
IST
2013
|
注意:这个命令在脚本中十分有用,以及基于时间和日期的脚本更完美。而且在终端中改变日期和时间,让你更专业!!!(当然你需要root权限才能操作这个,因为它是系统整体改变)
16. cat命令
“cat”代表了连结(Concatenation),连接两个或者更多文本文件或者以标准输出形式打印文件的内容。
1
|
root@tecmint:~# cat a.txt b.txt c.txt d.txt abcd.txt
|
1
2
3
4
|
root@tecmint:~# cat abcd.txt
....
contents of file abcd
...
|
注意:“>>”和“>”调用了追加符号。它们用来追加到文件里,而不是显示在标准输出上。“>”符号会删除已存在的文件,然后创建一个新的文件。所以因为安全的原因,建议使用“>>”,它会写入到文件中,而不是覆盖或者删除。
在深入探究之前,我必须让你知道通配符(你应该知道通配符,它出现在大多数电视选秀中)。通配符是shell的特色,和任何GUI文件管理器相比,它使命令行更强大有力!如你所看到那样,在一个图形文件管理器中,你想选择一大组文件,你通常不得不使用你的鼠标来选择它们。这可能觉得很简单,但是事实上,这种情形很让人沮丧!
例如,假如你有一个有很多很多各种类型的文件和子目录的目录,然后你决定移动所有文件名中包含“Linux”字样的HTML文件到另外一个目录。如何简单的完成这个?如果目录中包含了大量的不同名的HTML文件,你的任务很巨大,而不是简单了。
在LInux CLI中,这个任务就很简单,就好像只移动一个HTML文件,因为有shell的通配符,才会如此简单。这些是特殊的字符,允许你选择匹配某种字符模式的文件名。它帮助你来选择,即使是大量文件名中只有几个字符,而且在大多数情形中,它比使用鼠标选择文件更简单。
这里就是常用通配符列表:
1
2
3
4
5
6
7
8
|
Wildcard Matches
* 零个或者更多字符
? 恰好一个字符
[abcde] 恰好列举中的一个字符
[a-e] 恰好在所给范围中的一个字符
[!abcde] 任何字符都不在列举中
[!a-e] 任何字符都不在所给的范围中
{debian,linux} 恰好在所给选项中的一整个单词
|
! 叫做非,带’!’的反向字符串为真
更多请阅读Linux cat 命令的实例 13 Linux中cat命令实例
17. cp 命令
“copy”就是复制。它会从一个地方复制一个文件到另外一个地方。
1
|
root@tecmint:~# cp /home/user/Downloads abc.tar.gz /home/user/Desktop (Return
0
when sucess)
|
注意: cp,在shell脚本中是最常用的一个命令,而且它可以使用通配符(在前面一块中有所描述),来定制所需的文件的复制。
18. mv 命令
“mv”命令将一个地方的文件移动到另外一个地方去。
1
|
root@tecmint:~# mv /home/user/Downloads abc.tar.gz /home/user/Desktop (Return
0
when sucess)
|
注意:mv 命令可以使用通配符。mv需谨慎使用,因为易懂系统的或者未授权的文件不但会导致安全性问题,而且可能系统崩溃。
19. pwd 命令
“pwd”(print working directory),在终端中显示当前工作目录的全路径。
1
2
3
|
root@tecmint:~# pwd
/home/user/Desktop
|
注意: 这个命令并不会在脚本中经常使用,但是对于新手,当从连接到nux很久后在终端中迷失了路径,这绝对是救命稻草。
20. cd 命令
最后,经常使用的“cd”命令代表了改变目录。它在终端中改变工作目录来执行,复制,移动,读,写等等操作。
1
|
root@tecmint:~# cd /home/user/Desktop
|
1
2
3
|
server@localhost:~$ pwd
/home/user/Desktop
|
注意: 在终端中切换目录时,cd就大显身手了。“cd ~”会改变工作目录为用户的家目录,而且当用户发现自己在终端中迷失了路径时,非常有用。“cd ..”从当前工作目录切换到(当前工作目录的)父目录。
这些命令肯定会让你在Linux上很舒服。但是这并不是结束。不久,我就会写一些其它的针对于中级用户的有用命令。例如,如果你熟练使用这些命令,欢呼吧,少年,你会发现你已从小白级别提升为了中级用户了。在下篇文章,我会介绍像“kill”,”ps”,”grep”等等命令,期待吧,我不会让你失望的。
也许你已经发现第一篇文章非常的有用,这篇文章是继《对初级Linux用户非常有用的20个命令》的一个延伸。 第一篇文章的目的是为新手准备,而这篇文章则是为了Linux的中高级用户。在这里你将学会如何进行自定义搜索,知道正在进行的进程和停掉进程,如何使用Linux的强势功能和如何在系统内编译C、C++和JAVA程序。
21. 命令: Find
搜索指定目录下的文件,从开始于父目录,然后搜索子目录。
1
2
3
4
5
6
7
8
9
10
11
|
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‘你可以使用文件名或者文件名的一部分来制定输出结果)
1
2
3
4
5
6
7
8
9
10
11
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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’
1
2
3
|
root@tecmint:~# grep tecmint /etc/passwd
tecmint:x:
1000
:
1000
:Tecmint,,,:/home/tecmint:/bin/bash
|
使用’-i’选项将忽略大小写。
1
2
3
|
root@tecmint:~# grep -i TECMINT /etc/passwd
tecmint:x:
1000
:
1000
:Tecmint,,,:/home/tecmint:/bin/bash
|
使用’-r’选项递归搜索所有自目录下包含字符串 “127.0.0.1“.的行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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
|
注意:您还可以使用以下选项:
1 -w搜索单词 (egrep -w ‘word1|word2‘ /path/to/file).
2 -c用于统计满足要求的行 (i.e., total number of times the pattern matched) (grep -c ‘word‘ /path/to/file).
3 –color彩色输出 (grep –color server /etc/passwd).
23. 命令: man
‘man‘是系统帮助页。Man提供命令所有选项及用法的在线文档。几乎所有的命令都有它们的帮助页,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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。
1
2
3
4
5
|
root@tecmint:~# ps
PID TTY TIME CMD
4170 pts/1 00:00:00 bash
9628 pts/1 00:00:00 ps
|
使用‘-A‘选项可以列出所有的进程及其PID。
1
2
3
4
5
6
7
8
9
10
11
|
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‘合用来查询指定的输出结果,例如:
1
2
3
4
|
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′进程,运行如下命令:
1
2
3
|
root@tecmint:~# ps -A | grep -i apache2
1285 ? 00:00:00 apache2
|
搜索‘apache2′进程,找到PID并杀掉它.例如:在本例中‘apache2′进程的PID是1285..
1
|
root@tecmint:~# kill 1285 (to kill the process apache2)
|
注意:每次你重新运行一个进程或者启动系统,每个进程都会生成一个新的PID.你可以使用ps命令获得当前运行进程的PID.
另一个杀死进程的方法是:
1
|
root@tecmint:~# pkill apache2
|
注意:kill需要PID作为参数,pkill可以选择应用的方式,比如指定进程的所有者等.
26. 命令: whereis
whereis的作用是用来定位命令的二进制文件\资源\或者帮助页.举例来说,获得ls和kill命令的二进制文件/资源以及帮助页:
1
2
3
|
root@tecmint:~# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
|
1
2
3
|
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:
1
2
3
4
|
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:
1
2
3
4
|
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:
1
2
3
4
|
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’。
1
|
root@tecmint:~# alias l='ls -l'
|
试试它是否能用:
1
2
3
4
5
6
7
8
9
10
11
12
|
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命令:
1
|
root@tecmint:~# unalias l
|
再试试:
1
2
3
|
root@tecmint:~# l
bash: l: command not found
|
开个玩笑,把一个重要命令的别名指定为另一个重要命令:
1
2
3
4
5
|
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‘ 通过检查目录大小工作,但这一数值仅当文件关闭时才得到更新。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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
估计文件的空间占用。 逐层统计文件(例如以递归方式)并输出摘要。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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.
31. 命令: rm
‘rm’ 标准移除命令。 rm 可以用来删除文件和目录。
删除目录
1
2
3
|
root@tecmint:~# rm PassportApplicationForm_Main_English_V1.0
rm: cannot remove `PassportApplicationForm_Main_English_V1.0': Is a directory
|
‘rm’不能直接删除目录,需要加上相应的’-rf’参数才可以。
1
|
root@tecmint:~# rm -rf PassportApplicationForm_Main_English_V1.0
|
警告: ”rm -rf” 命令是一个破坏性的命令,假如你不小心删除一个错误的目录。一旦你使用’rm -rf’ 删除一个目录,在目录中所有的文件包括目录本身会被永久的删除,所以使用这个命令要非常小心。
32. 命令: echo
echo 的功能正如其名,就是基于标准输出打印一段文本。它和shell无关,shell也不读取通过echo命令打印出的内容。然而在一种交互式脚本中,echo通过终端将信息传递给用户。它是在脚本语言,交互式脚本语言中经常用到的命令。
1
2
3
|
root@tecmint:~# echo "Tecmint.com is a very good website"
Tecmint.com is a very good website
|
创建一小段交互式脚本
1. 在桌面上新建一个文件,命名为 ‘interactive_shell.sh‘ (记住必须带 ‘.sh‘扩展名)。
2. 复制粘贴如下脚本代码,确保和下面的一致。
1
2
3
4
|
#!/bin/bash
echo "Please enter your name:"
read name
echo "Welcome to Linux $name"
|
接下来,设置执行权限并运行脚本。
1
|
root@tecmint:~# chmod 777 interactive_shell.sh
|
1
2
3
4
5
|
root@tecmint:~# ./interactive_shell.sh
Please enter your name:
Ravi Saive
Welcome to Linux Ravi Saive
|
注意: ‘#!/bin/bash‘ 告诉shell这是一个脚本,并且在脚本首行写上这句话是个好习惯。. ‘read‘ 读取给定的输出.
33. 命令: passwd
这是一个很重要的命令,在终端中用来改变自己密码很有用。显然的,因为安全的原因,你需要知道当前的密码。
1
2
3
4
5
6
7
8
9
|
root@tecmint:~# passwd
Changing password for tecmint.
(current) UNIX password: ********
Enter new UNIX password: ********
Retype new UNIX password: ********
Password unchanged [这里表示密码未改变,例如:新密码=旧密码]
Enter new UNIX password: #####
Retype new UNIX password:#####
|
34. 命令: lpr
这个命令用来在命令行上将指定的文件在指定的打印机上打印。
1
|
root@tecmint:~# lpr -P deskjet-4620-series 1-final.pdf
|
注意: ”lpq”命令让你查看打印机的状态(是开启状态还是关闭状态)和等待打印中的工作(文件)的状态。
35. 命令: cmp
比较两个任意类型的文件并将结果输出至标准输出。如果两个文件相同, ‘cmp‘默认返回0;如果不同,将显示不同的字节数和第一处不同的位置。
以下面两个文件为例:
file1.txt
1
2
3
|
root@tecmint:~# cat file1.txt
Hi My name is Tecmint
|
file2.txt
1
2
3
|
root@tecmint:~# cat file2.txt
Hi My name is tecmint [dot] com
|
比较一下这两个文件,看看命令的输出。
1
2
3
|
root@tecmint:~# cmp file1.txt file2.txt
file1.txt file2.txt differ: byte 15, line 1
|
36. 命令: wget
Wget是用于非交互式(例如后台)下载文件的免费工具.支持HTTP, HTTPS, FTP协议和 HTTP 代理。
使用wget下载ffmpeg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
root@tecmint:~# 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 命令: mount
mount 是一个很重要的命令,用来挂载不能自动挂载的文件系统。你需要root权限挂载设备。
在插入你的文件系统后,首先运行”lsblk”命令,识别出你的设备,然后把分配的设备名记下来。
1
2
3
4
5
6
7
8
9
10
|
root@tecmint:~# 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
|
从这个输出上来看,很明显我插入的是4GB的U盘,因而“sdb1”就是要挂载上来的文件系统。以root用户操作,然后切换到/dev目录,它是所有文件系统挂载的地方。
1
2
3
4
|
root@tecmint:~# su
Password:
root@tecmint:~# cd /dev
|
创建一个任何名字的目录,但是最好和引用相关。
1
2
|
root@tecmint:~# mkdir usb
|
现在将“sdb1”文件系统挂载到“usb”目录.
1
|
root@tecmint:~# mount /dev/sdb1 /dev/usb
|
现在你就可以从终端进入到/dev/usb或者通过X窗口系统从挂载目录访问文件。
是时候让程序猿见识见识Linux环境是多么丰富了!
38. 命令: gcc
gcc 是Linux环境下C语言的内建编译器。下面是一个简单的C程序,在桌面上保存为Hello.c (记住必须要有‘.c‘扩展名)。
1
2
3
4
5
6
|
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
|
编译
1
|
root@tecmint:~# gcc Hello.c
|
运行
1
2
3
|
root@tecmint:~# ./a.out
Hello world
|
注意: 编译C程序时,输出会自动保存到一个名为“a.out”的新文件,因此每次编译C程序 “a.out”都会被修改。 因此编译期间最好定义输出文件名.,这样就不会有覆盖输出文件的风险了。
用这种方法编译
1
|
root@tecmint:~# gcc -o Hello Hello.c
|
这里‘-o‘将输出写到‘Hello‘文件而不是 ‘a.out‘。再运行一次。
1
2
3
|
root@tecmint:~# ./Hello
Hello world
|
39. 命令: g++
g++是C++的内建编译器。下面是一个简单的C++程序,在桌面上保存为Add.cpp (记住必须要有‘.cpp‘扩展名)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#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;
}
|
编译
1
|
root@tecmint:~# g++ Add.cpp
|
运行
1
2
3
4
5
|
root@tecmint:~# ./a.out
Enter first number:
...
...
|
注意:编译C++程序时,输出会自动保存到一个名为“a.out”的新文件,因此每次编译C++程序 “a.out”都会被修改。 因此编译期间最好定义输出文件名.,这样就不会有覆盖输出文件的风险了。
用这种方法编译
1
2
|
root@tecmint:~# g++ -o Add Add.cpp
|
运行
1
2
3
4
5
|
root@tecmint:~# ./Add
Enter first number:
...
...
|
40. 命令: java
Java 是世界上使用最广泛的编程语言之一. 它也被认为是高效, 安全和可靠的编程语言. 现在大多数基于网络的服务都使用Java实现.
拷贝以下代码到一个文件就可以创建一个简单的Java程序. 不妨把文件命名为tecmint.java (记住: ’.java’扩展名是必需的).
1
2
3
4
5
|
class tecmint {
public static void main(String[] arguments) {
System.out.println("Tecmint ");
}
}
|
用javac编译tecmint.java
1
2
|
root@tecmint:~# javac tecmint.java
|
运行
1
|
root@tecmint:~# java tecmint
|
注意: 几乎所有的Linux发行版都带有gcc编译器, 大多数发行版都内建了g++ 和 java 编译器, 有些也可能没有. 你可以用apt 或 yum 安装需要的包.
请留下您宝贵的意见和想看到的文章类型. 稍后, 我会来一个很有趣主题, 关于Linux的一些鲜为人知的知识.