【Linux】linux中很有用的指令(练习)

以下指令的总结(大多数)来自于:

http://www.oschina.net/translate/useful-linux-commands-for-newbies

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

http://www.oschina.net/translate/20-advanced-commands-for-linux-experts

练习平台环境:Ubuntu 14.04.2 LTS

1、ls

tyruschin@iZ28xhlm5vhZ:~$ ls
hello  hello2
tyruschin@iZ28xhlm5vhZ:~$ ls -a
.  ..  .bash_history  .bash_logout  .bashrc  hello  hello2  .profile  .viminfo
tyruschin@iZ28xhlm5vhZ:~$ ls -l
total 0
-rw-rw-r-- 1 tyruschin tyruschin 0 Jul 15 08:19 hello
-rw-rw-r-- 1 tyruschin tyruschin 0 Jul 15 08:20 hello2

tyruschin@iZ28xhlm5vhZ:~$ ls -al dir
total 8
drwxrwxr-x 2 tyruschin tyruschin 4096 Jul 15 08:25 .
drwxr-xr-x 3 tyruschin tyruschin 4096 Jul 15 08:25 ..
tyruschin@iZ28xhlm5vhZ:~$ ls -ald dir
drwxrwxr-x 2 tyruschin tyruschin 4096 Jul 15 08:25 dir

用于列出目录和文件的信息

选项:a表示列出所有文件,包含隐藏文件;l表示列出详细的信息;d表示查看目录文件的信息

2、lsblk

tyruschin@iZ28xhlm5vhZ:~$ lsblk 
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  20G  0 disk 
└─xvda1 202:1    0  20G  0 part /
xvdb    202:16   0  20G  0 disk 
└─xvdb1 202:17   0  20G  0 part 
tyruschin@iZ28xhlm5vhZ:~$ lsblk -l
NAME  MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda  202:0    0  20G  0 disk 
xvda1 202:1    0  20G  0 part /
xvdb  202:16   0  20G  0 disk 
xvdb1 202:17   0  20G  0 part 

 用于查看块设备信息,可以最快速和简单地查看新插入的设备信息

3、md5sum

tyruschin@iZ28xhlm5vhZ:~$ md5sum hello2
d41d8cd98f00b204e9800998ecf8427e  hello2
tyruschin@iZ28xhlm5vhZ:~$ md5sum hello
d41d8cd98f00b204e9800998ecf8427e  hello

tyruschin@iZ28xhlm5vhZ:~$ echo 123 >> hello2
tyruschin@iZ28xhlm5vhZ:~$ md5sum hello2
ba1f2511fc30423bdbb183fe33f3dd0f  hello2

 计算和检验文件的MD5信息签名,以上例子中两个文件一开始内容相同,后来hello2文件改变之后,md5信息就不同了

4、dd

用于数据的备份和恢复,可以将iso文件复制到u盘中,所以可以用来制作启动盘

5、uname

tyruschin@iZ28xhlm5vhZ:~$ uname -a
Linux iZ28xhlm5vhZ 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

用于打印机器、操作系统和内核信息

6、history

tyruschin@iZ28xhlm5vhZ:~$ history
    1  sudo vi a
    2  ll
    3  touch hello

查看历史记录,在终端输入ctrl+r可以根据history来查看输入的命令

7、sudo

获得超级用户权限来执行某一些特权指令

8、mkdir

tyruschin@iZ28xhlm5vhZ:~$ mkdir dir
tyruschin@iZ28xhlm5vhZ:~$ mkdir dir
mkdir: cannot create directory ‘dir’: File exists
tyruschin@iZ28xhlm5vhZ:~$ chmod 444 dir
tyruschin@iZ28xhlm5vhZ:~$ mkdir dir/dir
mkdir: cannot create directory ‘dir/dir’: Permission denied

创建目录,当文件(不一定是目录)已存在 或 没有写权限时,创建会失败

9、touch

