zoukankan      html  css  js  c++  java
  • Shell-3-文件之名

    1.生成任意大小的文件

    [root@localhost tmp]# dd if=/dev/zero of=junk.data bs=1M count=1

    记录了1+0 的读入

    记录了1+0 的写出

    1048576字节(1.0 MB)已复制,0.00219263 秒,478 MB/

    if代表输入文件,of代表输出文件,bs大小,count表示块数

    [root@localhost tmp]# dd if=/dev/zero of=junk.data bs=1M count=2

    记录了2+0 的读入

    记录了2+0 的写出

    2097152字节(2.1 MB)已复制,0.00375177 秒,559 MB/

    单元大小

    代码

    字节(1B

    c

    字(2B

    w

    块(512B

    b

    千字节(1024B

    k

    兆字节(1024kb

    M

    吉字节(1024MB

    G


    2.文本文件的交集与差集(comm)

    交集:打印出两个文件所共有的行。

    求差:打印出指定文件所包含的且互不相同的那些行。

    差集:打印出包含在文件A中,但不包含在其他指定文件中的那些行。

    [root@localhost tmp]# cat A.txt
    apple
    orange
    gold
    silver
    steel
    iron
    [root@localhost tmp]# cat B.txt
    orange
    gold
    cookies
    carrot
    [root@localhost tmp]# sort A.txt -o A.txt ;sort B.txt -o B.txt 
    [root@localhost tmp]# comm A.txt B.txt 
        
    apple
        carrot
        cookies
            gold
    iron
            orange
    silver
    steel
    为了打印交集,删除第1,2列:
    [root@localhost tmp]# comm A.txt B.txt -1 -2
    gold
    orange

    3.创建不可修改的文件

    chattr +i file
    chattr -i file
    
    [root@localhost tmp]# for name in {1..100}.txt
    > do 
    > touch $name
    > done

    4.使用回环文件

    (1)创建一个1G大小的文件

    [root@cai tmp]# dd if=/dev/zero of=looback.img bs=1G count=1

    (2)用mkfs命令将1G文件格式化成ext4文件系统

    [root@cai tmp]# mkfs.ext4 looback.img 

    (3)使用下列命令检查文件系统

    file loobackuo.img

    (4)现在可以挂载环回文件

    [root@cai tmp]# mkdir /mnt/looback
    [root@cai tmp]# mount -o loop looback.img /mnt/looback/

    (5)使用下面方法卸载(umount)

    umount /mnt/looback

    5.查找文件差异并进行修补(diff)

    [root@cai tmp]# cat 1.txt 
    this is a test1
    11
    22
    33
    44
    55
    [root@cai tmp]# cat 2.txt 
    this is a test2
    11
    44
    33
    55
    55
    [root@cai tmp]# diff 1.txt 2.txt 
    1c1
    < this is a test1
    ---
    > this is a test2
    3,4d2
    < 22
    < 33
    5a4,5
    > 33
    > 55
    [root@cai tmp]# diff -u 1.txt 2.txt 
    --- 1.txt    2017-06-11 14:51:18.763717808 +0800
    +++ 2.txt    2017-06-11 14:51:47.477782113 +0800
    @@ -1,6 +1,6 @@
    -this is a test1
    +this is a test2
     11
    -22
    -33
     44
    +33
    +55
     55

    (2)用下列命令来修补

    diff -u 1.txt 2.txt >3.txt
    patch -p1 1.txt <3.txt
    [root@cai tmp]# cat 1.txt(和2.txt一模一样) 
    this is a test2
    11
    44
    33
    55
    55

    (3)下列命令撤销做出的修改

    patch -p1 1.txt <version.patch

    6.只列出目录的各种办法

    (1)ls -d */

    (2)ls -F |grep “/$”

    (3)ls -l |grep “^d”

    (4)find . -type d -maxdepth 1

    7.统计文件的行数、单词数和字符数

    wc命令(word count单词统计)

    (1)统计行数

    wc -l file

    (2)统计单词数

    wc -w file

    (3)统计字符数

    wc -c file

    4)当wc不使用任何参数时,分别打印出行数,单词数,字符数。

  • 相关阅读:
    idea搭建Spring Boot+Mybatis及使用教程
    CentOS 6 安装配置JDK+tomcat环境
    关于无线路由器之间的连接
    关于链表的面试问题(判断一个单链表中是否有环)
    华为机试ACM(字符组合问题)
    Activity的启动模式
    Notification状态栏显示信息
    Java注释Override、Deprecated、SuppressWarnings
    Fragment
    Service
  • 原文地址:https://www.cnblogs.com/caicairui/p/7284390.html
Copyright © 2011-2022 走看看