• 文件管理之字符处理命令,打包压缩


    一. 字符处理命令

    1.  sort 排序

    -t 指定分隔符
    -r 倒叙排序
    -kN(N表示数字) 指定列进行排序
    -n 以数值大小进行排序
    [root@localhost ~]# sort file.txt
    a:4
    b:3
    c:2
    d:1
    e:5
    f:11
    ֺ
    [root@localhost ~]# sort -t ":" -n -k2 file.txt
    d:1
    c:2
    b:3
    a:4
    e:5
    f:11
    ֺ
    [root@localhost ~]# sort -t ":" -n -r -k2 file.txt
    f:11
    e:5
    a:4
    b:3
    c:2
    d:1

    2. uniq 去重
    -c 显示该行重复出现的次数
    -d 只显示重复的行
    -u 只显示不重复的行
    [root@localhost ~]# sort file.txt
    123
    123
    func
    hello
    hello
    ֺ
    [root@localhost ~]# sort file.txt | uniq
    123
    func
    hello
    ֺ
    [root@localhost ~]# sort file.txt | uniq -c
    2 123
    1 func
    2 hello
    ֺ
    [root@localhost ~]# sort file.txt | uniq -d
    123
    hello

    3. cut 
    -d 指定分隔符,默认分隔符是TAB
    -f  取指定的那一列
    [root@localhost ~]# head -1 /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    [root@localhost ~]# head -1 /etc/passwd | cut -d ":" -f1,3,4,6
    root:0:0:/root

    4. tr 替换或者删除
    -d 删除字符
    [root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT"
    ROOT:x:0:0:ROOT:/ROOT:/bin/bash
    [root@localhost ~]#
    [root@localhost ~]# head -1 /etc/passwd |tr -d "root"
    :x:0:0::/:/bin/bash

    5. wc 统计
    -l   统计行号
    -c  统计文件大小,字节数
    -w 统计文件中单词的个数,默认以空格为分隔符
    [root@localhost ~]# ll file.txt
    -rw-r--r--. 1 root root 25 8์ 12 20:09 file.txt
    [root@localhost ~]# wc -c file.txt
    25 file.txt

    [root@localhost ~]# cat file.txt
    hello
    123
    hello
    123
    func
    [root@localhost ~]# wc -l file.txt
    5 file.txt
    [root@localhost ~]# grep "hello" file.txt |wc -l
    2

    [root@localhost ~]# cat file.txt
    hello
    123
    hello
    123
    func
    [root@localhost ~]# wc -w file.txt
    5 file.txt


    二. 打包压缩
    windows下我们接触最多的压缩文件就是zip, rar格式, Linux:zip , tar.gz , tar , bz2.gz
    如果希望windows和Linux互相能使用的压缩工具, 建议zip格式.

    压缩打包的优点:
    1.节省磁盘空间占用率
    2.节省网络传输带宽消耗
    3.加快网络的传输

    格式       压缩工具
    .zip         zip压缩工具
    .gz          gzip压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)
    .bz2        bzip2压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)
    .tar.gz     先使用tar命令归档打包,然后使用gzip压缩
    .tar.bz2   先使用tar命令归档打包,然后使用bzip压缩

    1. gzip压缩工具
    gzip打包会删除源文件
    #压缩
    [root@oldboyedu ~]# gzip file1

    #解压
    [root@oldboyedu ~]# gzip -d file1.gz

    #不解压,查看文件内容
    [root@oldboyedu ~]# zcat file1.gz
    123
    ps:
    gzip 不可以压缩目录,只能压缩文件
    gzip压缩的文件不在当前目录下,而是在源文件所在的目录下
    压缩会删除源文件
    解压会删除压缩文件
     
     
    2. zip压缩工具
    -r:递归压缩
    压缩
    [root@oldboyedu ~]# zip zls.zip file1

    解压
    [root@oldboyedu ~]# unzip zls.zip
    Archive: zls.zip
    replace file1? [y]es, [n]o,      [A]ll,      [N]one, [r]ename:
          替换 不替换 替换所有 啥也不做 改名
    压缩目录
    [root@oldboyedu tmp]# zip -r tmp_dir2.zip /tmp/
    ps:
    压缩不会删除源文件
    解压不会删除压缩文件
    解压开会覆盖源文件内容
     
    3. tar压缩工具
    c //创建新的归档文件
    x //对归档文件解包
    t //列出归档文件里的文件列表
    v //输出命令的归档或解包的过程
    f //指定包文件名,多参数f写最后
    C //指定解压目录位置
    z //使用gzip压缩归档后的文件(.tar.gz)
    j //使用bzip2压缩归档后的文件(.tar.bz2)
    J //使用xz压缩归档后的文件(tar.xz)
    X //排除多个文件(写入需要排除的文件名称)
    h //打包软链接
    P //连带绝对路径打包
    --hard-dereference //打包硬链接
    --exclude //在打包的时候写入需要排除文件或目录
     
    #打包压缩
    [root@oldboyedu ~]# tar zcf zls2.tar.gz /etc
     
    #不解压查看打包内容
    [root@oldboyedu ~]# tar tf zls1.tar.gz
     
    #解压
    [root@oldboyedu ~]# tar xf zls1.tar.gz
     
    ps:
    (1).如果要打包绝对路径,加上参数P:[root@oldboyedu ~]# tar zcfP b.sh.tar.gz /usr/local/nginx/a.sh
    解压的时候:[root@oldboyedu ~]# tar xfP b.sh.tar.gz
    (2).tar 打包可以接多个文件
    [root@oldboyedu ~]# tar zcf abc1.tar.gz 1.txt 2.txt 3.txt
    (3).排除单个文件
    [root@oldboyedu /]# tar zcf etc3.tar.gz --exclude=排除的文件名 etc
    (4).排除多个文件,可以先把多个文件路径写入一个文件中,然后使用参数X
    [root@oldboyedu /]# tar zcfX etc4.tar.gz a.txt(里面内容是要排除的文件路径) etc
    [root@oldboyedu /]# cat a.txt
    etc/latrace.d/headers/
    etc/latrace.d/headers/ctype.h
    etc/latrace.d/headers/dirent.h
    (5).指定解压路径
    [root@oldboyedu ~]# tar xf abc1.tar.gz -C /opt/
     
    4.拓展
    打包压缩通常用于备份文件,文件的名字一般应该带上时间,主机名之类.
    [root@localhost ~]# tar czvf `date +%F`_bak.tar.gz /etc
    [root@localhost ~]# tar czvf `date +%F_%H_%M_%S`_bak.tar.gz /etc 
    如果带有时分秒,注意不要用冒号分隔,因为文件名的命名里不能有冒号

    时间的命令
    date
    -d 根据你的描述显示时间
    -s 修改时间
    %H 小时,24小时制
    %M 分钟
    %s 从1970年1月1日00:00:00到目前经历的秒数
    %S 显示秒
    %T 显示时间.24小时制(21:07:25)
    %d 一个月的第几天,即几号
    %j 一年的第几天
    %m 月份
    %w 一个星期的第几天,即星期几(0代表星期天)
    %W 一年的第几个星期
    %y 年的最后两个数字(1999则为99)
    %Y 年,实际
    %F 显示日期
     
    [root@localhost ~]# date +%F
    2020-08-12
    [root@localhost ~]# date +%Y-%m-%d
    2020-08-12
    [root@localhost ~]# date +%y-%m-%d
    20-08-12
    [root@localhost ~]# date +%T
    00:01:03
    [root@localhost ~]# date +%H:%M:%S
    00:01:11
    [root@localhost ~]# date +%w
    3
    [root@localhost ~]# date +%s
    1597236988
    [root@localhost ~]# date +%d
    12
    [root@localhost ~]# date +%W
    32
    [root@localhost ~]# date +%j
    225
    [root@localhost ~]# date -d "-1 day" +%F
    2020-08-11
    [root@localhost ~]# date -d "1 day" +%F
    2020-08-13
    [root@localhost ~]# date -d "+1 day" +%F
    2020-08-13
    [root@localhost ~]# date -d "3 years" +%F
    2023-08-12
    [root@localhost ~]# date -d "+3 years" +%F
    2023-08-12
    [root@localhost ~]# date -d "+3 hours" +%F_%H:%M:%S
    2020-08-12_23:58:06
     

  • 相关阅读:
    js与asp.net后台交互
    Asp.net封装js的类
    RegisterClientScriptBlock 与 RegisterStartupScript 的区别
    Page.ClientScript.RegisterStartupScript()
    错误与修复:ASP.NET无法检测IE10,导致_doPostBack未定义JavaScript错误,恒处于FF5卷动条位置
    JS数组的操作
    拉里·埃里森和历史上最牛的演讲【转】
    SSIS 学习(9):包部署常见问题汇总【转】
    SSIS 学习(8):事务【转】
    SSIS 学习(7):包配置(下)【转】
  • 原文地址:https://www.cnblogs.com/allenzhu128/p/13887576.html
Copyright © 2020-2023  润新知