tyruschin@iZ28xhlm5vhZ:~$ touch touchtest
tyruschin@iZ28xhlm5vhZ:~$ ll touchtest 
-rw-rw-r-- 1 tyruschin tyruschin 0 Jul 15 08:57 touchtest
tyruschin@iZ28xhlm5vhZ:~$ touch touchtest
tyruschin@iZ28xhlm5vhZ:~$ ll touchtest 
-rw-rw-r-- 1 tyruschin tyruschin 0 Jul 15 08:58 touchtest

当文件不存在的时候,创建一个空文件,当文件存在时,修改最后访问时间和最后修改时间至当前

10、chmod

tyruschin@iZ28xhlm5vhZ:~$ ll touchtest 
-rw-rw-r-- 1 tyruschin tyruschin 0 Jul 15 08:58 touchtest
tyruschin@iZ28xhlm5vhZ:~$ chmod g+x touchtest 
tyruschin@iZ28xhlm5vhZ:~$ ll touchtest 
-rw-rwxr-- 1 tyruschin tyruschin 0 Jul 15 08:58 touchtest*
tyruschin@iZ28xhlm5vhZ:~$ chmod 711 touchtest
tyruschin@iZ28xhlm5vhZ:~$ ll touchtest
-rwx--x--x 1 tyruschin tyruschin 0 Jul 15 08:58 touchtest*

改变文件权限(user, group, other),可以用数字或者+-来表示权限变化

r(read):4 w(write):2 x(execute):1

ugoa +- rwxa

超级用户和具有w权限的文件拥有者,才可以修改权限

递归修改使用选项R

11、chown

tyruschin@iZ28xhlm5vhZ:~$ ll touchtest
-rwx--x--x 1 tyruschin tyruschin 0 Jul 15 08:58 touchtest*

tyruschin@iZ28xhlm5vhZ:~$ sudo chown root:root touchtest
tyruschin@iZ28xhlm5vhZ:~$ ll touchtest 
-rwx--x--x 1 root root 0 Jul 15 08:58 touchtest*

修改文件所有者和所属组的信息,用:分隔即可

递归修改使用选项R

12、apt

一个debian系列的包管理器,用于智能安装软件

apt-get , dpkg

http://baike.baidu.com/link?url=7oDsdLuTunultr2-tUhAPCtYMCE6kBaT4qVmCDtoSmg6tvP81a7DB8agyYiOA2u27JuhK47BQKjcmyf7R0o-La

13、tar

tyruschin@iZ28xhlm5vhZ:~$ tar -zcvf aaa1.tar.gz dir/
dir/
tyruschin@iZ28xhlm5vhZ:~$ tar -jcvf aaa2.tar.bz2 dir/
dir/
tyruschin@iZ28xhlm5vhZ:~$ tar -zxvf aaa1.tar.gz 
dir/
tyruschin@iZ28xhlm5vhZ:~$ tar -jxvf aaa2.tar.bz2 
dir/

解压和压缩文件,z代表tar.gz格式,j代表tar.bz2格式,c代表压缩,x代表解压缩。v是看到压缩或解压过程,f选项必须放在最后,后接文件名

14、cal

tyruschin@iZ28xhlm5vhZ:~$ cal
     July 2016        
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                    
tyruschin@iZ28xhlm5vhZ:~$ cal 12 2010
   December 2010      
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 

显示日历

15、date

tyruschin@iZ28xhlm5vhZ:~$ date
Fri Jul 15 09:31:54 CST 2016
tyruschin@iZ28xhlm5vhZ:~$ date +%Y"-"%m"-"%d" "%H":"%M":"%S
2016-07-15 09:31:56

显示时间相关

16、cat

tyruschin@iZ28xhlm5vhZ:~$ cat hello2
123
tyruschin@iZ28xhlm5vhZ:~$ cat hello
123
4321
tyruschin@iZ28xhlm5vhZ:~$ cat hello >> hello2
tyruschin@iZ28xhlm5vhZ:~$ cat hello2
123
123
4321
tyruschin@iZ28xhlm5vhZ:~$ cat hello > hello2
tyruschin@iZ28xhlm5vhZ:~$ cat hello2
123
4321
tyruschin@iZ28xhlm5vhZ:~$ cat hello >> hello
cat: hello: input file is output file

