基础命令

Linux系统的命令通常都是如下所示的格式:

  1. 命令名称 [命名参数] [命令对象]
  1. 获取登录信息 - w / who / last/ lastb

    1. [root ~]# w
    2. 23:31:16 up 12:16, 2 users, load average: 0.00, 0.01, 0.05
    3. USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    4. root pts/0 182.139.66.250 23:03 4.00s 0.02s 0.00s w
    5. jackfrue pts/1 182.139.66.250 23:26 3:56 0.00s 0.00s -bash
    6. [root ~]# who
    7. root pts/0 2018-04-12 23:03 (182.139.66.250)
    8. jackfrued pts/1 2018-04-12 23:26 (182.139.66.250)
    9. [root ~]# who am i
    10. root pts/0 2018-04-12 23:03 (182.139.66.250)
    11. [root ~]# who mom likes
    12. root pts/0 2018-04-12 23:03 (182.139.66.250)
    13. [root ~]# last
    14. root pts/0 117.136.63.184 Sun May 26 18:57 still logged in
    15. reboot system boot 3.10.0-957.10.1. Mon May 27 02:52 - 19:10 (-7:-42)
    16. root pts/4 117.136.63.184 Sun May 26 18:51 - crash (08:01)
    17. root pts/4 117.136.63.184 Sun May 26 18:49 - 18:49 (00:00)
    18. root pts/3 117.136.63.183 Sun May 26 18:35 - crash (08:17)
    19. root pts/2 117.136.63.183 Sun May 26 18:34 - crash (08:17)
    20. root pts/0 117.136.63.183 Sun May 26 18:10 - crash (08:42)
  2. 查看自己使用的Shell - ps

    Shell也被称为“壳”或“壳程序”,它是用户与操作系统内核交流的翻译官,简单的说就是人与计算机交互的界面和接口。目前很多Linux系统默认的Shell都是bash(Bourne Again SHell),因为它可以使用tab键进行命令和路径补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作。

    1. [root ~]# ps
    2. PID TTY TIME CMD
    3. 3531 pts/0 00:00:00 bash
    4. 3553 pts/0 00:00:00 ps
  3. 查看命令的说明和位置 - whatis / which / whereis

    1. [root ~]# whatis ps
    2. ps (1) - report a snapshot of the current processes.
    3. [root ~]# whatis python
    4. python (1) - an interpreted, interactive, object-oriented programming language
    5. [root ~]# whereis ps
    6. ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz
    7. [root ~]# whereis python
    8. python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
    9. [root ~]# which ps
    10. /usr/bin/ps
    11. [root ~]# which python
    12. /usr/bin/python
  4. 清除屏幕上显示的内容 - clear

  5. 查看帮助文档 - man / info / —help / apropos

    1. [root@izwz97tbgo9lkabnat2lo8z ~]# ps --help
    2. Usage:
    3. ps [options]
    4. Try 'ps --help <simple|list|output|threads|misc|all>'
    5. or 'ps --help <s|l|o|t|m|a>'
    6. for additional help text.
    7. For more details see ps(1).
    8. [root@izwz97tbgo9lkabnat2lo8z ~]# man ps
    9. PS(1) User Commands PS(1)
    10. NAME
    11. ps - report a snapshot of the current processes.
    12. SYNOPSIS
    13. ps [options]
    14. DESCRIPTION
    15. ...
  6. 查看系统和主机名 - uname / hostname

    1. [root@izwz97tbgo9lkabnat2lo8z ~]# uname
    2. Linux
    3. [root@izwz97tbgo9lkabnat2lo8z ~]# hostname
    4. izwz97tbgo9lkabnat2lo8z
    5. [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release
    6. CentOS Linux release 7.6.1810 (Core)

    说明:cat是连接文件内容并打印到标准输出的命令,后面会讲到该命令;/etc是Linux系统上的一个非常重要的目录,它保存了很多的配置文件;centos-release是该目录下的一个文件,因为我自己使用的Linux发行版本是CentOS 7.6,因此这里会有一个这样的文件。

  7. 时间和日期 - date / cal

    1. [root@iZwz97tbgo9lkabnat2lo8Z ~]# date
    2. Wed Jun 20 12:53:19 CST 2018
    3. [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal
    4. June 2018
    5. Su Mo Tu We Th Fr Sa
    6. 1 2
    7. 3 4 5 6 7 8 9
    8. 10 11 12 13 14 15 16
    9. 17 18 19 20 21 22 23
    10. 24 25 26 27 28 29 30
    11. [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal 5 2017
    12. May 2017
    13. Su Mo Tu We Th Fr Sa
    14. 1 2 3 4 5 6
    15. 7 8 9 10 11 12 13
    16. 14 15 16 17 18 19 20
    17. 21 22 23 24 25 26 27
    18. 28 29 30 31
  8. 重启和关机 - reboot / shutdown

    1. [root ~]# shutdown -h +5
    2. Shutdown scheduled for Sun 2019-05-26 19:34:27 CST, use 'shutdown -c' to cancel.
    3. [root ~]#
    4. Broadcast message from root (Sun 2019-05-26 19:29:27 CST):
    5. The system is going down for power-off at Sun 2019-05-26 19:34:27 CST!
    6. [root ~]# shutdown -c
    7. Broadcast message from root (Sun 2019-05-26 19:30:22 CST):
    8. The system shutdown has been cancelled at Sun 2019-05-26 19:31:22 CST!
    9. [root ~]# shutdown -r 23:58
    10. Shutdown scheduled for Sun 2019-05-26 23:58:00 CST, use 'shutdown -c' to cancel.
    11. [root ~]# shutdown -c
    12. Broadcast message from root (Sun 2019-05-26 19:31:06 CST):
    13. The system shutdown has been cancelled at Sun 2019-05-26 19:32:06 CST!

    说明:在执行shutdown命令时会向登录系统的用户发出警告,可以在命令后面跟上警告消息来替换默认的警告消息,也可以在-h参数后通过now来表示立刻关机。

  9. 退出登录 - exit / logout

  10. 查看历史命令 - history

    1. [root@iZwz97tbgo9lkabnat2lo8Z ~]# history
    2. ...
    3. 452 ls
    4. 453 cd Python-3.6.5/
    5. 454 clear
    6. 455 history
    7. [root@iZwz97tbgo9lkabnat2lo8Z ~]# !454

    说明:查看到历史命令之后,可以用!历史命令编号来重新执行该命令;通过history -c可以清除历史命令。