Linux基础命令

玩转Linux操作系统

说明:本文中对Linux命令的讲解都是基于名为CentOS的Linux发行版本,我自己使用的是阿里云服务器,系统版本为CentOS Linux release 7.6.1810。不同的Linux发行版本在Shell命令和工具程序上会有一些差别,但是这些差别是很小的。

Linux概述

Linux是一个通用操作系统。一个操作系统要负责任务调度、内存分配、处理外围设备I/O等操作。操作系统通常由内核(运行其他程序,管理像磁盘、打印机等硬件设备的核心程序)和系统程序(设备驱动、底层库、shell、服务程序等)两部分组成。

Linux内核是芬兰人Linus Torvalds开发的,于1991年9月发布。而Linux操作系统作为Internet时代的产物,它是由全世界许多开发者共同合作开发的,是一个自由的操作系统(注意自由和免费并不是同一个概念,想了解二者的差别可以点击这里)。

Linux系统优点

  1. 通用操作系统,不跟特定的硬件绑定。
  2. 用C语言编写,可移植性强,有内核编程接口。
  3. 支持多用户和多任务,支持安全的分层文件系统。
  4. 大量的实用程序,完善的网络功能以及强大的支持文档。
  5. 可靠的安全性和良好的稳定性,对开发者更友好。

Linux系统发行版本

  1. Redhat
  2. Ubuntu
  3. CentOS
  4. Fedora
  5. Debian
  6. openSUSE

基础命令

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

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

    [root ~]# w
     23:31:16 up 12:16,  2 users,  load average: 0.00, 0.01, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    182.139.66.250   23:03    4.00s  0.02s  0.00s w
    jackfrue pts/1    182.139.66.250   23:26    3:56   0.00s  0.00s -bash
    [root ~]# who
    root     pts/0        2018-04-12 23:03 (182.139.66.250)
    jackfrued pts/1        2018-04-12 23:26 (182.139.66.250)
    [root ~]# who am i
    root     pts/0        2018-04-12 23:03 (182.139.66.250)
    [root ~]# who mom likes
    root     pts/0        2018-04-12 23:03 (182.139.66.250)
    [root ~]# last
    root     pts/0        117.136.63.184   Sun May 26 18:57   still logged in   
    reboot   system boot  3.10.0-957.10.1. Mon May 27 02:52 - 19:10  (-7:-42)   
    root     pts/4        117.136.63.184   Sun May 26 18:51 - crash  (08:01)    
    root     pts/4        117.136.63.184   Sun May 26 18:49 - 18:49  (00:00)    
    root     pts/3        117.136.63.183   Sun May 26 18:35 - crash  (08:17)    
    root     pts/2        117.136.63.183   Sun May 26 18:34 - crash  (08:17)    
    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键进行命令和路径补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作。

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

    [root ~]# whatis ps
    ps (1)        - report a snapshot of the current processes.
    [root ~]# whatis python
    python (1)    - an interpreted, interactive, object-oriented programming language
    [root ~]# whereis ps
    ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz
    [root ~]# whereis python
    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
    [root ~]# which ps
    /usr/bin/ps
    [root ~]# which python
    /usr/bin/python
  4. 清除屏幕上显示的内容 - clear

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

    [root@izwz97tbgo9lkabnat2lo8z ~]# ps --help
    Usage:
     ps [options]
     Try 'ps --help '
      or 'ps --help '
     for additional help text.
    For more details see ps(1).
    [root@izwz97tbgo9lkabnat2lo8z ~]# man ps
    PS(1)                                User Commands                                PS(1)
    NAME
           ps - report a snapshot of the current processes.
    SYNOPSIS
           ps [options]
    DESCRIPTION
    ...
  6. 查看系统和主机名 - uname / hostname

    [root@izwz97tbgo9lkabnat2lo8z ~]# uname
    Linux
    [root@izwz97tbgo9lkabnat2lo8z ~]# hostname
    izwz97tbgo9lkabnat2lo8z
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release
    CentOS Linux release 7.6.1810 (Core)

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

  7. 时间和日期 - date / cal

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# date
    Wed Jun 20 12:53:19 CST 2018
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal
          June 2018
    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
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal 5 2017
          May 2017
    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
  8. 重启和关机 - reboot / shutdown

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

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

  9. 退出登录 - exit / logout

  10. 查看历史命令 - history