将文件内容输出(默认输出到标准输出流,可以使用> 或 >>来改变)

>表示目标文件清空后重新输入,>>表示追加

输入和输出文件不能相同

17、cp

tyruschin@iZ28xhlm5vhZ:~$ ll hello*
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:33 hello
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:34 hello2
tyruschin@iZ28xhlm5vhZ:~$ cp hello hello3
tyruschin@iZ28xhlm5vhZ:~$ ll hello*
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:33 hello
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:34 hello2
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:40 hello3

复制一个可读的文件到新的路径或文件名中,默认是自动覆盖(如果目标文件已存在的话)。如果需要提示,需要加入选项i,如 cp -i src desc

复制文件夹使用选项r

18、mv

tyruschin@iZ28xhlm5vhZ:~$ ll hello*
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:33 hello
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:34 hello2
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:42 hello3
tyruschin@iZ28xhlm5vhZ:~$ mv hello3 hello4
tyruschin@iZ28xhlm5vhZ:~$ ll hello*
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:33 hello
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:34 hello2
-rw-rw-r-- 1 tyruschin tyruschin 9 Jul 15 09:42 hello4

移动文件或目录到另一处(也可以视为是重命名)

19、pwd

tyruschin@iZ28xhlm5vhZ:~$ pwd
/home/tyruschin

获得当前路径

20、cd

tyruschin@iZ28xhlm5vhZ:~$ cd dir
tyruschin@iZ28xhlm5vhZ:~/dir$ pwd
/home/tyruschin/dir

切换目录

21、find

tyruschin@iZ28xhlm5vhZ:~/dir$ find ~ -name *.gz
/home/tyruschin/aaa1.tar.gz

在磁盘上查找文件

22、grep

tyruschin@iZ28xhlm5vhZ:~$ cat hello2
123
4321
213
tyruschin@iZ28xhlm5vhZ:~$ grep "21" hello2
4321
213

在文件或输入中查找匹配规则的行并输出

选项:i表示忽略大小写的匹配,r表示递归整个目录下去查找匹配

23、man

查看命令的帮助文档,man command

24、ps

tyruschin@iZ28xhlm5vhZ:~$ ps
  PID TTY          TIME CMD
11013 pts/0    00:00:00 bash
11100 pts/0    00:00:00 ps

tyruschin@iZ28xhlm5vhZ:~$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Apr17 ?        00:00:04 /sbin/init
...

tyruschin@iZ28xhlm5vhZ:~$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.2  33380  2316 ?        Ss   Apr17   0:04 /sbin/init
...

ps用于报告当前系统进程的状态,可以配合grep管道来筛选进程输出,配合kill来终止进程的执行

常用ps -ef 和 ps aux,都可以查看到所有TTY的进程情况

http://man.linuxde.net/ps

25、kill/pkill

kill通过进程号来终止进程,pkill通过命令来终止程序执行

26、whereis

tyruschin@iZ28xhlm5vhZ:~$ whereis kill
kill: /bin/kill /usr/share/man/man1/kill.1.gz /usr/share/man/man2/kill.2.gz

二进制文件保存位置

27、service

tyruschin@iZ28xhlm5vhZ:~$ service apache2 reload
 * Reloading web server apache2                                                                      * 

tyruschin@iZ28xhlm5vhZ:~$ /etc/init.d/apache2 reload
 * Reloading web server apache2                                                                      * 

处理放在/etc/init.d下的服务

28、alias/unalias

tyruschin@iZ28xhlm5vhZ:~$ alias lll="ls -a"
tyruschin@iZ28xhlm5vhZ:~$ lll
.   aaa1.tar.gz   .bash_history  .bashrc  hello   hello4    ps.doc     .viminfo
..  aaa2.tar.bz2  .bash_logout   dir      hello2  .profile  touchtest
tyruschin@iZ28xhlm5vhZ:~$ ls -a
.   aaa1.tar.gz   .bash_history  .bashrc  hello   hello4    ps.doc     .viminfo
..  aaa2.tar.bz2  .bash_logout   dir      hello2  .profile  touchtest
tyruschin@iZ28xhlm5vhZ:~$ unalias lll
tyruschin@iZ28xhlm5vhZ:~$ lll
lll: command not found

