Linux操作系统 - 01 Linux基本命令

Linux操作系统 - 基本命令

一.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

四.基础命令

  1. 快捷键 - ctrl + h退格 / ctrl + w全部清空 / ctrl + c终止命令 / ctrl + z停止命令 并未终止(放在后台) / clear / ctrl + l 清屏

  2. 获取登录信息 - w / who / last / who am i

    [root@izwz97tbgo9lkabnat2lo8z ~]# 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@izwz97tbgo9lkabnat2lo8z ~]# 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@izwz97tbgo9lkabnat2lo8z ~]# who am i
    root     pts/0        2018-04-12 23:03 (182.139.66.250)
  3. 查看自己使用的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
  4. 查看进程 - netstat -lntp / ps -ef

    ps aux | grep 端口号  : 查看指定端口进程
    ps -ef | grep 程序名  : 查看某个程序进程
    kill -9 进程编号  : 关闭进程
  5. 查看命令的说明 - whatis 命令

    [root@izwz97tbgo9lkabnat2lo8z ~]# whatis ps
    ps (1)        - report a snapshot of the current processes.
    [root@izwz97tbgo9lkabnat2lo8z ~]# whatis python
    python (1)    - an interpreted, interactive, object-oriented programming language
  6. 查看命令的位置 - which / whereis 命令

    [root@izwz97tbgo9lkabnat2lo8z ~]# whereis ps
    ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz
    [root@izwz97tbgo9lkabnat2lo8z ~]# 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@izwz97tbgo9lkabnat2lo8z ~]# which ps
    /usr/bin/ps
    [root@izwz97tbgo9lkabnat2lo8z ~]# which python
    /usr/bin/python
  7. 查看命令帮助文档 - man / info / apropos 命令 / 命令 –help

    [root@izwz97tbgo9lkabnat2lo8z ~]# ps --help
    Usage:
    ps [options]
    Try 'ps --help <simple|list|output|threads|misc|all>'
     or 'ps --help <s|l|o|t|m|a>'
    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
    ...
    [root@izwz97tbgo9lkabnat2lo8z ~]# info ps
    ...
  8. 查看相关命令 - man -k 命令。

    扫描二维码关注公众号,回复: 2953017 查看本文章
  9. 恢复命令 - fg %1

  10. 切换用户 - su

    [root@izwz97tbgo9lkabnat2lo8z ~]# su hellokitty
    [hellokitty@izwz97tbgo9lkabnat2lo8z root]$
  11. 以管理员身份执行命令 - sudo

    [jackfrued@izwz97tbgo9lkabnat2lo8z ~]$ ls /root
    ls: cannot open directory /root: Permission denied
    [jackfrued@izwz97tbgo9lkabnat2lo8z ~]$ sudo ls /root
    [sudo] password for jackfrued:
    calendar.py  code  error.txt  hehe  hello.c  index.html  myconf  result.txt

    说明:如果希望用户能够以管理员身份执行命令,用户必须被添加到sudoers名单中,该文件在 /etc目录下。

  12. 登入登出用户 - logout断开连接 / exit退出登录

  13. 重启和关机 - reboot / init 6shutdown / init 0

  14. 查看系统和主机名 - uname / hostname

    [root@izwz97tbgo9lkabnat2lo8z ~]# uname
    Linux
    [root@izwz97tbgo9lkabnat2lo8z ~]# hostname
    izwz97tbgo9lkabnat2lo8z
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release
    CentOS Linux release 7.4.1708 (Core) 
  15. 用户相关(命令后面跟用户名) - adduser 创建用户 / userdel 删除用户 / passwd 设置用户密码 / ssh 用户名@ip地址- 连接到其他用户 / su 切换用户 / sudo ls /root 以管理员身份执行命令

    [root@izwz97tbgo9lkabnat2lo8z ~]# adduser hellokitty
    [root@izwz97tbgo9lkabnat2lo8z ~]# passwd hellokitty
    Changing password for user jackfrued.
    New password:
    Retype new password:
    passwd: all authentication tokens updated successfully.
    [root@izwz97tbgo9lkabnat2lo8z ~]# ssh [email protected]
    hellokitty@1.2.3.4's password:
    Last login: Thu Apr 12 23:05:32 2018 from 10.12.14.16
    [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ logout
    Connection to 1.2.3.4 closed.
    [root@izwz97tbgo9lkabnat2lo8z ~]#
  16. 查看历史命令 - 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@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir abc
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir -p xyz/abc
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# rmdir abc
    
    mkdir  -p  文件名1/文件名2/文件名3  递归创建目录
  2. 创建/删除文件 - touch / rm

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

    cd  文件名         : 进入当前文件名目录
    cd /usr/local/src  : 按相对路径查找
    cd /  : 进入计算机主目录
    cd ~  : 进入当前用户主目录
    cd -  : 返回上次进入的目录
    cd .. : 返回上一级目录

    说明:cd命令后面可以跟相对路径(以当前路径作为参照)或绝对路径(以/开头)来切换到指定的目录,也可以用cd ..来返回上一级目录。

  4. 查看目录内容 - ls

    • -l:以长格式查看文件和目录。
    • -a:显示以点开头的文件和目录(隐藏文件)。
    • -al /目录名 : 长格式查看当前目录下所有文件。
    • -R:遇到目录要进行递归展开(继续列出目录下面的文件和目录)。
    • -d:只列出目录,不列出其他内容。
    • -S/-t:按大小/时间排序。
    • -h : 文件大小显示为常见大小单位 B KB MB …
  5. 查看文件内容 - cat / head / tail / more / less

    cat 文件名  :  查看打印文件里面内容
    cat -n 文件名 : 查看文件内容,并列出行号
    cat 文件名 | grep '字符串'  : 查找含有字符串的行
    
    head 文件名  : 查看文件头几行(默认显示10行)
    head -n 文件名  : 查看文件前n行内容
    
    tail -n 文件名  : 查看文件后n行内容
    
    more 文件名  : 分屏显示文件内容
    cat -n 文件名 | more  : 查看文件时按回车一行一行查看
    
    less 文件名  :在查看之前不会加载整个文件(向下翻再加载)
    
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# 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@iZwz97tbgo9lkabnat2lo8Z ~]# cat sohu.html
    ...
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# 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@iZwz97tbgo9lkabnat2lo8Z ~]# tail -2 sohu.html
    </body>
    </html>
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# less sohu.html
    ...
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat -n sohu.html | more
    ...
  6. 拷贝/移动文件 - cp / mv

    cp 文件名 目标位置  : 复制文件
    cp 文件名 目标位置/文件新名称  : 复制文件并改名
    
    mv 文件名 新文件名  : 更改文件名
    mv /root/test /tmp/  : 将/root/下的test文件移动到/tmp/目录下
    mv /root/test /root/newtest  : 将/root/下的test文件改名为newtest
    
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir backup
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cp sohu.html backup/
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cd backup
    [root@iZwz97tbgo9lkabnat2lo8Z backup]# ls
    sohu.html
    [root@iZwz97tbgo9lkabnat2lo8Z backup]# mv sohu.html sohu_index.html
    [root@iZwz97tbgo9lkabnat2lo8Z backup]# ls
    sohu_index.html
  7. 查找文件和查找内容 - find / grep

    find / name 文件A  : 查找目录下名为A的文件
    find / -name "*.html"  : 查找目录下所有的后缀为html文件
    
    egrep 正则表达式 文件名 -n  : 查找文件中的内容
    
    [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 "<script>" sohu.html -n
    20:<script>
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# grep -E \<\/?script.*\> sohu.html -n
    20:<script>
    22:</script>
    24:<script src="//statics.itc.cn/web/v3/static/js/es5-shim-08e41cfc3e.min.js"></script>
    25:<script src="//statics.itc.cn/web/v3/static/js/es5-sham-1d5fa1124b.min.js"></script>
    26:<script src="//statics.itc.cn/web/v3/static/js/html5shiv-21fc8c2ba6.js"></script>
    29:<script type="text/javascript">
    52:</script>
    ...

    说明:grep在搜索字符串时可以使用正则表达式,如果需要使用正则表达式可以用grep -E或者直接使用egrep

  8. 创建软连接 - ln

    ln -s  源文件 目标文件  : 创建链接文件(文件名都必须写绝对路径)
    
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html
    -rw-r--r-- 1 root root 212131 Jun 20 19:15 sohu.html
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html
    -rw-r--r-- 2 root root 212131 Jun 20 19:15 sohu.html
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup2
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html
    -rw-r--r-- 3 root root 212131 Jun 20 19:15 sohu.html
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln -s /etc/centos-release sysinfo
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sysinfo
    lrwxrwxrwx 1 root root 19 Jun 20 19:21 sysinfo -> /etc/centos-release
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat sysinfo
    CentOS Linux release 7.4.1708 (Core)
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release
    CentOS Linux release 7.4.1708 (Core)

    说明:链接可以分为硬链接和软链接(符号链接)。硬链接可以认为是一个指向文件数据的指针,就像Python中对象的引用计数,每添加一个硬链接,文件的对应链接数就增加1,只有当文件的链接数为0时,文件所对应的存储空间才有可能被其他文件覆盖。我们平常删除文件时其实并没有删除硬盘上的数据,我们删除的只是一个指针,或者说是数据的一条使用记录,所以类似于“文件粉碎机”之类的软件在“粉碎”文件时除了删除文件指针,还会在文件对应的存储区域填入数据来保证文件无法再恢复。软链接类似于Windows系统下的快捷方式,当软链接链接的文件被删除时,软链接也就失效了。

  9. 压缩/解压缩和归档/解归档 - gzip / gunzip / xz / tar

    gzip : 压缩
    gunzip  : 解压缩(解压的扩展名.gz)
    
    tar -xvf  : 解归档
    tar -cvf  : 归档
    
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz
    --2018-06-20 19:29:59--  http://download.redis.io/releases/redis-4.0.10.tar.gz
    Resolving download.redis.io (download.redis.io)... 109.74.203.151
    Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 1738465 (1.7M) [application/x-gzip]
    Saving to: ‘redis-4.0.10.tar.gz100%[==================================================>] 1,738,465   70.1KB/s   in 74s
    2018-06-20 19:31:14 (22.9 KB/s) - ‘redis-4.0.10.tar.gz’ saved [1738465/1738465]
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*
    redis-4.0.10.tar.gz
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip redis-4.0.10.tar.gz
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*
    redis-4.0.10.tar
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf redis-4.0.10.tar
    redis-4.0.10/
    redis-4.0.10/.gitignore
    redis-4.0.10/00-RELEASENOTES
    redis-4.0.10/BUGS
    redis-4.0.10/CONTRIBUTING
    redis-4.0.10/COPYING
    redis-4.0.10/INSTALL
    redis-4.0.10/MANIFESTO
    redis-4.0.10/Makefile
    redis-4.0.10/README.md
    redis-4.0.10/deps/
    redis-4.0.10/deps/Makefile
    redis-4.0.10/deps/README.md
    ...
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*
    redis-4.0.10.tar
    redis-4.0.10:
    00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
    BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
    CONTRIBUTING     INSTALL  README.md  runtest-cluster  src
  10. 其他工具 - sort / uniq / diff / tr / cut / paste / file / wc

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat foo.txt
    grape
    apple
    pitaya
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat bar.txt
    100
    200
    300
    400
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt
    grape   100
    apple   200
    pitaya  300
    400
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt > hello.txt
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cut -b 4-8 hello.txt
    pe      10
    le      20
    aya     3
    0
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat hello.txt | tr '\t' ','
    grape,100
    apple,200
    pitaya,300
    ,400
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# wget https://www.baidu.com/img/bd_logo1.png
    --2018-06-20 18:46:53--  https://www.baidu.com/img/bd_logo1.png
    Resolving www.baidu.com (www.baidu.com)... 220.181.111.188, 220.181.112.244
    Connecting to www.baidu.com (www.baidu.com)|220.181.111.188|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 7877 (7.7K) [image/png]
    Saving to: ‘bd_logo1.png100%[==================================================>] 7,877       --.-K/s   in 0s
    2018-06-20 18:46:53 (118 MB/s) - ‘bd_logo1.png’ saved [7877/7877][root@iZwz97tbgo9lkabnat2lo8Z ~]# file bd_logo1.png
    bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# wc sohu.html
    2979   6355 212527 sohu.html
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# wc -l sohu.html
    2979 sohu.html

六.管道和重定向

  1. 管道的使用 - |

    例子:查找当前目录下文件个数。

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# find ./ | wc -l
    6152

    例子:列出当前路径下的文件和文件夹,给每一项加一个编号。

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls | cat -n
        1  dump.rdb
        2  mongodb-3.6.5
        3  Python-3.6.5
        4  redis-3.2.11
        5  redis.conf

    例子:查找record.log中包含AAA,但不包含BBB的记录的总数

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat record.log | grep AAA | grep -v BBB | wc -l
  2. 输出重定向和错误重定向 - > / >> / 2>

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat readme.txt
    banana
    apple
    grape
    apple
    grape
    watermelon
    pear
    pitaya
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat readme.txt | sort | uniq > result.txt
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat result.txt
    apple
    banana
    grape
    pear
    pitaya
    watermelon
  3. 输入重定向 - \<

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# echo 'hello, world!' > hello.txt
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# wall < hello.txt
    [root@iZwz97tbgo9lkabnat2lo8Z ~]#
    Broadcast message from root@iZwz97tbgo9lkabnat2lo8Z (Wed Jun 20 19:43:05 2018):
    hello, world!
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# echo 'I will show you some code.' >> hello.txt
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# wall < hello.txt
    [root@iZwz97tbgo9lkabnat2lo8Z ~]#
    Broadcast message from root@iZwz97tbgo9lkabnat2lo8Z (Wed Jun 20 19:43:55 2018):
    hello, world!
    I will show you some code.

七.别名

  1. alias

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# alias ll='ls -l'
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# alias frm='rm -rf'
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ll
    ...
    drwxr-xr-x  2 root       root   4096 Jun 20 12:52 abc
    ...
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# frm abc
  2. unalias

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# unalias frm
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# frm sohu.html
    -bash: frm: command not found

八.其他程序

  1. 时间和日期 - 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
    [root@izbp1aqcmdz5fou0bri0mgz ~]# cal 2017
                                  2017                               
          January               February                 March       
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6  7             1  2  3  4             1  2  3  4
    8  9 10 11 12 13 14    5  6  7  8  9 10 11    5  6  7  8  9 10 11
    15 16 17 18 19 20 21   12 13 14 15 16 17 18   12 13 14 15 16 17 18
    22 23 24 25 26 27 28   19 20 21 22 23 24 25   19 20 21 22 23 24 25
    29 30 31               26 27 28               26 27 28 29 30 31
  2. 录制操作脚本 - script

  3. 给用户发送消息 - mesg / write / wall / mail

    write 用户名  发送消息
    wall  发送给所有人
    mesg n 阻止接受消息

九.文件系统(文件和路径)

  1. 命名规则:文件名的最大长度与文件系统类型有关,一般情况下,文件名不应该超过255个字符,虽然绝大多数的字符都可以用于文件名,但是最好使用英文大小写字母、数字、下划线、点这样的符号。文件名中虽然可以使用空格,但应该尽可能避免使用空格,否则在输入文件名时需要用将文件名放在双引号中或者通过\对空格进行转义。
  2. 扩展名:在Linux系统下文件的扩展名是可选的,但是使用扩展名有助于对文件内容的理解。有些应用程序要通过扩展名来识别文件,但是更多的应用程序并不依赖文件的扩展名,就像file命令在识别文件时并不是依据扩展名来判定文件的类型。
  3. 隐藏文件:以点开头的文件在Linux系统中是隐藏文件(不可见文件)。

目录结构

/                 根目录
/bin              命令保存目录(普通用户就可以读取的命令,基本命令的二进制文件)
/boot             启动目录,启动相关文件(引导加载程序的静态文件)
/dev              设备文件保存目录
/etc              配置文件保存目录
/home             普通用户的家目录
/lib              系统库保存目录(共享库文件)
/lib64           共享64位库文件
/mnt              系统挂载目录
/media            挂载目录
/root             超级用户的家目录
/tmp              临时目录
/sbin             命令保存目录(超级用户才能使用的目录)
/proc             直接写入内存的
/sys              将内核的一些信息映射,可供应用程序所用
/usr              系统软件资源目录
/usr/bin/         系统命令(普通用户)
/usr/sbin/        系统命令(超级用户)
/var              系统相关文档内容
/var/log/         系统日志位置
/var/spool/mail/  系统默认邮箱位置
/var/lib/         默认安装的库文件目录

十一.访问权限

  1. chmod - 改变文件模式比特。

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l
    ...
    -rw-r--r--  1 root       root 211878 Jun 19 16:06 sohu.html
    ...
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# chmod g+w,o+w sohu.html
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l
    ...
    -rw-rw-rw-  1 root       root 211878 Jun 19 16:06 sohu.html
    ...
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# chmod 644 sohu.html
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l
    ...
    -rw-r--r--  1 root       root 211878 Jun 19 16:06 sohu.html
    ...

    说明:通过上面的例子可以看出,用chmod改变文件模式比特有两种方式:一种是字符设定法,另一种是数字设定法。除了chmod之外,可以通过umask来设定哪些权限将在新文件的默认权限中被删除。

    长格式查看目录或文件时显示结果及其对应权限的数值如下表所示。

  2. chown - 改变文件所有者。

       [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l
    ...
    -rw-r--r--  1 root root     54 Jun 20 10:06 readme.txt
    ...
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# chown hellokitty readme.txt
    [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l
    ...
    -rw-r--r--  1 hellokitty root     54 Jun 20 10:06 readme.txt
    ...

十二.磁盘管理

  1. 列出文件系统的磁盘使用状况 - df

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        40G  5.0G   33G  14% /
    devtmpfs        486M     0  486M   0% /dev
    tmpfs           497M     0  497M   0% /dev/shm
    tmpfs           497M  356K  496M   1% /run
    tmpfs           497M     0  497M   0% /sys/fs/cgroup
    tmpfs           100M     0  100M   0% /run/user/0
  2. 磁盘分区表操作 - fdisk

    [root@iZwz97tbgo9lkabnat2lo8Z ~]# fdisk -l
    Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000a42f4
      Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048    83884031    41940992   83  Linux
    Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
  3. 格式化文件系统 - mkfs

  4. 文件系统检查 - fsck

  5. 挂载/卸载 - mount / umount

猜你喜欢

转载自blog.csdn.net/hanbo6/article/details/82183296