美文网首页
Linux就该这么学 -- 第2章 新手必须掌握的命令

Linux就该这么学 -- 第2章 新手必须掌握的命令

作者: liaozb1996 | 来源:发表于2018-03-03 22:08 被阅读0次

    帮助

    • man
    man [command name]
    ##  显示命令的使用手册
    # 空格键 -- 下一页
    # /<key> -- 向下搜索
    # ?<key> -- 向上搜索
    # n --  下一个关键字
    # N -- 上一个关键字
    
    • whatis
    whatis [command name]
    ##  显示命令的简介
    

    常用命令

    • echo
    echo [$parameter] [string]
    ## 打印变量的值 或 打印字符串
    
    • date
    date [+format] [-s <string for setting date>]
    ##  显示或设置日期
    # 在 date 命令后面带上加号和格式标识符,可使 date 按指定格式显示日期信息(使用 date -h 查看可用格式标识符)
    # %H -- 小时 (00-23)
    # %I -- 小时 (01-12)大写 i
    # %j -- 现在是一年中的第几天
    
    $ date '+%Y-%m-%d %H:%M:%S'
    2018-03-03 21:15:58
    
    • reboot
    • poweroff

    下载命令

    • wget
    • curl
    wget [url]
    # -b -- 后台下载
    # -p <path> -- 保存到目录,大写 p
    # -t -- 重试次数
    # -c -- 续点续传 (使用 ctrl-c 中断后,再次执行命令继续下载)
    

    进程

    查看进程信息

    ps aux
    # a -- 显示所有进程
    # u -- 列出所属用户
    # x -- BSD 风格 (命令参数不需要加前缀 -)
    
    top  
    ##  动态显示进程信息
    ## load average: 系统1,5,15 分钟内的负载情况;值越小负载越低;一般小于 1,生产环境小于5
    
    top.PNG
    pidof <server name>
    ##  显示进程的pid
    
    kill <pid>
    ## 发送信号至进程,默认是终止信号
    
    killall <server name>
    ## 终止服务对应的所有进程
    

    网络

    • ifconfig
    ifconfig [interface name]
    ## 查看ip, mac地址 / 网络接口名称
    

    ifconfig 正在逐步被淘汰

    • ip
    ip addr
    ## 显示所有网卡的信息
    ip addr show <interface name>
    
    ### Arch Wiki Example
      # ip link set eth0 up
      # ip addr add 192.168.1.2/24 broadcast 192.168.1.255 dev eth0
      # ip route add default via 192.168.1.1
    

    Arch Wiki -- 设置静态ip地址

    系统信息

    uname -a
    ## 显示内核信息
    

    操作系统信息一般保存在 /etc 目录下,文件名包含 release 的文件中

    ~ $ ls /etc/ | grep 'release'
    lsb-release
    os-release
    ~ $ cat /etc/os-release
    NAME="Ubuntu"
    VERSION="16.04.3 LTS (Xenial Xerus)"
    ...
    
    • uptime
    ~ $ uptime
     12:28:49 up  1:53,  0 users,  load average: 0.52, 0.58, 0.59
    # 显示系统运行时间,相当于 top 命令的第一行
    
    • free:查看内存的使用情况
    ~ $ free -h
                  total        used        free      shared  buff/cache   available
    Mem:           3.9G        2.1G        1.5G         17M        230M        1.6G
    Swap:          9.9G        341M        9.6G
    
    • history:显示执行过的命令
      • 文件: ~/.bash_history
      • 环境变量:HISTSIZE (/etc/profile)
    history
    # 显示执行过的命令
    ## 使用 ! 加行号执行命令
    ~/download $ wget -c http://mirrors.163.com/archlinux/iso/2018.03.01/archlinux-2018.03.01-x86_64.iso
    >>> archlinux-2018.03.01-x86_64.i   3%[>                                                ]  21.18M  3.89MB/s    eta 2m 23s ^C
    ~/download $ history | grep wget
      239  history | grep wget
      241  wget --help
      242  wget -c http://mirrors.163.com/archlinux/iso/2018.03.01/archlinux-2018.03.01-x86_64.iso
      243  history | grep wget
    ~/download $ !242
        ## 使用 ! 加行号执行命令
    wget -c http://mirrors.163.com/archlinux/iso/2018.03.01/archlinux-2018.03.01-x86_64.iso
    >>> archlinux-2018.03.01-x86_64.i   5%[+>                                               ]  32.99M  3.71MB/s    eta 2m 20s 
    
    histroy -c 
    # 清除历史记录
    
    • sosreport:收集和打包系统(配置)信息,并输出诊断报告

    sos: 求救信号
    report: 报告

    当前工作目录

    • pwd: 显示当前工作目录
    • cd [path]: 切换工作目录
    cd
    # 切换到 home 目录
    cd /etc 
    # 切换到 /etc 目录
    
    • ls: 列出目录下的文件
    ~ $ ls
    download  git
    ~ $ ls -a
    ## 列出所有文件,包括隐藏文件
    .   .bash_history  .bashrc  .config   git         .ipython  .local  .nvm      .ssh                       .viminfo
    ..  .bash_logout   .cache   download  .gitconfig  .lesshst  .npm    .profile  .sudo_as_admin_successful  .virtualenvs
    ~ $ ls -l
    ## 以列表形式输出
    total 0
    drwxrwxrwx 0 ubuntu ubuntu 4096 Mar  4 12:44 download
    drwxrwxrwx 0 ubuntu ubuntu 4096 Feb 12 18:57 git
    ~ $ ls -dl
    ## 列出指定目录的信息,而非该目录下的文件
    drwxr-xr-x 0 ubuntu ubuntu 4096 Mar  3 21:30 .
    

    查看文件

    • cat
    ~ $ cat -n /etc/passwd
            ## n -- 显示行号
         1  root:x:0:0:root:/root:/bin/bash
         2  daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
         3  bin:x:2:2:bin:/bin:/usr/sbin/nologin
         4  sys:x:3:3:sys:/dev:/usr/sbin/nologin
    
    • less <path>:查看长文件,每次仅输出一页的内容
    less <path>
    # 空格键 -- 下一页
    # /<key> -- 向下查找
    # ?<key> -- 向上查找
    # n -- 下一个关键字
    # N -- 上一个关键字
    
    • head
    • tail
    head -n 5 <path>
    # 查看前行的内容,不指定时,默认为10
    ~ $ head -n 3 /etc/passwd | cat -n
      # 显示行号
         1  root:x:0:0:root:/root:/bin/bash
         2  daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
         3  bin:x:2:2:bin:/bin:/usr/sbin/nologin
    
    tail -f -n 5 <path>
     # -f -- 动态显示内容更新
    
    • tr: 从 stdin 读取内容,替换后,输出到 stdout
    ~ $ cat /etc/os-release | tr [a-z] [A-Z]
        # 将小写替换为大写
    NAME="UBUNTU"
    VERSION="16.04.3 LTS (XENIAL XERUS)"
    ID=UBUNTU
    ID_LIKE=DEBIAN
    
    • wc: 打印行数,字数,字节数
    ~ $ grep nologin /etc/passwd  | wc -l
    # 打印出不可登陆的用户数
    17
    
    # l -- line
    # w -- word
    # c -- charater 
    
    • cut: 按列提取内容
    ~ $ cut -d: -f 1-3 /etc/passwd
    root:x:0
    daemon:x:1
    bin:x:2
        # d -- 指定分隔符
        # f -- 指定要提取的列
            ## 1- 从第一列到结尾
            ## -3 从第一列到第三列
            ## 1-3 从第一列到第三列
    
    • diff -c : 比较文件
    ~/test $ cat hello
    import re
    
    print('hello')
    
    
    
    ~/test $ cat hello~
    import re
    # print something
    print('hello')
    print('world')
    
    
    ~/test $ diff -c hello hello~
    *** hello       2018-03-04 13:49:00.466506500 +0800
    --- hello~      2018-03-04 13:49:36.287373800 +0800
    ***************
    *** 1,3 ****  # 1 到 3 行
      import re
    !            # 不一样的行
      print('hello')
    --- 1,4 ----
      import re
    ! # print something
      print('hello')
    + print('world')    # 新增的行
    

    文件目录管理

    • touch: 创建新文件/修改文件的时间属性
    • mkdir: 创建目录(-p 递归创建目录)
    • mv: 移动/重命名文件
    • cp:复制文件
    • rm: 删除文件
    • rmdir: 删除空目录
    • file:查看文件类型(当不能通过后缀判断时)
    cp  <source> <destination>
    # r -- 递归复制(用于复制目录)
    # v -- 输出复制过程
    # i -- 询问是否覆盖
    # a -- (相当于 -pdr ; p: 保留原始文件属性;d: 保留 链接文件 属性)
    
    rm <path>
    # r -- 递归删除(用于删除目录)
    # f -- 强制删除
    # i -- 询问是否删除
    

    文档打包

    • tar

    tar 命令比较特殊,它的参数不需要前缀 -

    tar 本身时将多个文件打包成一个文件(不压缩),但可以使用参数指定压缩类型

    ~/test $ ls
    hello  hello~
    ~/test $
    ~/test $
    ~/test $ tar zcvf hello.tar.gz hello*
        # 打包当前目录下的两个文件
    hello
    hello~
    ~/test $ tar tf hello.tar.gz
        # 查看压缩包里面的内容
    hello
    hello~
    ~/test $ mkdir new_file
    ~/test $ tar zxvf hello.tar.gz -C ./new_file/
        # 解压到指定目录
    hello
    hello~
    ~/test $ ls new_file/
    hello  hello~
    
        # 压缩形式
        ## z -- gz (gzip)
        ## j -- bz (bzip2)
        # 操作
        ## c -- 创建压缩包
        ## x -- 解压
        ## t -- 查看压缩包里面的内容
        # 其他
        ## v -- 显示详细信息
        ## f -- 指定压缩文件名 (一般作为最后一个参数)
        ## -C -- 大写c,指定解压的目的路径
    

    查找

    • grep: 在文件的内容中查找
        grep 'regex' filename [option]
        # i -- ignore 忽略大小写
        # v -- 反向匹配
        # n -- 显示行号
        # c -- 仅显示匹配到的数量
    
    • find: 查找某个文件
    ~/test $ ls
    hello  hello~  hello.tar.gz  new_file
    
    ~/test $ find -maxdepth 1 -name 'hello*'
        # maxdepth: 指定最大深度
        # name: 文件名称,用单引号括起来,避免shell本身的通配符解释
    ./hello
    ./hello.tar.gz
    ./hello~
    
    ~/test $ find -maxdepth 1 -name 'hello*' -exec rm -rf {} \;
        # exec -- 对结果执行命令
        ## {} 表示结果
        ## \; 表达式以 ;(分号)结尾 (\ 是转义序列)
    ~/test $ ls
    new_file
    
    
        # user -- 使用者
        # group -- 组
        # nouser -- 无使用者
        # nogroup -- 无组
        # type b/d/f/c/p/l -- 文件类型
        ## b: block 块设备
        ## d: directory 目录
        ## f: file 文本文件
        ## c: charater 字符设备
        ## p: pipeline 管道文件
        ## l: link 链接文件
    

    相关文章

      网友评论

          本文标题:Linux就该这么学 -- 第2章 新手必须掌握的命令

          本文链接:https://www.haomeiwen.com/subject/ilsxfftx.html