总结Linux常用50条命令

在linux中一切皆文件


linux中的绝对路径和相对路径。路径就是文件存放的位置。
绝对路径的写法是由根目录“/”写起,相对路径的写法不是由根目录写起
  eg:绝对路径 cd /bin
  相对路径 cd python
1.命令mkdir  该命令是用于创建目录,英文缩写为:make directory,格式 mkdir [-mp] [目录名称] -m用于制定要创建目录的权限(该选项不常用)-p该选项可以创建串联级目录,若上一个目录不存在也不会报错,若有已存在的目录也不会报错。
  

1  eg:[root@localhost tmp]$ mkdir /tmp/test/py
2       mkdir: cannot create directory `/tmp/test/py': No such file or directory
3       
4     [root@localhost /]# mkdir -p /tmp/test/py
5     [root@localhost tmp]# ls -ld /tmp/test/
6     drwxr-xr-x. 3 root root 4096 May 2 18:31 /tmp/test/
7     [root@localhost tmp]# ls -ld /tmp/test/py
8     drwxr-xr-x. 2 root root 4096 May 2 18:31 /tmp/test/py

 


2.命令rm 与  命令rmdir   (rm既可以删除文件也可以删除目录,而rmdir只能删除空目录不常用)
   rm -r:删除目录的选项,当使用该选项时,系统会询问是否删除,输入“y”表示同意,“n”表示不同意
    

1 eg:[root@localhost tmp]# rm -r /tmp/test/py
2         rm: remove directory `/tmp/test/py'

 


另外rm -f 可以善春非空目录。
    rm -f 强制删除,不再询问。
      

1 eg:[root@localhost tmp]# rm -f /tmp/test/py
2       rm: cannot remove `/tmp/test/py': Is a directory

 

 


这样用该命令,系统提示“这是一个目录”,因此在使用rm删除目录的时候,一定要加上 -r
    

1 rm -rf /tmp/test/py
2     eg;[root@localhost ]# ls -ld /tmp/test/py
3 ls: cannot access /tmp/test/py: No such file or directory

 

 


注意:经常使用的选项是-rf,但是在rm -rf后面不能加“/”,这样会把整个系统文件全部删除。
 3.命令mv 用于移动目录或者文件,其还有重命名的作用,是英文move的缩写。命令格式  mv [选项] [源文件或目录] 目标文件或目录]
  目标文件是目录,但该目录不存在。
  目标文件是目录,但该目录存在。
(如果该目录存在,会把源文件或者目录移动到该目录中,如果该目录不存在,则会把源目录重命名为给定的目标文件)
  目标文件是文件,且该文件不存在。
   

 1  eg:
 2     [root@localhost home]# mkdir snow snowy
 3     [root@localhost home]# ls
 4     123 456 py snow snowy
 5     [root@localhost home]# mv snow snowman
 6     [root@localhost home]# ls
 7     123 456 py snowman snowy
 8     [root@localhost home]# mv snowman snowy
 9     [root@localhost home]# ls 
10     123 456 py snowy
11     [root@localhost home]# cd snowy
12     [root@localhost snowy]# ls
13     snowman
14      
15     [root@localhost snowman]# touch file_one
16     [root@localhost snowman]# ls
17     file_one
18     [root@localhost snowman]# mv file_one file_two
19     [root@localhost snowman]# ls
20     file_two
21     
22     [root@localhost snowman]# mkdir -p file
23     [root@localhost snowman]# ls
24     file file_two
25     [root@localhost snowman]# mv file_two file
26     [root@localhost snowman]# ls
27     file
28     [root@localhost snowman]# cd file
29     [root@localhost file]# ls 
30     file_two
31   [root@localhost file]#

 

 


  目标文件是文件,且文件存在。
(如果该文件存在,系统询问是否覆盖。如果该文件不存在,则会把源文件重命名为给定的目标文件名)
 eg:mv /tmp/python file  /home/py
4.命令cp,是copy的缩写,格式 cp [选项] [来源文件] [目的文件]
-r 用于复制一个目录,必须加-r选项,否则不能复制目录。
-i 是安全选项,若遇到一个已经存在的文件,会询问是否覆盖。

 1 eg:[root@localhost snowy]# mkdir snow
 2 [root@localhost snowy]# ls
 3 snow snowman snow_white
 4 [root@localhost snowy]# cp snow_white snow
 5 [root@localhost snowy]# ls
 6 snow snowman snow_white
 7 [root@localhost snowy]# cd snow
 8 [root@localhost snow]# ls 
 9 snow_white
10 [root@localhost snow]#

 


5.命令which,用于查找某个命令的绝对路径。

eg:[root@localhost snow]# which pwd
/bin/pwd
[root@localhost snow]# 

 


6.命令touch,该命令是:如果有这个文件,则会改变这个文件的访问时间;如果没有这个文件就会创建这个文件(一般用于创建新文件)。

eg:[root@localhost home]# ls 
123 456 py snowy
[root@localhost home]# cd snowy
[root@localhost snowy]# ls
snowman
[root@localhost snowy]# touch snow_white
[root@localhost snowy]# ls
snowman snow_white

 


7.命令cat用于连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,
注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容。因此,一般用more等命令分屏显示。为了控制滚屏,可以按Ctrl+S键,停止滚屏;按Ctrl+Q键可以恢复滚屏。按Ctrl+C(中断)键可以终止该命令的执行,并且返回Shell提示符状态。
cat [选项] [参数]
-n 或 -number 由1开始对所有输出的行数编号

eg:[root@localhost home]# echo 'good luck' > home/snowy/snow_while
bash: home/snowy/snow_while: No such file or directory
[root@localhost home]# echo 'good luck' > snowy/snow_while
[root@localhost home]# cd snowy
[root@localhost snowy]# ls 
snow snowman snow_while snow_white
[root@localhost snowy]# vi snow_while

 


(上面重定向的时候,文件名输错了,导致新建了一个文件,snow_while)
-A 显示所有的内容,包含特殊字符。
eg:[root@localhost snowy]# cat -A snow_while
good luck$
god god$
(Ps:命令tac,也是把文件的内容显示在屏幕上,只不过先显示的是最后一行,然后显示第二行,最后才显示第一行)
8.命令 echo,用于在shell中打印shell变量的值,或者直接输出指定的字符串。功能是在显示器上显示一段文字,一般起到一个提示的作用。
echo [选项][参数]
-e 激活转义字符
其中出现的“>”作用是重定向,把前面的内容输入到后面的文件中,符号“>>”是追加的意思,当使用“>”时,如果文件
中有内容则会删除文件中原有的内容,而使用符号“>>”不会删除原有的内容。
9.命令 more 也是用于查看一个文件内容,后面直接跟文件名。当文件内容太多,cat看不了的话,就应该用more来解决
这个问题。
more [语法][参数]
按Space键:显示文本的下一屏内容。
按Enier键:只显示文本的下一行内容。
按斜线符|:接着输入一个模式,可以在文本中寻找下一个相匹配的模式。
按H键:显示帮助屏,该屏上有相关的帮助信息。 按B键:显示上一屏内容。
按Q键:退出rnore命令。
eg: more -c -10 file 显示文件file的内容,每10行显示一次,而且在现实之前先清屏。
10.命令less ,less命令的作用与more十分相似,都可以用来浏览文字档案的内容,不同的是less命令允许用户向前或
向后浏览文件,而more命令只能向前浏览。用less命令显示文件时,用PageUp键向上翻页,用PageDown键向下翻页。
要退出less程序,应按Q键。
less[选项][参数]
小知识点:"/"查找一个字符串,可以用?来代替。不同的是“/”从当前行向下搜索,而“?”从当前行,向上搜索。
“n”显示下一个查找出的字符串。
11.命令head ,用于显示文件的开头的内容。在默认情况下,head命令显示文件的头10行内容。
head [参数] [参数]
-n<数字>:指定显示头部内容的行数;

eg:[root@localhost /]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

[root@localhost /]# head -5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

 


(ps;命令tail,tail命令用于输入文件中的尾部内容。tail命令默认在屏幕上显示指定文件的末尾10行。)

12.linux中的文件
一个liunx目录或者文件,都会有一个所属主和所属组。
所属主食指文件的拥有者;所属组是指该文件所属主所在的一个组。

eg:[root@localhost /]# ls -l
total 90
dr-xr-xr-x. 2 root root 4096 Apr 26 08:38 bin
dr-xr-xr-x. 5 root root 1024 Apr 26 15:58 boot
drwxr-xr-x. 18 root root 3800 May 2 18:01 dev
drwxr-xr-x. 101 root root 4096 May 2 18:01 etc
drwxr-xr-x. 6 root root 4096 May 2 20:19 home

 


例子中,第3 4列的root就是所属主和所属组。

通过ls-al显示的结果中看文件属性示意图
列中的第1个字符代表这个文件是“目录、文件或链接文件等”
若是【d】则代表该条记录是目录;
若是【-】则代表是文件;
若是【|】则表示为连接文件(linkfile);
若是【b】则表示设备文件里面的可供存储的接口设备;
若是【c】则表示设备文件里面的串口端口设备,例如键盘、鼠标。

接下来的字符中,以3个为一组,且均为”rwx”:
其中【r】代表可读(read);
其中【w】代表可写(write);
其中【x】代表可执行(execute);
这3个权限的位置不会改变,如果没有相应的权限,就会出现减号【-】


第2列表示有多少文件名连接到此节点(i-node)
第3列表表示这个文件(或目录)的“所有者账号”
第4列表表示这个文件的所属用户组
第5列为这个文件的大小,默认单位为B
第6列为这个文件的创建文件日期或者是最近的修改日期
第7列为文件名
13.命令chgrp,用来改变文件或目录所属的用户组。该命令用来改变指定文件所属的用户组。其中,组名可以是用户组
的id,也可以是用户组的组名。文件名可以 是由空格分开的要改变属组的文件列表,也可以是由通配符描述的文件集
合。如果用户不是该文件的文件主或超级用户(root),则不能改变该文件的组。

eg:[root@localhost py]# groupadd testsnow
[root@localhost py]# touch snowfile1
[root@localhost py]# ls -l snowfile1
-rw-r--r--. 1 root root 0 May 3 20:57 snowfile1
[root@localhost py]# chgrp testsnow snowfile1
[root@localhost py]# ls -l snowfile1
-rw-r--r--. 1 root testsnow 0 May 3 20:57 snowfile1

 


14.命令chown,改变某个文件或目录的所有者和所属的组,该命令可以向某个用户授权,使该用户变成指定文件的所有者或
者改变文件所属的组。用户可以是用户或者是用户D,用户组可以是组名或组id。文件名可以使由空格分开的文件列表,在
文件名中可以包含通配符。
只有文件主和超级用户才可以便用该命令。

chown [选项][参数] -R或——recursive:递归处理,将指定目录下的所有文件及子目录一并处理;

eg:[root@localhost py]# useradd user1
[root@localhost py]# touch testsnow/test
[root@localhost py]# chown user1 test
chown: cannot access `test': No such file or directory
[root@localhost py]# cd testsnow
[root@localhost testsnow]# chown user1 test
[root@localhost testsnow]# ls -l test
-rw-r--r--. 1 user1 root 0 May 3 21:02 test
[root@localhost testsnow]#