[root@iZwz97tbgo9lkabnat2lo8Z ~]# history
...
452  ls
453  cd Python-3.6.5/
454  clear
455  history
[root@iZwz97tbgo9lkabnat2lo8Z ~]# !454

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

实用程序

文件和文件夹操作
  1. 创建/删除空目录 - mkdir / rmdir

    [root ~]# mkdir abc
    [root ~]# mkdir -p xyz/abc
    [root ~]# rmdir abc
  2. 创建/删除文件 - touch / rm

    [root ~]# touch readme.txt
    [root ~]# touch error.txt
    [root ~]# rm error.txt
    rm: remove regular empty file ‘error.txt’? y
    [root ~]# rm -rf xyz
    • touch命令用于创建空白文件或修改文件时间。在Linux系统中一个文件有三种时间:
      • 更改内容的时间 - mtime。
      • 更改权限的时间 - ctime。
      • 最后访问时间 - atime。
    • rm的几个重要参数:
      • -i:交互式删除,每个删除项都会进行询问。
      • -r:删除目录并递归的删除目录中的文件和目录。
      • -f:强制删除,忽略不存在的文件,没有任何提示。
  3. 切换和查看当前工作目录 - cd / pwd

    说明:cd命令后面可以跟相对路径(以当前路径作为参照)或绝对路径(以/开头)来切换到指定的目录,也可以用cd ..来返回上一级目录。请大家想一想,如果要返回到上上一级目录应该给cd命令加上什么样的参数呢?

  4. 查看目录内容 - ls

    • -l:以长格式查看文件和目录。
    • -a:显示以点开头的文件和目录(隐藏文件)。
    • -R:遇到目录要进行递归展开(继续列出目录下面的文件和目录)。
    • -d:只列出目录,不列出其他内容。
    • -S / -t:按大小/时间排序。
  5. 查看文件内容 - cat / tac / head / tail / more / less / rev / od

    [root ~]# wget http://www.sohu.com/ -O sohu.html
    --2018-06-20 18:42:34--  http://www.sohu.com/
    Resolving www.sohu.com (www.sohu.com)... 14.18.240.6
    Connecting to www.sohu.com (www.sohu.com)|14.18.240.6|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 212527 (208K) [text/html]
    Saving to: ‘sohu.html’
    100%[==================================================>] 212,527     --.-K/s   in 0.03s
    2018-06-20 18:42:34 (7.48 MB/s) - ‘sohu.html’ saved [212527/212527]
    [root ~]# cat sohu.html
    ...
    [root ~]# head -10 sohu.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>搜狐</title>
    <meta name="Keywords" content="搜狐,门户网站,新媒体,网络媒体,新闻,财经,体育,娱乐,时尚,汽车,房产,科技,图片,论坛,微博,博客,视频,电影,电视剧"/>
    <meta name="Description" content="搜狐网为用户提供24小时不间断的最新资讯,及搜索、邮件等网络服务。内容包括全球热点事件、突发新闻、时事评论、热播影视剧、体育赛事、行业动态、生活服务信息,以及论坛、博客、微博、我的搜狐等互动空间。" />
    <meta name="shenma-site-verification" content="1237e4d02a3d8d73e96cbd97b699e9c3_1504254750">
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
    [root ~]# tail -2 sohu.html
    </body>
    </html>
    [root ~]# less sohu.html
    ...
    [root ~]# cat -n sohu.html | more
    ...

    说明:上面用到了一个名为wget的命令,它是一个网络下载器程序,可以从指定的URL下载资源。

  6. 拷贝/移动文件 - cp / mv

    [root ~]# mkdir backup
    [root ~]# cp sohu.html backup/
    [root ~]# cd backup
    [root backup]# ls
    sohu.html
    [root backup]# mv sohu.html sohu_index.html
    [root backup]# ls
    sohu_index.html
  7. 文件重命名 - rename

[root@iZwz97tbgo9lkabnat2lo8Z ~]# rename .htm .html *.htm
  1. 查找文件和查找内容 - find / grep

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# find / -name "*.html"
    /root/sohu.html
    /root/backup/sohu_index.html
    [root@izwz97tbgo9lkabnat2lo8z ~]# find . -atime 7 -type f -print
    [root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -size +2k
    [root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -name "*.swp" -delete
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# grep "
                        
                        

你可能感兴趣的:(开发工具,操作系统,运维)