Shell-3-文件之名

时间:2022-04-22
本文章向大家介绍Shell-3-文件之名,主要内容包括1.生成任意大小的文件、2.文本文件的交集与差集(comm)、3.创建不可修改的文件、4.使用回环文件、5.查找文件差异并进行修补(diff)、6.只列出目录的各种办法、7.统计文件的行数、单词数和字符数、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

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不使用任何参数时,分别打印出行数,单词数,字符数。