[root@localhost testsnow]# ll 
total 0
-rw-r--r--. 1 user1 root 0 May 3 21:02 test
[root@localhost testsnow]# chown -R user1:testsnow test
[root@localhost testsnow]# ls
test
[root@localhost testsnow]# ll
total 0
-rw-r--r--. 1 user1 testsnow 0 May 3 21:02 test
[root@localhost testsnow]# 

 

15.chmod命令,用来变更文件或目录的权限。用户可以使用chmod指令去变更文件与目录的权限,设置方式采用文字或数字
代号皆可。符号连接的权限无法变更,如果用户对符号连接修改权限,其改变会作用在被连接的原始文件。

chmod(选项)(参数)

liunx中使用数字代替“rwx”,r = 4,w = 2,x = 1, - 等于0.-R选项的作用也是级联更改。
在liunx中,一个目录的默认权限为755,而一个文件的默认权限是644.

eg:[root@localhost testsnow]# ls -ld
drwxr-xr-x. 2 root root 4096 May 3 21:02 .
[root@localhost testsnow]# ls -ld test
-rw-r--r--. 1 user1 testsnow 0 May 3 21:02 test
[root@localhost testsnow]# chmod 750 test
[root@localhost testsnow]# ls -ld test
-rwxr-x---. 1 user1 testsnow 0 May 3 21:02 test

 

chmod还支持rwx的方式来设置权限,可以使用 u g o 分别代表user group others的属性,a代表all.

eg:[root@localhost testsnow]# chmod u=rwx,og=rx test
[root@localhost testsnow]# ls -ld test
-rwxr-xr-x. 1 user1 testsnow 0 May 3 21:02 test

 

chmod的还可以针对u g o a 增加或者减少它们的某个权限

eg:[root@localhost testsnow]# chmod u-x test
[root@localhost testsnow]# ls -ld test
-rw-r-xr-x. 1 user1 testsnow 0 May 3 21:02 test

 


16.命令chattr,命令用来改变文件属性。这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下8种
模式: a:让文件或目录仅供附加用途; b:不更新文件或目录的最后存取时间; c:将文件或目录压缩后存放;
d:将文件或目录排除在倾倒操作之外; i:不得任意更动文件或目录; s:保密性删除文件或目录;
S:即时更新文件或目录; u:预防意外删除。

chattr [选项] 或 chattr [+-=][Asaci][文件或目录名]
在8种模式中,常用的是a 和 i

eg:[root@localhost testsnow]# mkdir test2
[root@localhost testsnow]# ll
total 4
-rw-r-xr-x. 1 user1 testsnow 0 May 3 21:02 test
drwxr-xr-x. 2 root root 4096 May 4 00:04 test2
[root@localhost testsnow]# chattr +i test2
[root@localhost testsnow]# touch test2/test1
touch: cannot touch `test2/test1': Permission denied
[root@localhost testsnow]# chattr -i test2
[root@localhost testsnow]# touch test2/test1
[root@localhost testsnow]# chattr +i test2
[root@localhost testsnow]# rm -f test2/test1
rm: cannot remove `test2/test1': Permission denied
[root@localhost testsnow]# 
(在给test2目录增加了i权限后,虽然是root账户也不能在test2中创建或者删除test1文件)

[root@localhost testsnow]# chattr -i test2
[root@localhost testsnow]# touch test2/test3
[root@localhost testsnow]# ls test2
test1 test3
[root@localhost testsnow]# chattr +a test2
[root@localhost testsnow]# rm -f test2/test1
rm: cannot remove `test2/test1': Operation not permitted
[root@localhost testsnow]# touch test2/test4
[root@localhost testsnow]# ls test2
test1 test3 test4

 


17.命令which ,用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录。which指令会
在环境变量$PATH设置的目录里查找符合条件的文件。也就是说,使用which命令,就可以看到某个系统命令是否存在,
以及执行的到底是哪一个位置的命令。
which(选项)(参数)

18.命令find,用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,
不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

-mtime<24小时数>:查找在指定时间曾被更改过的文件或目录,单位以24小时计算;
-name<范本样式>:指定字符串作为寻找文件或目录的范本样式;

find(选项)(参数)

eg:[root@localhost testsnow]# find /tmp/ -mtime -1
/tmp/
/tmp/.X0-lock
/tmp/vmware-root
/tmp/vmware-root/vmware-db.pl.2275
/tmp/vmware-root/vmware-db.pl.2282
/tmp/vmware-root/vmware-db.pl.2269
/tmp/vmware-root/vmware-db.pl.2272
/tmp/orbit-gdm
/tmp/orbit-gdm/linc-a34-0-1f62f913b8b24
/tmp/orbit-gdm/bonobo-activation-server-ec8d57b0a591075f1b131c1000000037-ior
/tmp/orbit-gdm/linc-a36-0-4f545290a6420
/tmp/orbit-gdm/linc-a18-0-2a8fbedb3a6ea
/tmp/orbit-gdm/linc-a67-0-2545db41d4629
/tmp/orbit-gdm/bonobo-activation-register-ec8d57b0a591075f1b131c1000000037.lock


[root@localhost testsnow]# find .-name test2
find: `.-name': No such file or directory
test2
test2/test1
test2/test3
test2/test4