取别名,但只适用于此终端,通常把这个命令放在~/.bashrc中,打开bash自动载入

29、df

tyruschin@iZ28xhlm5vhZ:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/xvda1      20509308 4053196  15391256  21% /
none                   4       0         4   0% /sys/fs/cgroup
udev              497280       4    497276   1% /dev
tmpfs             101628     396    101232   1% /run
none                5120       0      5120   0% /run/lock
none              508136       0    508136   0% /run/shm
none              102400       0    102400   0% /run/user

显示磁盘使用情况,仅当文件关闭之后数值才更新

选项h可以让数值用更容易阅读的方式显示,如20G, 100M等,理解略,同理可以参考du -h

30、du

tyruschin@iZ28xhlm5vhZ:~$ du -s ~
100	/home/tyruschin
tyruschin@iZ28xhlm5vhZ:~$ du -h ~
4.0K	/home/tyruschin/dir
100K	/home/tyruschin

查看文件和目录的使用情况

选项:默认是显示目录及目录以下的子目录的大小,s表示只查看该目录的总大小,h表示用更容易阅读的方式显示

31、rm

tyruschin@iZ28xhlm5vhZ:~$ rm -rf dir
tyruschin@iZ28xhlm5vhZ:~$ rm hello4

删除文件或目录(使用选项r,递归删除)

选项:f强制删除,不提示

32、echo

