1. 程式人生 > >Shell-3-文件之名

Shell-3-文件之名

roo count 差集 class 表示 /dev/zero tex font 1.0

1.生成任意大小的文件

[[email protected] 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表示塊數

[[email protected] 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中,但不包含在其他指定文件中的那些行。

[[email protected] tmp]# cat A.txt
apple
orange
gold
silver
steel
iron
[[email protected]
/* */ tmp]# cat B.txt orange gold cookies carrot [[email protected] tmp]# sort A.txt -o A.txt ;sort B.txt -o B.txt [[email protected] tmp]# comm A.txt B.txt apple carrot cookies gold iron orange silver steel 為了打印交集,刪除第1,2列: [[email protected] tmp]# comm A.txt B.txt
-1 -2 gold orange

3.創建不可修改的文件

chattr +i file
chattr -i file

[[email protected] tmp]# for name in {1..100}.txt
> do 
> touch $name
> done

4.使用回環文件

(1)創建一個1G大小的文件

[[email protected] tmp]# dd if=/dev/zero of=looback.img bs=1G count=1

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

[[email protected] tmp]# mkfs.ext4 looback.img 

(3)使用下列命令檢查文件系統

file loobackuo.img

(4)現在可以掛載環回文件

[[email protected] tmp]# mkdir /mnt/looback
[[email protected] tmp]# mount -o loop looback.img /mnt/looback/

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

umount /mnt/looback

5.查找文件差異並進行修補(diff)

[[email protected] tmp]# cat 1.txt 
this is a test1
11
22
33
44
55
[[email protected] tmp]# cat 2.txt 
this is a test2
11
44
33
55
55
[[email protected] tmp]# diff 1.txt 2.txt 
1c1
< this is a test1
---
> this is a test2
3,4d2
< 22
< 33
5a4,5
> 33
> 55
[[email protected] 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
[[email protected] 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不使用任何參數時,分別打印出行數,單詞數,字符數。

Shell-3-文件之名