19.命令groupadd,用于创建一个新的工作组,新工作组的信息将被添加到系统文件中。

groupadd(选项)(参数)

eg:[root@localhost testsnow]# groupadd grptest1
[root@localhost testsnow]# tail -n1 /etc/group
grptest1:x:503:

 

-g:指定新建工作组的id; -r:创建系统工作组,系统工作组的组ID小于500;

eg:[root@localhost testsnow]# groupadd -g 511 grptest2
[root@localhost testsnow]# tail -n2 /etc/group
grptest1:x:503:
grptest2:x:511:

 


20.命令groupdel,用于删除指定的工作组,本命令要修改的系统文件包括/ect/group和/ect/gshadow。若该群组中仍包括某
些用户,则必须先删除这些用户后,方能删除群组。

groupdel(参数)

eg:[root@localhost testsnow]# groupdel grptest2
[root@localhost testsnow]# tail -n3 /etc/group
testsnow:x:501:
user1:x:502:
grptest1:x:503:

 


若组中有用户,必须删除用户后才能删除该组。

eg:[root@localhost testsnow]# groupdel user1
groupdel: cannot remove the primary group of user 'user1'

 


21.命令useradd,可用来建立用户帐号。帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号。
使用useradd指令所建立的帐号,实际上是保存在/etc/passwd文本文件中。
-s:指定用户登入后所使用的shell; -u:指定用户id。-d<登入目录>:指定用户登入时的启始目录;
-M:不要自动建立用户的登入目录;-g<群组>:指定用户所属的群组;

useradd(选项)(参数)

eg:[root@localhost testsnow]# useradd test0
[root@localhost testsnow]# tail -n1 /etc/passwd
test0:x:502:504::/home/test0:/bin/bash
[root@localhost testsnow]# tail -n1 /etc/group
test0:x:504:

 


命令useradd,不加任何选项,直接跟用户名,会创建一个跟用户名同名的组。指定组则需要加入参数。

[root@localhost testsnow]# useradd -u510 -g 513 -M -s /sbin/nologin user11
useradd: group '513' does not exist
[root@localhost testsnow]# useradd -u510 -g 502 -M -s /sbin/nologin user11
[root@localhost testsnow]# tail -n2 /etc/passwd
test0:x:502:504::/home/test0:/bin/bash
user11:x:510:502::/home/user11:/sbin/nologin
[root@localhost testsnow]# tail -n2 /etc/group
grptest1:x:503:
test0:x:504:

 


22.命令userdel,用于删除给定的用户,以及与用户相关的文件。若不加选项,则仅删除用户帐号,而不删除相关文件。
userdel(选项)(参数)
-f:强制删除用户,即使用户当前已登录; -r:删除用户的同时,删除与用户相关的所有文件。

[root@localhost testsnow]# ls -ld /home/user12
ls: cannot access /home/user12: No such file or directory
[root@localhost testsnow]# ls -ld /home/user1
drwx------. 4 user1 user1 4096 May 3 21:01 /home/user1
[root@localhost testsnow]# userdel user1
userdel: group user1 is the primary group of another user and is not removed.
[root@localhost testsnow]# useradd user12
[root@localhost testsnow]# ls
test test2
[root@localhost testsnow]# ls -ld /home/user12
drwx------. 4 user12 user12 4096 May 4 01:27 /home/user12
[root@localhost testsnow]# userdel user12
[root@localhost testsnow]# ls -ld /home/user12
drwx------. 4 511 511 4096 May 4 01:27 /home/user12
[root@localhost testsnow]# ls -ld /home/test2
ls: cannot access /home/test2: No such file or directory
[root@localhost testsnow]# ls -ld /home/test10
ls: cannot access /home/test10: No such file or directory
[root@localhost testsnow]# ls -ld /home/test1
ls: cannot access /home/test1: No such file or directory
[root@localhost testsnow]# ls -ld /home/test
ls: cannot access /home/test: No such file or directory
[root@localhost testsnow]# useradd test12
[root@localhost testsnow]# ls -ld /home/test12/
drwx------. 4 test12 test12 4096 May 4 01:29 /home/test12/
[root@localhost testsnow]# userdel -r test12
[root@localhost testsnow]# ls -ld /home/test12
ls: cannot access /home/test12: No such file or directory
[root@localhost testsnow]# 

 


23.命令passwd,用于设置用户的认证信息,包括用户密码、密码过期时间等。系统管理者则能用它管理系统用户的密码。
只有管理者可以指定用户名称,一般用户只能变更自己的密码。

passwd(选项)(用户名)

-d:删除密码,仅有系统管理者才能使用; -f:强制执行; -k:设置只有在密码过期失效后,方能更新;
-l:锁住密码; -s:列出密码的相关信息,仅有系统管理者才能使用; -u:解开已上锁的帐号。

ps:只有在root权限下,才能更改其他账户的密码发,普通账户只能修改自己的密码。

eg:[root@localhost /]# passwd test0
Changing password for user test0.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@localhost /]# 

 


24.命令df,用于显示磁盘分区上的可使用的磁盘空间。默认显示单位为KB。可以利用该命令来获取硬盘被占用了多少空间,
目前还剩下多少空间等信息。

df(选项)(参数)

常用选项:-h或--human-readable:以可读性较高的方式来显示信息;