tyruschin@iZ28xhlm5vhZ:~$ echo -e "e[1;31m这是红色的字!e[0m"
这是红色的字!
tyruschin@iZ28xhlm5vhZ:~$ echo "e[1;31m这不是红色的字!e[0m"
e[1;31m这不是红色的字!e[0m

常用于shell编程中输出给用户查看信息

选项:e表示支持识别转义字符(默认不支持)

33、passwd

设置用户的密码

34、ln

tyruschin@iZ28xhlm5vhZ:~$ ln -s hello dir/
tyruschin@iZ28xhlm5vhZ:~$ ln hello2 dir
tyruschin@iZ28xhlm5vhZ:~$ ll dir/
total 8
drwxrwxr-x 2 tyruschin tyruschin 4096 Jul 15 14:40 ./
drwxr-xr-x 3 tyruschin tyruschin 4096 Jul 15 14:40 ../
lrwxrwxrwx 1 tyruschin tyruschin    5 Jul 15 14:40 hello -> hello
-rw-rw-r-- 2 tyruschin tyruschin   13 Jul 15 11:59 hello2

添加链接(默认是硬链接,会增加inode连接数),选项s则添加软链接,相当于快捷方式

35、wget

wget 链接 用于下载文件,支持http,https,ftp,http代理

36、mount

使用lsblk查看到设备名之后,需要把它挂载到具体目录(已存在的)下用于访问

mount /dev/sdb1 /mnt/diskname

37、ifconfig

root@iZ28xhlm5vhZ:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:16:3e:00:1c:2f  
          inet addr:10.251.155.55  Bcast:10.251.159.255  Mask:255.255.248.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:180650 errors:0 dropped:0 overruns:0 frame:0
          TX packets:288695 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:47024057 (47.0 MB)  TX bytes:21696412 (21.6 MB)

eth1      Link encap:Ethernet  HWaddr 00:16:3e:00:1f:67  
          inet addr:xx.xx.xx.xx  Bcast:xx.xx.xx.255  Mask:255.255.252.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4442364 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3191965 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2684719339 (2.6 GB)  TX bytes:1144128598 (1.1 GB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:49414 errors:0 dropped:0 overruns:0 frame:0
          TX packets:49414 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:9694649 (9.6 MB)  TX bytes:9694649 (9.6 MB) 

查看网络接口信息(常用于查询或修改ip和接口等相关信息)

例子:ifconfig eth0 192.168.0.1 netmask 255.255.255.0 broadcast 192.168.0.255,将eth0的ip、掩码和广播地址修改

ifconfig eth0 down,禁用网卡

38、netstat

root@iZ28xhlm5vhZ:~# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:8005          *:*                     LISTEN     
tcp        0      0 *:8009                  *:*                     LISTEN     
tcp        0      0 *:mysql                 *:*                     LISTEN     
tcp        0      0 *:http-alt              *:*                     LISTEN     
tcp        0      0 *:http                  *:*                     LISTEN     
tcp        0      0 *:ftp                   *:*                     LISTEN     
tcp        0      0 *:ssh                   *:*                     LISTEN     
tcp        0      0 139.129.27.76:34243     42.156.166.25:http      ESTABLISHED
tcp        0      0 139.129.27.76:ssh       14.125.141.6:60949      ESTABLISHED

root@iZ28xhlm5vhZ:~# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 139.129.27.76:ntp       *:*                                
udp        0      0 iZ28xhlm5vhZ:ntp        *:*                                
udp        0      0 localhost:ntp           *:*                                
udp        0      0 *:ntp                   *:*                                
udp6       0      0 [::]:ntp                [::]:* 

root@iZ28xhlm5vhZ:~# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:8009            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 139.129.27.76:34243     42.156.166.25:80        ESTABLISHED
tcp        0      0 139.129.27.76:80        14.125.141.6:61997      TIME_WAIT  
tcp        0      0 139.129.27.76:80        14.125.141.6:61998      TIME_WAIT  
tcp        0      0 139.129.27.76:22        14.125.141.6:60949      ESTABLISHED

root@iZ28xhlm5vhZ:~# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN      20938/java      
tcp        0      0 0.0.0.0:8009            0.0.0.0:*               LISTEN      20938/java      
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      12409/mysqld    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      20938/java      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2008/apache2    
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      26779/vsftpd    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      741/sshd        
tcp        0      0 139.129.27.76:34243     42.156.166.25:80        ESTABLISHED 8759/AliYunDun  
tcp        0    332 139.129.27.76:22        14.125.141.6:60949      ESTABLISHED 11198/4  

查看网络状态,包括协议和端口等的情况。

选项:a表示显示所有socket(包括正在监听的),n表示以ip代替名称来显示(如127.0.0.1代替localhost),t表示显示tcp连接,u表示显示udp连接,p表示显示进程id和程序名称

39、top

top - 15:26:00 up 89 days,  5:30,  1 user,  load average: 2.00, 2.01, 2.05
Tasks: 113 total,   2 running, 111 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.7 sy,  0.0 ni,  0.0 id, 98.7 wa,  0.3 hi,  0.0 si,  0.0 st
KiB Mem:   1016272 total,   927448 used,    88824 free,   140456 buffers
KiB Swap:        0 total,        0 used,        0 free.   348492 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                         
20938 root      20   0 2208564 123464  10964 S  0.3 12.1  36:16.78 java                            
12409 mysql     20   0  689864  55752   2500 S  0.0  5.5  51:55.90 mysqld                          
21767 www-data  20   0  391520  34280  23812 S  0.0  3.4  10:41.19 apache2

显示cpu进程信息,自动刷新。可以运用一些交互命令去排序等

http://man.linuxde.net/top

40、free

root@iZ28xhlm5vhZ:~# free
             total       used       free     shared    buffers     cached
Mem:       1016272     927140      89132      60724     140464     348492
-/+ buffers/cache:     438184     578088
Swap:            0          0          0

内存使用情况

选项:h同样可以将数值变成容易读的数,如100M;s后接秒数参数,表示每几秒动态刷新显示。

41、lsof

root@iZ28xhlm5vhZ:~# lsof -i :22
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
sshd      741 root    3u  IPv4    8554      0t0  TCP *:ssh (LISTEN)
sshd    11198 root    3u  IPv4 1578484      0t0  TCP 139.129.27.76:ssh->14.125.141.6:60949 (ESTABLISHED)

列出打开的进程情况

http://man.linuxde.net/lsof

42、sed && awk

以行为单位来处理文本,相对于awk来说比较简单,但是sed的列处理能力比较差

例子较多,以后补充。。

原文地址:https://www.cnblogs.com/tyrus/p/linux_instructions.html