zoukankan      html  css  js  c++  java
  • Linux学习基础命令(二)


    文件打包

    gzip

    • 压缩文件,压缩后的文件是以.gz结尾
      • 用法:gzip [选项] [文件名]
      • 选项:
        • -d 解压,解压完成后会删除原文件
        • -c 将结果输出至标准输出
        • -# #用1-9代替,指定压缩比,默认为6
    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# gzip anaconda-ks.cfg  //压缩,不保留原文件
    [root@bad test]# ls
    anaconda-ks.cfg.gz
    [root@bad test]# gzip -d anaconda-ks.cfg.gz  //解压缩,不保留压缩包
    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# 
    
    
    • 也可以使用gunzip解压缩,后直接跟压缩的文件名,解压完成后会删除原文件。
    • 使用zcat可以不解压压缩包查看文件内容

    bzip2

    • 压缩文件,压缩后的文件是以.bz2结尾
      • 用法:bzip2 [选项] [文件]
      • 选项:
        • -d 解压缩,并删除原文件
        • -# #用1-9代替,指定压缩比,默认为6
        • -k 即keep,保留原文件压缩
    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# bzip2 anaconda-ks.cfg  //压缩,不保留原文件
    [root@bad test]# ls
    anaconda-ks.cfg.bz2
    [root@bad test]# bzip2 -d anaconda-ks.cfg.bz2  //解压,并删除原压缩包
    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# bzip2 -k anaconda-ks.cfg  //保留原文件压缩
    [root@bad test]# ls
    anaconda-ks.cfg  anaconda-ks.cfg.bz2
    [root@bad test]# 
    
    • 也可以使用bunzip2解压缩,后直接跟压缩的文件名,解压完成后会删除原文件。
    • 使用bzcat可以不解压压缩包查看文件内容

    xz

    • 压缩文件,压缩后的文件是以.xz结尾,比bzip2的压缩比跟大
      • 用法:xz [选项] [文件]
      • 选项
        • -d 解压缩,并删除原文件
        • -# #用1-9代替,指定压缩比,默认为6
        • -k 即keep,保留原文件压缩
    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# xz -k anaconda-ks.cfg  //压缩,保留原文件
    [root@bad test]# ls
    anaconda-ks.cfg  anaconda-ks.cfg.xz
    [root@bad test]# rm -rf anaconda-ks.cfg  //删除anaconda-ks.cfg
    [root@bad test]# ls
    anaconda-ks.cfg.xz
    [root@bad test]# xz -d anaconda-ks.cfg.xz  //解压缩,解压完删除原文件
    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# 
    
    • 也可以使用unxz解压缩,后直接跟压缩的文件名,解压完成后会删除原文件。
      • 使用xzcat可以不解压压缩包查看文件内容
    • gzipbzip2xz都是只能打包压缩文件,无法打包目录。

    compress

    • 这是用的较少的一种压缩格式,压缩的文件是以.Z结尾,解压用uncompress。同样,这条解压命令也是需要使用yum工具下载命令的。
    [root@bad test]# yum install -y compress //下载combress命令
    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# compress anaconda-ks.cfg //压缩
    [root@bad test]# ls
    anaconda-ks.cfg.Z
    [root@bad test]# uncompress anaconda-ks.cfg.Z //解压缩
    [root@bad test]# ls
    anaconda-ks.cfg 
    

    zip

    • 既归档又压缩,可以压缩目录,压缩后,不会删除原文件
      • 用法:zip [压缩包文件名] [打包文件]
    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# zip 1.zip anaconda-ks.cfg  //压缩文件,压缩包名字定义为1.zip
      adding: anaconda-ks.cfg (deflated 37%)
    [root@bad test]# ls
    1.zip  anaconda-ks.cfg
    [root@bad test]# rm -rf anaconda-ks.cfg  //删除anaconda-ks.cfg
    [root@bad test]# ls
    1.zip
    [root@bad test]# unzip 1.zip  //解压,解压完保留原文件
    Archive:  1.zip
      inflating: anaconda-ks.cfg         
    [root@bad test]# ls
    1.zip  anaconda-ks.cfg
    [root@bad test]# 
    
    • 使用unzip [压缩包]可以解压缩。注意!一般系统默认没有这条解压缩的命令,可以使用yum工具安装此命令。当然前提是你配置了yum仓库。
    [root@bad test]# yum install -y unzip //下载解压zip命令
    

    tar

    • 归档工具,只归档不压缩

      • 用法:tar [选项] [文件或目录]
      • 选项:
        • c 创建归档文件
        • f file.tar 指定要操作的归档文件
        • v 显示过程
        • x 还原归档
        • p 归档时保留权限信息。只有管理员才有权限用此选项
        • C 指定还原归档或解压时的目标目录
        • tf 不展开归档,直接查看归档了哪些文件
    • 选项f后必须跟文件名

    • 常用的组合选项:

    -zcf file.tar.gz——归档并调用gzip进行压缩
    -jcf file.tar.bz2——归档并调用bzip2进行压缩
    -Jcf file.tar.xz——归档并调用xz进行压缩
    -zcf file.tar.gz——归档并调用gzip进行压缩
    -jcf file.tar.bz2——归档并调用bzip2进行压缩
    -Jcf file.tar.xz——归档并调用xz进行压缩
    -xf file.tar.gz|file.tar.bz2|file.tar.xz——还原归档并自动根据压缩方式调用相应的工具解压

    [root@bad test]# ls
    anaconda-ks.cfg
    [root@bad test]# tar -zcf 1.tar.gz anaconda-ks.cfg  //调用gzip压缩
    [root@bad test]# ls
    1.tar.gz  anaconda-ks.cfg
    [root@bad test]# rm -rf anaconda-ks.cfg  //删除anaconda-ks.cfg
    [root@bad test]# ls
    1.tar.gz
    [root@bad test]# tar -xf 1.tar.gz  //调用gzip解压
    [root@bad test]# ls
    1.tar.gz  anaconda-ks.cfg
    [root@bad test]#
    
    • 指定解压的目录:
    [root@bad test]# tar -zcf 1.tar.gz anaconda-ks.cfg  //用gzip的格式归档
    [root@bad test]# ls
    1.tar.gz  anaconda-ks.cfg
    [root@bad test]# ls /root/bad/
    [root@bad test]# tar -xf 1.tar.gz -C /root/bad/  //自动调用gzip的格式解压缩,并指定解压目录
    [root@bad test]# ls /root/bad/
    anaconda-ks.cfg
    [root@bad test]# 
    

    系统管理

    which

    • 查找Linux命令文件并显示所在位置,搜索范围由PATH环境变量指定
    [root@bad test]# which ls  //查找ls命令文件的位置
    alias ls='ls --color=auto'
    	/usr/bin/ls
    

    echo

    • 所见即所得,原样输出
    [root@bad test]# echo “bad not bad”
    bad not bad
    [root@bad test]# echo $?  //判断上一条命令是否有错误,返回值为1表示有错误,0表示正确
    0
    [root@bad test]# echo “bad not bad” >> 1.txt  //两个>>是想文件中追加内容
    [root@bad test]# cat 1.txt
    bad not bad
    [root@bad test]# echo “I am bad” > 1.txt  //单个>会覆盖文件中的内容
    [root@bad test]# cat 1.txt
    I am bad
    

    pwd

    • 打印出当前工作目录
    [root@bad test]# pwd
    /root/test
    

    shell

    • 查看当前系统正在使用的shell
    [root@bad test]# echo $SHELL
    /bin/bash
    
    • 查看当前系统支持的所有的shell
    [root@bad test]# cat /etc/shells 
    /bin/sh
    /bin/bash
    /sbin/nologin
    /usr/bin/sh
    /usr/bin/bash
    /usr/sbin/nologin
    /bin/tcsh
    /bin/csh
    
    • 查看bash命令的绝对路径
    [root@bad test]# which bash
    /usr/bin/bash
    

    stat

    • 显示文件或文件系统的详细信息

    type

    • Linux中的命令有两种类型;

      • 内部命令(shell内置)
      • 外部命令:在文件系统的某个路径下有一个与命令名称相应的可执行文件
    • type命令则是检查命令是内部命令还是外部命令

    [root@bad ~]# type cd
    cd is a shell builtin
    [root@bad ~]# type mv
    mv is aliased to `mv -i'
    [root@bad ~]# type ls
    ls is aliased to `ls --color=auto'
    

    file

    • 查看文件类型

    alias

    • 定义别名
      • 用法:alias [定义名]=[‘要定义的命令’]
    • 临时生效
    [root@bad test]# alias cdyum='cd /etc/yum.repos.d/'  //将yum的配置路路径定义别名为cdyum
    [root@bad test]# pwd  //打印出当前所在的目录
    /root/test
    [root@bad test]# cdyum  //执行此命令就会自动执行 cd /etc/yum.repos.d/
    [root@bad yum.repos.d]# pwd
    /etc/yum.repos.d
    
    • 永久生效
    [root@bad ~]# vim .bashrc
    alias cdyum='cd /etc/yum.repos/d'
    
    • 使用unalias可以取消别名
    • 注意这里我们还没有讲vim编辑器工具的使用,我们可以使用前面讲到的>>追加命令向配置文件中加入配置文件(如下)
    [root@bad ~]# echo “alias cdyum='cd /etc/yum.repos/d” >> .bashrc
    

    ldd

    • 查看指定程序有哪些依赖文件库,移除依赖文件,命令则无法使用,可以使用软连接,恢复命令的使用
    [root@bad test]# ldd /usr/bin/ls  //查看ls命令依赖哪些文件库
    	linux-vdso.so.1 =>  (0x00007fff629fe000)
    	libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f86b1705000)
    	libcap.so.2 => /lib64/libcap.so.2 (0x00007f86b1500000)
    	libacl.so.1 => /lib64/libacl.so.1 (0x00007f86b12f6000)
    	libc.so.6 => /lib64/libc.so.6 (0x00007f86b0f35000)
    	libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f86b0cd4000)
    	liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f86b0aae000)
    	libdl.so.2 => /lib64/libdl.so.2 (0x00007f86b08aa000)
    	/lib64/ld-linux-x86-64.so.2 (0x00007f86b193c000)
    	libattr.so.1 => /lib64/libattr.so.1 (0x00007f86b06a5000)
    	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f86b0488000)
    

    ln

    • 软连接
      • 用法:ln -s [源文件或目录 ] [目的文件或目录]
    [root@bad test]# ldd /usr/bin/ls  //查看ls命令的依赖库文件
    	linux-vdso.so.1 =>  (0x00007fff193fe000)
    	libselinux.so.1 => /lib64/libselinux.so.1 (0x00007ff238e85000)
    	libcap.so.2 => /lib64/libcap.so.2 (0x00007ff238c80000)
    	libacl.so.1 => /lib64/libacl.so.1 (0x00007ff238a76000)
    	libc.so.6 => /lib64/libc.so.6 (0x00007ff2386b5000)
    	libpcre.so.1 => /lib64/libpcre.so.1 (0x00007ff238454000)
    	liblzma.so.5 => /lib64/liblzma.so.5 (0x00007ff23822e000)
    	libdl.so.2 => /lib64/libdl.so.2 (0x00007ff23802a000)
    	/lib64/ld-linux-x86-64.so.2 (0x00007ff2390bc000)
    	libattr.so.1 => /lib64/libattr.so.1 (0x00007ff237e25000)
    	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007ff237c08000)
    [root@bad test]# mv /lib64/libselinux.so.1 /tmp/  //将依赖库libselinux.so.1文件移走
    [root@bad test]# ls  //ls命令无法使用
    ls: error while loading shared libraries: libselinux.so.1: cannot open shared object file: No such file or directory
    [root@bad test]# ln -s /tmp/libselinux.so.1 /lib64/  使用软连接,将ls命令的依赖库文件链接到lib64目录下
    [root@bad test]# ls  //ls命令恢复使用
    anaconda-ks.cfg
    [root@bad test]# mv /tmp/libselinux.so.1 /lib64/  将依赖库文件移回到lib64目录下
    mv: overwrite ‘/lib64/libselinux.so.1’? y  确定写入
    [root@bad test]# 
    

    cal

    • 查看日历
    [root@bad test]# cal
        October 2019    
    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
    

    date

    • 显示或设置日期与时间
      • -s 以字符串方式设置时间
      • + 格式化输出时间
        • %Y:年 %m:月 %D:日
        • %H:时 %M:分 %S:秒
    [root@bad test]# date
    Thu Oct 31 00:33:30 CST 2019
    [root@bad test]# date '+%Y'  //显示年份
    2019
    [root@bad test]# date '+%Y%m'  //显示年份、月
    201910
    [root@bad test]# date '+%Y%m%d'  //显示年、月、日
    20191031
    [root@bad test]# date '+%Y-%m-%d %H'  //显示年、月、日、时
    2019-10-31 00
    [root@bad test]# date '+%Y-%m-%d %H:%M'  //显示年、月、日、时、分
    2019-10-31 00:34
    [root@bad test]# date '+%Y-%m-%d %H:%M:%S'  //显示年、月、日、时、分、秒
    2019-10-31 00:35:00
    [root@bad test]# date  显示当前时间
    Thu Oct 31 00:37:43 CST 2019
    [root@bad test]# date -s 20191030  //修改系统时间为2019年10月30日
    Wed Oct 30 00:00:00 CST 2019
    [root@bad test]#
    

    who

    • 查看当前用户是谁
    [root@bad test]# whoami  //查看当前登录的用户是谁
    root
    [root@bad test]# who am i  //查看当前登陆的用户的详细信息
    jay      pts/0        2019-10-20 15:46 (:0)
    [root@bad test]# who  //显示关于当前在本地系统上的所有用户的信息
    jay      :0           2019-10-20 15:45 (:0)
    jay      pts/0        2019-10-20 15:46 (:0)
    [root@bad test]# w  //显示当前系统所有登录用户的详细信息并显示其在执行什么命令
     00:12:00 up  1:14,  2 users,  load average: 0.25, 0.10, 0.06
    USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
    jay      :0        20Oct19 ?xdm?   3:00   0.09s gdm-session-worker [pam/gdm-aut
    jay      pts/0     20Oct19   ?     0.33s  3.66s /usr/libexec/gnome-terminal-ser
    [root@bad test]# 
    

    df

    • 显示所有磁盘空间占用情况
      • 用法:df [选项]
      • 选项:
        • -a 显示所有
        • -h 以更易读的字节单位显示信息
        • -T 显示文件系统类型
    [root@bad test]# df -h
    Filesystem             Size  Used Avail Use% Mounted on
    /dev/mapper/rhel-root   18G  2.9G   15G  17% /
    devtmpfs               905M     0  905M   0% /dev
    tmpfs                  914M  168K  914M   1% /dev/shm
    tmpfs                  914M  8.9M  905M   1% /run
    tmpfs                  914M     0  914M   0% /sys/fs/cgroup
    /dev/sda1              497M  119M  379M  24% /boot
    [root@bad test]# df -T
    Filesystem            Type     1K-blocks    Used Available Use% Mounted on
    /dev/mapper/rhel-root xfs       18348032 3026140  15321892  17% /
    devtmpfs              devtmpfs    926100       0    926100   0% /dev
    tmpfs                 tmpfs       935380     168    935212   1% /dev/shm
    tmpfs                 tmpfs       935380    9072    926308   1% /run
    tmpfs                 tmpfs       935380       0    935380   0% /sys/fs/cgroup
    /dev/sda1             xfs         508588  121216    387372  24% /boot
    [root@bad test]#
    

    history

    • 查看历史命令,存放路径:/.bash_history
      • 用法:histpry [选项]
      • 选项:
        • -w 将历史命令保存到本地文件中
        • -c 清除历史命令
        • -d 删除指定命令
    [root@bad test]# history -w
    [root@bad test]# cat /root/.bash_history
    a=`ls bad`
    echo $a
    echo `ls bad`
    echo $(ls bad)
    history
    HISTSIZE=10
    history 
    cat /.bash_history
    cat /root/.bash_history
    history -w
    [root@bad test]# history -c
    [root@bad test]# history 
       20  history 
    [root@bad test]# 
    
    • 命令历史的使用技巧:
      • esc,. 按下esc松开后按.,引用前一个命令的最后一个参数
      • !$ 引用前一个命令的最后一个参数
      • !! 执行上一条命令
      • !n 执行命令历史中的第n条命令
      • !string 执行命令历史中最近一个以指定字符串开头的命令
      • !-n 执行命令历史中倒数第n条命令
    [root@bad test]# history 
        7  echo "bad"
        8  echo "jay"
        9  echo "redhat"
       10  history 
    [root@bad test]# !8  //执行第八条命令
    echo "jay"
    jay
    [root@bad test]# history 
        9  echo "redhat"
       10  history 
       11  echo "jay"
       12  history 
    [root@bad test]# !-4  //执行倒数第四条命令
    echo "redhat"
    redhat
    [root@bad test]# cd /etc/yum.repos.d/
    [root@bad yum.repos.d]# ls !$
    ls /etc/yum.repos.d/
    [root@bad yum.repos.d]# echo redhat
    redhat
    [root@bad yum.repos.d]# !!  //执行上一条命令
    echo redhat
    redhat
    [root@bad yum.repos.d]# 
    

    定义变量

    • 环境变量
      • PATH 命令搜索路径
    [root@bad test]# echo $PATH  //指出使用的命令所在的目录
    /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/jay/.local/bin:/home/jay/bin
    [root@bad test]# 
    
    • HISTSIZE 命令历史缓冲区大小
    • SHELL 当前shell
    • PS1 定义着提示符
    [root@bad test]# echo $PS1
    PS1='[u@h w]$ '
    
    • bash内置变量
      • RANDOM 保存着0-32768之间的随机数

    如果有错误或不详尽的地方,还请留言指出。

    本文作者: 坏坏
  • 相关阅读:
    DML-DDL-DCL
    FastDFS常见场景模拟
    如何定义软件版本
    鸟哥的linux私房菜学习-(七)改变文件属性与权限
    鸟哥的linux私房菜学习-(六)Linux 文件权限介绍
    二、基本语法
    一、JavaSE语言概述
    鸟哥的linux私房菜学习-(五)补充:重点回顾
    鸟哥的linux私房菜学习-(五)Linux系统的在线求助man page与info page
    鸟哥的linux私房菜学习-(四)linux命令的基本概念
  • 原文地址:https://www.cnblogs.com/bad5/p/12424300.html
Copyright © 2011-2022 走看看