eg:[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 3.3G 14G 20% /
tmpfs 495M 72K 495M 1% /dev/shm
/dev/sda1 283M 37M 232M 14% /boot

[root@localhost /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 18208184 3432564 13844036 20% /
tmpfs 506228 72 506156 1% /dev/shm
/dev/sda1 289293 36874 237059 14% /boot

 


25.命令du,也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df
命令有一些区别的。

du [选项][文件]

常用的选项:-s或--summarize 仅显示总计,只列出最后加总的值。

-h或--human-readable 以K,M,G为单位,提高信息的可读性。

eg:[root@localhost /]# du -sh /etc/passwd
4.0K    /etc/passwd

 


26.liunx中压缩文件

.gz 表示由gzip压缩工具压缩的文件
.bz2 表示由bzip2压缩工具压缩的文件
.tar 表示由tar打包程序打包的文件(tar并没有压缩功能,只是把一个目录合并成一个文件)
.tar.gz 表示由tar打包程序后,再由gzip压缩。
.tar.bz2 表示由tar打包,然后再由bzip2压缩

命令gzip,gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。gzip不仅可以用来
压缩大的、较少使用的文件以节省磁盘空间,还可以和tar命令一起构成Linux操作系统中比较流行的压缩文件格式。
据统计,gzip命令对文本文件有60%~70%的压缩率。减少文件大小有两个明显的好处,一是可以减少存储空间,二是
通过网络传输文件时,可以减少传输的时间。

gzip(选项)(参数)

-d或--decompress或----uncompress:解开压缩文件;
-#:表示压缩等级,1为最差,9为最好,6为默认。

eg:[root@localhost py]# cd /.
[root@localhost /]# cd home
[root@localhost home]# mkdir test
[root@localhost home]# mv test.txt test
mv: cannot stat `test.txt': No such file or directory
[root@localhost home]# touch test.txt
[root@localhost home]# ls
123 456 py snowy test test0 test.txt user1 user12
[root@localhost home]# mv test.txt test
[root@localhost home]# cd test
[root@localhost test]# ls
test.txt
[root@localhost test]# gzip test.txt
[root@localhost test]# ls
test.txt.gz
[root@localhost test]# 

 


用gzip压缩后,源文件消失了。

用gzip用来解压文件,

eg:[root@localhost test]# ls
test.txt.gz
[root@localhost test]# gzip -d test.txt.gz
[root@localhost test]# ls
test.txt

 



注意:gzip不支持压缩目录,压缩目录时会报错。

27.命令bzip2压缩工具, 用于创建和管理(包括解压缩)“.bz2”格式的压缩包。

bzip2(选项)(参数)

-d或——decompress:执行解压缩;

-z或——compress:强制执行压缩;

eg:[root@localhost test]# ls
test.txt
[root@localhost test]# bzip test.txt
bash: bzip: command not found
[root@localhost test]# bzip2 test.txt
[root@localhost test]# ls
test.txt.bz2
[root@localhost test]# bzip2 -d test.txt.bz2 
[root@localhost test]# ls
test.txt
[root@localhost test]# bzip2 -z test.txt 
[root@localhost test]# ls
test.txt.bz2
[root@localhost test]# ll
total 4
-rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2
[root@localhost test]# 

 


28.命令tar,可以为linux的文件和目录创建档案。利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中
改变文件,或者向档案中加入新的文件。

首先要弄清两个概念:打包和压缩。打包是指将一大堆文件或目录变成一个总的文件;压缩则是将一个大的文件
通过一些压缩算法变成一个小文件。

为什么要区分这两个概念呢?这源于Linux中很多压缩程序只能针对一个文件进行压缩,这样当你想要压缩一大堆
文件时,你得先将这一大堆文件先打成一个包(tar命令),然后再用压缩程序进行压缩(gzip bzip2命令)。


-c或--create:建立一个tar包或者压缩文件包;

-t或--list:查看压缩包中的文件;
-z或--gzip或--ungzip:同时用gzip压缩;

-f<备份文件>或--file=<备份文件>:指定备份文件;
-v或--verbose:显示指令执行过程;

-j:支持bzip2解压文件;
-v:显示操作过程;
-p或--same-permissions:用原来的文件权限还原文件;(不常用)
-P或--absolute-names:文件名使用绝对名称,不移除文件名称前的“/”号;(不常用)
--exclude=<范本样式>:排除符合范本样式的文件。(不常用)

eg:

[root@localhost test]# mkdir test11
[root@localhost test]# touch cd /test2.txt
[root@localhost test]# echo "nihao" > test11/test2.txt
[root@localhost test]# ls 
test11 test.txt.bz2
[root@localhost test]# tar -cvf test11.tar test11
test11/
test11/test2.txt
[root@localhost test]# ll
total 20
drwxr-xr-x. 2 root root 4096 May 4 06:32 test11
-rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar

[root@localhost test]# rm -rf test11
[root@localhost test]# ls 
test11.tar test.txt.bz2
[root@localhost test]# tar -xvf test.tar
tar: test.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
[root@localhost test]# tar -xvf test11.tar
test11/
test11/test2.txt

 

打包的同时使用gzip压缩 ,使用-czvf就可以同时打包

eg:[root@localhost test]# ll
total 20
drwxr-xr-x. 2 root root 4096 May 4 06:32 test11
-rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar
-rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2
[root@localhost test]# tar -czvf test11.tar.gz test11
test11/
test11/test2.txt
[root@localhost test]# ]ll
bash: ]ll: command not found
[root@localhost test]# ll
total 24
drwxr-xr-x. 2 root root 4096 May 4 06:32 test11
-rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar
-rw-r--r--. 1 root root 159 May 4 06:45 test11.tar.gz
-rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2

 

使用-zxvf解压tar.gz格式的压缩包

eg:[root@localhost test]# ll
total 20
-rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar
-rw-r--r--. 1 root root 159 May 4 06:45 test11.tar.gz
-rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2
[root@localhost test]# tar -zxvf test.tar.gz
tar (child): test.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
[root@localhost test]# tar -zxvf test11.tar.gz
test11/
test11/test2.txt
[root@localhost test]# ll
total 24
drwxr-xr-x. 2 root root 4096 May 4 06:32 test11
-rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar
-rw-r--r--. 1 root root 159 May 4 06:45 test11.tar.gz
-rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2
[root@localhost test]#

 

使用-cjvf选项来压缩

eg:[root@localhost test]# tar -cjvf test11.tar.bz2 test11
test11/
test11/test2.txt
[root@localhost test]# ll
total 28
drwxr-xr-x. 2 root root 4096 May 4 06:32 test11
-rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar
-rw-r--r--. 1 root root 155 May 4 06:59 test11.tar.bz2
-rw-r--r--. 1 root root 159 May 4 06:45 test11.tar.gz
-rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2
[root@localhost test]#

 

使用-jxvf来解压.tar.bz2格式的压缩包

eg:[root@localhost test]# tar -jxvf test.tar.bz2
\tar (child): test.tar.bz2: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
[root@localhost test]# tar -jxvf test11.tar.bz2
test11/
test11/test2.txt
[root@localhost test]#

 


29.命令w,用于显示已经登陆系统的用户列表,并显示用户正在执行的指令。执行这个命令可得知目前登入系统的用户
有那些人,以及他们正在执行的程序。单独执行w命令会显示所有的用户,您也可指定用户名称,仅显示某位用户的
相关信息。

w(选项)(参数)

eg:[root@localhost /]# w
08:11:41 up 13:13, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
py pts/0 192.168.100.11 Tue19 1.00s 3.22s 0.38s sshd: py [priv] 
[root@localhost /]# 

 



查看CPU的详细信息,用命令cat /proc/cpuinfo

eg:[root@localhost /]# cat /proc/cpuinfo
processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model    : 42
model name    : Intel(R) Core(TM) i3-2130 CPU @ 3.40GHz
stepping    : 7
microcode    : 40
cpu MHz    : 3392.340
cache size    : 3072 KB
physical id    : 0
siblings    : 2
core id    : 0
cpu cores    : 2
apicid    : 0
initial apicid    : 0
fpu    : yes
fpu_exception    : yes
cpuid level    : 13
wp    : yes
flags    : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt xsave avx hypervisor lahf_lm arat epb xsaveopt pln pts dts
bogomips    : 6784.68
clflush size    : 64
cache_alignment    : 64
address sizes    : 40 bits physical, 48 bits virtual
power management:

processor    : 1
vendor_id    : GenuineIntel
cpu family    : 6
model    : 42
model name    : Intel(R) Core(TM) i3-2130 CPU @ 3.40GHz
stepping    : 7
microcode    : 40
cpu MHz    : 3392.340
cache size    : 3072 KB
physical id    : 0
siblings    : 2
core id    : 1
cpu cores    : 2
apicid    : 1
initial apicid    : 1
fpu    : yes
fpu_exception    : yes
cpuid level    : 13
wp    : yes
flags    : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt xsave avx hypervisor lahf_lm arat epb xsaveopt pln pts dts
bogomips    : 6784.68
clflush size    : 64
cache_alignment    : 64
address sizes    : 40 bits physical, 48 bits virtual
power management:

[root@localhost /]#

 


30.命令pwd,以绝对路径的方式显示用户当前工作目录。命令将当前目录的全路径名称(从根目录)写入标准输出。
全部目录使用/分隔。第一个/表示根目录,最后一个目录是当前目录。执行pwd命令可立刻得知您目前所在的工作
目录的绝对路径名称。

pwd(选项)

--help:显示帮助信息; --version:显示版本信息。

 

eg:[py@localhost ~]$ pwd
/home/py
[py@localhost ~]$ 

 



31.命令rename,用字符串替换的方式批量改变文件名。

rename(参数)

原字符串:将文件名需要替换的字符串;
目标字符串:将文件名中含有的原字符替换成目标字符串;
文件:指定要改变文件名的文件列表。

注意:rename支持通配符
?可替代单个字符
* 可替代多个字符
[charset] 克替代charset集中的任意单个字符

eg:[root@localhost home]# touch main1.c
[root@localhost home]# ll
total 32
drwxr-xr-x. 2 root root 4096 May 2 19:13 123
drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:23 main1.c
drwx------. 28 py py 4096 May 3 21:01 py
drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy
drwxr-xr-x. 3 root root 4096 May 4 06:59 test
drwx------. 4 test0 test0 4096 May 4 00:54 test0
drwx------. 4 501 user1 4096 May 3 21:01 user1
drwx------. 4 511 511 4096 May 4 01:27 user12
[root@localhost home]# rename main1.c main.c main.c
[root@localhost home]# ll
'total 32
drwxr-xr-x. 2 root root 4096 May 2 19:13 123
drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:23 main1.c
drwx------. 28 py py 4096 May 3 21:01 py
drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy
drwxr-xr-x. 3 root root 4096 May 4 06:59 test
drwx------. 4 test0 test0 4096 May 4 00:54 test0
drwx------. 4 501 user1 4096 May 3 21:01 user1
drwx------. 4 511 511 4096 May 4 01:27 user12
[root@localhost home]# rename main1.c main.c main1.c
[root@localhost home]# ll
total 32
drwxr-xr-x. 2 root root 4096 May 2 19:13 123
drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:23 main.c
drwx------. 28 py py 4096 May 3 21:01 py
drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy
drwxr-xr-x. 3 root root 4096 May 4 06:59 test
drwx------. 4 test0 test0 4096 May 4 00:54 test0
drwx------. 4 501 user1 4096 May 3 21:01 user1
drwx------. 4 511 511 4096 May 4 01:27 user12
[root@localhost home]#



[root@localhost home]# ll
total 32
drwxr-xr-x. 2 root root 4096 May 2 19:13 123
drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:35 foo1
-rw-r--r--. 1 root root 0 May 5 07:35 foo2
-rw-r--r--. 1 root root 0 May 5 07:35 foo3
-rw-r--r--. 1 root root 0 May 5 07:35 foo4
-rw-r--r--. 1 root root 0 May 5 07:35 foo5
-rw-r--r--. 1 root root 0 May 5 07:23 main.c
drwx------. 28 py py 4096 May 3 21:01 py
drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy
drwxr-xr-x. 3 root root 4096 May 4 06:59 test
drwx------. 4 test0 test0 4096 May 4 00:54 test0
drwx------. 4 501 user1 4096 May 3 21:01 user1
drwx------. 4 511 511 4096 May 4 01:27 user12
[root@localhost home]# rename foo foo0 foo?
[root@localhost home]# ll
total 32
drwxr-xr-x. 2 root root 4096 May 2 19:13 123
drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:35 foo01
-rw-r--r--. 1 root root 0 May 5 07:35 foo02
-rw-r--r--. 1 root root 0 May 5 07:35 foo03
-rw-r--r--. 1 root root 0 May 5 07:35 foo04
-rw-r--r--. 1 root root 0 May 5 07:35 foo05
-rw-r--r--. 1 root root 0 May 5 07:23 main.c
drwx------. 28 py py 4096 May 3 21:01 py
drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy
drwxr-xr-x. 3 root root 4096 May 4 06:59 test
drwx------. 4 test0 test0 4096 May 4 00:54 test0
drwx------. 4 501 user1 4096 May 3 21:01 user1
drwx------. 4 511 511 4096 May 4 01:27 user12
[root@localhost home]#

 

32.命令tree,以树状图列出目录的内容。

tree(选项)(参数)


-a:显示所有文件和目录;

-C:在文件和目录清单加上色彩,便于区分各种类型;
-d:先是目录名称而非内容; -D:列出文件或目录的更改时间;
-f:在每个文件或目录之前,显示完整的相对路径名称;
-F:在执行文件,目录,Socket,符号连接,管道名称名称,各自加上"*","/","@","|"号;
-g:列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码;

-l:如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录;
-n:不在文件和目录清单加上色彩;
-N:直接列出文件和目录名称,包括控制字符;
-p:列出权限标示;


-s:列出文件和目录大小;
-t:用文件和目录的更改时间排序;

eg:
├── Python-2.7.11.tar.xz
├── Python-3.5.1.tar.xz
├── snowfile1
├── Templates
├── testsnow
│   ├── test
│   └── test2
│   ├── test1
│   ├── test3
│   └── test4

 

33.命令basename,用于打印目录或者文件的基本名称。basename和dirname命令通常用于shell脚本中的命令替换来指
定和指定的输入文件名称有所差异的输出文件名称。

basename(选项)(参数)

eg:显示一个shell变量的基本名称
[py@localhost ~]$ basename /etc/yum.conf
yum.conf


命令dirname,显示指定路径处了文件名之外的路径前缀

eg:[py@localhost ~]$ dirname /home/py/test.tar
/home/py

34.命令lsblk,列出块设备。除了RAM外,以标准的树状输出格式,整齐地显示块设备。

eg:[py@localhost ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk 
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 17.8G 0 part /
└─sda3 8:3 0 2G 0 part [SWAP]
sr0 11:0 1 1024M 0 rom 

 



“lsblk -l”命令以列表格式显示块设备(而不是树状格式)。

[py@localhost ~]$ lsblk -l
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk 
sda1 8:1 0 300M 0 part /boot
sda2 8:2 0 17.8G 0 part /
sda3 8:3 0 2G 0 part [SWAP]
sr0 11:0 1 1024M 0 rom 

 

 

 


注意:lsblk是最有用和最简单的方式来了解新插入的USB设备的名字,特别是当你在终端上处理磁盘/块设备时。

35.命令lsattr,显示文件隐藏属性。

lsattr [选项][文件或目录]

-R
递归地列出目录以及其下内容的属性.
-V
显示程序版本.
-a
列出目录中的所有文件,包括以`.'开头的文件的属性.
-d
以列出其它文件的方式那样列出目录的属性, 而不列出其下的内容.
-v
显示文件版本.

eg:[root@localhost /]# mkdir /tmp/attrtest
[root@localhost /]# 
[root@localhost /]# chattr +aij attrtest
chattr: No such file or directory while trying to stat attrtest
[root@localhost /]# cd /tmp
[root@localhost tmp]# chattr +aij attrtest
[root@localhost tmp]# lsattr attrtest/
[root@localhost tmp]# lsattr -a attrtest/
----ia---j---e- attrtest/.
-------------e- attrtest/..
[root@localhost tmp]# 

 


36. 命令file,确定文件类型

eg:[root@localhost tmp]# file *
attrtest: directory
keyring-2DcMS9: directory
ks-script-nGXdB2: ASCII text
ks-script-nGXdB2.log: ASCII text
orbit-gdm: directory
pulse-tuvfPBIzAlnP: directory
pulse-vWYu5SLNfS11: directory
test: directory
virtual-py.DV03kt: directory
virtual-py.H7iN1p: directory
virtual-py.TSYhZy: directory
vmware-config0: directory
VMwareDnD: sticky directory
vmware-py: directory
vmware-root: directory
vmware-root-1018539731: directory
yum.log: empty
yum_save_tx-2016-05-05-08-14vjVlfn.yumtx: ASCII text, with very long lines
[root@localhost tmp]# 

 


37.命令vmstat,监控系统各项压力参数。此命令可以打印出procs memory swap io system cpu

eg:[root@localhost tmp]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 749652 20724 114132 0 0 25 4 32 28 0 1 98 0 0    
[root@localhost tmp]# 

 



注意;关注r b wa这3列

类别 项目 含义 说明
Procs r 等待执行的任务数 展示了正在执行和等待CPU资源的任务个数。当这个值超过了CPU数目,就会出现CPU瓶颈了
b 处在非中断睡眠状态的进程数

Memory swpd 正在使用的swap大小单位K
free 空闲的内存空间
buff 已使用的buff大小,对块设备的读写进行缓冲
cache 已使用的cache大小,文件系统的cache
inact 非活跃内存大小
active 活跃的内存大小

Swap si 交换内存使用,由磁盘调入内存
so 交换内存使用,由内存调入磁盘

IO bi 从块设备读入的数据总量(读磁盘) (KB/s),
bo 写入到块设备的数据总理(写磁盘) (KB/s)

System in 每秒产生的中断次数
cs 每秒产生的上下文切换次数 上面这2个值越大,会看到由内核消耗的CPU时间会越多

CPU us 用户进程消耗的CPU时间百分比 us 的值比较高时,说明用户进程消耗的CPU时间多,但是如果长期超过50% 的使用,那么我们就该考虑优化程序算法或者进行加速了
sy 内核进程消耗的CPU时间百分比 sy 的值高时,说明系统内核消耗的CPU资源多,这并不是良性的表现,我们应该检查原因。
id 空闲
wa IO等待消耗的CPU时间百分比 wa 的值高时,说明IO等待比较严重,这可能是由于磁盘大量作随机访问造成,也有可能是磁盘的带宽出现瓶颈(块操作)。


38.命令top,可以显示当前系统正在执行的进程的相关信息,包括进程ID、内存占用率、CPU占用率等。可以理解为
动态监控进程所占的系统资源。3秒更新一次

[root@localhost tmp]# top
top - 08:26:51 up 1:01, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 114 total, 1 running, 113 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.2%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1012456k total, 263348k used, 749108k free, 20924k buffers
Swap: 2031612k total, 0k used, 2031612k free, 114420k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
12 root 20 0 0 0 0 S 0.3 0.0 0:05.83 events/1 
1679 root 20 0 174m 4448 3608 S 0.3 0.4 0:15.71 vmtoolsd 
2706 py 20 0 99972 1820 856 S 0.3 0.2 0:00.77 sshd 
1 root 20 0 19356 1600 1272 S 0.0 0.2 0:04.45 init 
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd 
3 root RT 0 0 0 0 S 0.0 0.0 0:00.12 migration/0 
4 root 20 0 0 0 0 S 0.0 0.0 0:00.56 ksoftirqd/0 
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0 
6 root RT 0 0 0 0 S 0.0 0.0 0:00.06 watchdog/0 
7 root RT 0 0 0 0 S 0.0 0.0 0:00.79 migration/1 
8 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/1 
9 root 20 0 0 0 0 S 0.0 0.0 0:00.36 ksoftirqd/1 

 



注意:top -bn1表示非动态打印系统资源的使用情况。

39.命令sar,sar是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对
系统的活动进行报告,包括:文件的读写情况、系统调用的使用情况、磁盘I/O、CPU效率、
内存使用状况、进程活动及IPC有关的活动等。

sar命令常用格式

sar [options] [-A] [-o file] t [n]

t为采样间隔,n为采样次数,默认值是1;

查看网卡流量,sar -n DEV

eg:    [root@localhost tmp]# sar -n DEV
Linux 2.6.32-573.el6.x86_64 (localhost.localdomain) 05/06/2016 _x86_64_(2 CPU)

07:25:15 AM LINUX RESTART

07:30:01 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
07:40:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:40:01 AM eth0 0.70 0.36 0.06 0.04 0.00 0.00 0.00
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: eth0 0.70 0.36 0.06 0.04 0.00 0.00 0.00

07:40:55 AM LINUX RESTART

07:50:02 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
08:00:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:00:01 AM eth0 0.43 0.21 0.04 0.03 0.00 0.00 0.00
08:10:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:10:01 AM eth0 0.11 0.03 0.01 0.00 0.00 0.00 0.00
08:20:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:20:01 AM eth0 0.15 0.06 0.01 0.00 0.00 0.00 0.00
08:30:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:30:01 AM eth0 0.22 0.15 0.02 0.09 0.00 0.00 0.00
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: eth0 0.23 0.11 0.02 0.03 0.00 0.00 0.00
[root@localhost tmp]#

 

查看历史负载,sar -q

[root@localhost tmp]# sar -q
Linux 2.6.32-573.el6.x86_64 (localhost.localdomain) 05/06/2016 _x86_64_(2 CPU)

07:25:15 AM LINUX RESTART

07:30:01 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
07:40:01 AM 0 213 0.00 0.02 0.05
Average: 0 213 0.00 0.02 0.05

07:40:55 AM LINUX RESTART

07:50:02 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
08:00:01 AM 0 188 0.00 0.00 0.00
08:10:01 AM 0 189 0.00 0.00 0.00
08:20:01 AM 0 189 0.00 0.00 0.00
08:30:01 AM 0 189 0.00 0.00 0.00
Average: 0 189 0.00 0.00 0.00
[root@localhost tmp]#

 

40. 命令ps,专门显示系统进程的命令。此命令比top更加方便。

经常用到的,ps aux ps -elf

eg:[root@localhost tmp]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 19356 1600 ? Ss 07:24 0:04 /sbin/init
root 2 0.0 0.0 0 0 ? S 07:24 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:24 0:00 [migration/0]
root 4 0.0 0.0 0 0 ? S 07:24 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S 07:24 0:00 [stopper/0]
root 6 0.0 0.0 0 0 ? S 07:24 0:00 [watchdog/0]
root 7 0.0 0.0 0 0 ? S 07:24 0:00 [migration/1]
root 8 0.0 0.0 0 0 ? S 07:24 0:00 [stopper/1]
root 9 0.0 0.0 0 0 ? S 07:24 0:00 [ksoftirqd/1]
root 10 0.0 0.0 0 0 ? S 07:24 0:00 [watchdog/1]
root 11 0.0 0.0 0 0 ? S 07:24 0:01 [events/0]
root 12 0.1 0.0 0 0 ? S 07:24 0:07 [events/1]
root 13 0.0 0.0 0 0 ? S 07:24 0:00 [events/0]
root 14 0.0 0.0 0 0 ? S 07:24 0:00 [events/1]
root 15 0.0 0.0 0 0 ? S 07:24 0:00 [events_long/0]
root 16 0.0 0.0 0 0 ? S 07:24 0:00 [events_long/1]
root 17 0.0 0.0 0 0 ? S 07:24 0:00 [events_power_e]
root 18 0.0 0.0 0 0 ? S 07:24 0:00 [events_power_e]
root 19 0.0 0.0 0 0 ? S 07:24 0:00 [cgroup]
root 20 0.0 0.0 0 0 ? S 07:24 0:00 [khelper]
root 21 0.0 0.0 0 0 ? S 07:24 0:00 [netns]

 

命令ps -elf

[root@localhost tmp]# ps -elf
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 4839 poll_s 07:24 ? 00:00:04 /sbin/
1 S root 2 0 0 80 0 - 0 kthrea 07:24 ? 00:00:00 [kthr]
1 S root 3 2 0 -40 - - 0 migrat 07:24 ? 00:00:00 [migr]
1 S root 4 2 0 80 0 - 0 ksofti 07:24 ? 00:00:00 [ksof]
1 S root 5 2 0 -40 - - 0 cpu_st 07:24 ? 00:00:00 [stop]
5 S root 6 2 0 -40 - - 0 watchd 07:24 ? 00:00:00 [watc]
1 S root 7 2 0 -40 - - 0 migrat 07:24 ? 00:00:00 [migr]
1 S root 8 2 0 -40 - - 0 cpu_st 07:24 ? 00:00:00 [stop]
1 S root 9 2 0 80 0 - 0 ksofti 07:24 ? 00:00:00 [ksof]
5 S root 10 2 0 -40 - - 0 watchd 07:24 ? 00:00:00 [watc]
1 S root 11 2 0 80 0 - 0 worker 07:24 ? 00:00:01 [even]
1 S root 12 2 0 80 0 - 0 worker 07:24 ? 00:00:07 [even]
1 S root 13 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even]
1 S root 14 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even]
1 S root 15 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even]
1 S root 16 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even]
1 S root 17 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even]
1 S root 18 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even]
1 S root 19 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [cgro]
1 S root 20 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [khel]
1 S root 21 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [netn]
1 S root 22 2 0 80 0 - 0 async_ 07:24 ? 00:00:00 [asyn]
1 S root 23 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [pm]
1 S root 24 2 0 80 0 - 0 bdi_sy 07:24 ? 00:00:00 [sync]

 



PID表示进程的id,这个id很有用,比如我们要终止一个进程,则用命令 kill 进程的id.有时候
这样还不能终止进程,那么可以使用kill -9 进程的id。

STAT表示进程的状态。


41.命令netstat,查看网络连接状况。显示网络连接,路由表,接口状态,伪装连接,网络链路信息和组播成员组。

netstat -lnp 打印系统启动哪些端口

eg:[root@localhost tmp]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2269/sshd 
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2105/cupsd 
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2397/master 
tcp 0 0 :::22 :::* LISTEN 2269/sshd 
tcp 0 0 ::1:631 :::* LISTEN 2105/cupsd 
tcp 0 0 ::1:25 :::* LISTEN 2397/master 
udp 0 0 0.0.0.0:68 0.0.0.0:* 2108/dhclient 
udp 0 0 0.0.0.0:631 0.0.0.0:* 2105/cupsd 
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 13803 2156/hald @/var/run/hald/dbus-t9u3xeiG2L
unix 2 [ ACC ] STREAM LISTENING 

 


netstat -an打印网络连接状况

eg:[root@localhost tmp]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State 
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 
tcp 0 64 192.168.100.44:22 192.168.100.5:6080 ESTABLISHED 
tcp 0 0 :::22 :::* LISTEN 
tcp 0 0 ::1:631 :::* LISTEN 
tcp 0 0 ::1:25 :::* LISTEN 
udp 0 0 0.0.0.0:68 0.0.0.0:* 
udp 0 0 0.0.0.0:631 0.0.0.0:* 
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 13803 @/var/run/hald/dbus-t9u3xeiG2L
unix 2 [ ACC ] STREAM LISTENING 9292 @/com/ubuntu/upstart
unix 2 [ ACC ] STREAM LISTENING 13810 @/var/run/hald/dbus-MKoSzKTZ4n
unix 2 [ ] DGRAM 13835 @/org/freedesktop/hal/udev_event
unix 2 [ ] DGRAM 9798 @/org/kernel/udev/udevd
unix 2 [ ACC ] STREAM LISTENING 13448 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 13679 /var/run/cups/cups.sock
unix 2 [ ACC ] STREAM LISTENING 14587 public/cleanup
unix 2 [ ACC ] STREAM LISTENING 14595 private/tlsmgr
unix 2 [ ACC ] STREAM LISTENING 14599 private/rewrite
unix 2 [ ACC ] STREAM LISTENING 14603 private/bounce
unix 2 [ ACC ] STREAM LISTENING 14607 private/defer
unix 2 [ ACC ] STREAM LISTENING 14611 private/trace
unix 2 [ ACC ] STREAM LISTENING 14615 private/verify
unix 2 [ ACC ] STREAM LISTENING 14619 public/flush
unix 2 [ ACC ] STREAM LISTENING 14623 private/proxymap
unix 2 [ ACC ] STREAM LISTENING 14627 private/proxywrite
unix 2 [ ACC ] STREAM LISTENING 14631 private/smtp
unix 2 [ ACC ] STREAM LISTENING 14635 private/relay

 

42.命令cut,用来显示行中的指定部分,删除文件中指定字段。cut经常用来显示文件的内容

cut(选项)(参数)
-b:仅显示行中指定直接范围的内容;
-c:仅显示行中指定范围的字符;
-d:指定字段的分隔符,默认的字段分隔符为“TAB”;
-f:显示指定字段的内容

 

cut命令有2个功能:
1.用来显示文件的内容,它依次读取由参数file所指 明的文件,将它们的内容输出到标准输出上


2.是连接两个或多个文件,如cut fl f2 > f3将把文件fl和几的内容合并起来,然后通过输出重定
向符“>”的作用,将它们放入文件f3中。





43.命令split,可以将一个大文件分割成很多个小文件,有时需要将文件分割成更小的片段,比如为提高可读性,
生成日志等。

-b:值为每一输出档案的大小,单位为 byte。
-C:每一输出档中,单行的最大 byte 数。
-d:使用数字作为后缀。
-l:值为每一输出档的列数大小。

eg:生成一个大小为100KB的测试文件。
[root@localhost test]# dd if=/dev/zero bs=100k count=1 of=date.file
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.000919489 s, 111 MB/s
[root@localhost test]# 

 



使用split将上面创建的date.file文件分割大小为10kb的小文件

[root@localhost test]# split -b 10k date.file
[root@localhost test]# lls
bash: lls: command not found
[root@localhost test]# ls
date.file test11.tar test11.tar.gz xaa xac xae xag xai
test11 test11.tar.bz2 test.txt.bz2 xab xad xaf xah xaj
[root@localhost test]#

 

44.命令sort,它将文件进行排序,并将排序结果标准输出。sort命令既可以从特定的文件,也可以从stdin中
获取输入。

sort(选项)(参数)

-b:忽略每行前面开始出的空格字符;
-c:检查文件是否已经按照顺序排序;
-d:排序时,处理英文字母、数字及空格字符外,忽略其他的字符;
-f:排序时,将小写字母视为大写字母;
-n:依照数值的大小排序;
-r:以相反的顺序来排序;

 

eg:[root@localhost sort]# cat /etc/passwd | sort
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt

 

45.命令diff,在最简单的情况下,比较给定的两个文件的不同。如果使用“-”代替“文件”参数,则要
比较的内容将来自标准输入。diff命令是以逐行的方式,比较文本文件的异同处。如果该命令指定进行
目录的比较,则将会比较该目录中具有相同文件名的文件,而不会对其子目录文件进行任何比较操作。

diff(选项)(参数)

-<行数>:指定要显示多少行的文本。此参数必须与-c或-u参数一并使用;
-a或——text:diff预设只会逐行比较文本文件;
-b或--ignore-space-change:不检查空格字符的不同;
-B或--ignore-blank-lines:不检查空白行;
-c:显示全部内容,并标出不同之处;

eg:[root@localhost home]# diff test/ test0/
Only in test0/: .bash_logout
Only in test0/: .bash_profile
Only in test0/: .bashrc
Only in test/: date.file
Only in test0/: .gnome2
Only in test0/: .mozilla
Only in test/: test11
Only in test/: test11.tar
Only in test/: test11.tar.bz2
Only in test/: test11.tar.gz
Only in test/: test.txt.bz2
Only in test/: xaa
Only in test/: xab
Only in test/: xac
Only in test/: xad
Only in test/: xae
Only in test/: xaf
Only in test/: xag
Only in test/: xah
Only in test/: xai
Only in test/: xaj

 

46.命令rev,将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,
依次类推。

rev(参数)

eg:[root@localhost home]# ls 
123 foo01 foo03 foo05 py sort test0 user12
456 foo02 foo04 main.c snowy test user1
[root@localhost home]# cat main.c
printf ("Hello World!\n")
[root@localhost home]# rev main.c
)"n\!dlroW olleH"( ftnirp
[root@localhost home]#

 

47.命令ln,用来为文件创件连接,连接类型分为硬连接和符号连接两种,默认的连接类型是硬连接。如果要
创建符号连接必须使用"-s"选项。

ln(选项)(参数)

链接文件分为:1.硬链接 2.软链接

硬链接,直接建立一个inode链接到文件放置的块区域,就是进行硬链接的时候,该文件内容没有任何变化,
只是增加了一个指向这个文件的inode。硬链接的限制有2个,1.不能跨文件系统,因为不同的文件系统有
不同的inode table 2.不能链接目录


软链接,是建立一个独立的文件,当读取这个链接文件时,它会把读取的行为转发到该文件所链接的文件上。

命令ln ,-s该选项就是建立软连接,如果不加-s就是硬链接。

源文件:指定连接的源文件。如果使用-s选项创建符号连接,则“源文件”可以是文件或者目录。创建硬连接时,则“源文件”参数只能是文件; 目标文件:指定源文件的目标连接文件。

eg:[root@localhost home]# mkdir ceshi
[root@localhost home]# ls 
123 ceshi foo02 foo04 main.c snowy test0 user12
456 foo01 foo03 foo05 py sort user1
[root@localhost home]# cd ceshi
[root@localhost ceshi]# cp /etc/passwd ./
[root@localhost ceshi]# ls
passwd
[root@localhost ceshi]# ll
total 4
-rw-r--r--. 1 root root 1509 May 6 21:11 passwd
[root@localhost ceshi]# ln passwd passwd-hard
[root@localhost ceshi]# ll
total 8
-rw-r--r--. 2 root root 1509 May 6 21:11 passwd
-rw-r--r--. 2 root root 1509 May 6 21:11 passwd-hard
[root@localhost ceshi]# du -sk
8    .
[root@localhost ceshi]# 


软连接测试

[root@localhost home]# cd ceshi2
[root@localhost ceshi2]# cd /etc/passwd ./
bash: cd: /etc/passwd: Not a directory
[root@localhost ceshi2]# cp /etc/passwd ./
[root@localhost ceshi2]# ln -s passwd passed-soft
[root@localhost ceshi2]# ll
total 4
lrwxrwxrwx. 1 root root 6 May 6 21:15 passed-soft -> passwd
-rw-r--r--. 1 root root 1509 May 6 21:15 passwd
[root@localhost ceshi2]# head n1 passed
head: cannot open `n1' for reading: No such file or directory
head: cannot open `passed' for reading: No such file or directory
[root@localhost ceshi2]# head n1 passwd
head: cannot open `n1' for reading: No such file or directory
==> passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@localhost ceshi2]# rm -f passwd
[root@localhost ceshi2]# head -n1 passwd-soft
head: cannot open `passwd-soft' for reading: No such file or directory
[root@localhost ceshi2]# ll
total 0
lrwxrwxrwx. 1 root root 6 May 6 21:15 passed-soft -> passwd
[root@localhost ceshi2]#

 

48. 命令ifconfig,用于配置和显示Linux内核中网络接口的网络参数。用ifconfig命令配置的网卡信息,在网卡重启
后机器重启后,配置就不存在。

ifconfig(参数)

add<地址>:设置网络设备IPv6的ip地址;
del<地址>:删除网络设备IPv6的IP地址;
down:关闭指定的网络设备;
<硬件地址>:设置网络设备的类型与硬件地址;
media<网络媒介类型>:设置网络设备的媒介类型;
mtu<字节>:设置网络设备的MTU;
netmask<子网掩码>:设置网络设备的子网掩码;
up:启动指定的网络设备;
IP地址:指定网络设备的IP地址;
网络设备:指定网络设备的名称。

eg:[root@localhost ceshi2]# clear
[root@localhost ceshi2]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:D0:8B:8C 
inet addr:192.168.100.44 Bcast:192.168.100.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fed0:8b8c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2352 errors:0 dropped:0 overruns:0 frame:0
TX packets:1270 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000 
RX bytes:214617 (209.5 KiB) TX bytes:265181 (258.9 KiB)

lo Link encap:Local Loopback 
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0 
RX bytes:240 (240.0 b) TX bytes:240 (240.0 b)

 


说明: eth0表示第一块网卡,其中HWaddr表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址)是00:0C:29:D0:8B:8C 。
inet addr用来表示网卡的IP地址,此网卡的IP地址是192.168.100.44,广播地址Bcast:192.168.100.255,掩码地址Mask:255.255.255.0。
lo是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。
比如把 httpd服务器的指定到回坏地址,在浏览器输入127.0.0.1就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道。
第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址)。
第二行:网卡的IP地址、子网、掩码。
第三行:UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节。 第四、五行:接收、发送数据包情况统计。
第七行:接收、发送数据字节数统计信息。


49.命令wc,用于统计文档的行数,字符数或词数。该命令常用的选项有-l(统计行数),-m(统计字符数),-w(统计词数)

[root@localhost ceshi2]# wc /etc/passwd
33 47 1509 /etc/passwd
[root@localhost ceshi2]# wc -l /etc/passwd
33 /etc/passwd
[root@localhost ceshi2]# wc -m /etc/passwd
1509 /etc/passwd
[root@localhost ceshi2]# wc -w /etc/passwd
47 /etc/passwd
[root@localhost ceshi2]# 

 

若wc后面不跟任何参数,直接跟文档,会把行数,词数和字符数依次输出。

50.命令tr,可以对来自标准输入的字符进行替换、压缩和删除。它可以将一组字符变成另一组字符,
经常用来编写优美的单行命令,作用很强大。

tr(选项)(参数)

-c或——complerment:取代所有不属于第一字符集的字符;
-d或——delete:删除所有属于第一字符集的字符;
-s或--squeeze-repeats:把连续重复的字符以单独一个字符表示;
-t或--truncate-set1:先删除第一字符集较第二字符集多出的字符。

eg:[root@localhost /]# head -n2 /etc/passwd | tr '[a-z]''[A-Z]'
tr: missing operand after `[a-z][A-Z]'
Two strings must be given when translating.
Try `tr --help' for more information.
[root@localhost /]# echo "HELLO WORLD" | tr 'A-Z''a-z'
tr: missing operand after `A-Za-z'
Two strings must be given when translating.
Try `tr --help' for more information.
[root@localhost /]# echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world

 

tr还可以替换一个字符,代码如下

[root@localhost /]# grep 'root' /etc/passwd | tr 'r' 'R'
Root:x:0:0:Root:/Root:/bin/bash
opeRatoR:x:11:0:opeRatoR:/Root:/sbin/nologin
[root@localhost /]# 

 


51.命令uniq,用来删除重复的行,该命令只有-c选项比较常用给,它表示统计重复的行数,并把数字写在前面

uniq(选项)(参数)

eg:[root@localhost home]# mkdir test
[root@localhost home]# ls 
123 ceshi foo01 foo03 foo05 py sort test0 user12
456 ceshi2 foo02 foo04 main.c snowy test user1
[root@localhost home]# cd test
[root@localhost test]# ls 
[root@localhost test]# vi test.txt
[root@localhost test]# cat test.txt 
111
222
111
333

[root@localhost test]# uniq test.txt
111
222
111
333

[root@localhost test]# sort test.txt | uniq

111
222
333
[root@localhost test]# sort test.txt | uniq -c
1 
2 111
1 222
1 333
[root@localhost test]#

 

liunx的常用命令,暂时就总结到这里,可能有错误,请打击给予指正。后续学到的知识点,会及时更新到此篇文章中。

posted @ 2016-05-07 15:00  Snow_man  阅读(4961)  评论(0编辑  收藏  举报