前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Shell-3-文件之名

Shell-3-文件之名

作者头像
用户1173509
发布2018-01-17 14:49:11
5690
发布2018-01-17 14:49:11
举报
文章被收录于专栏:CaiRuiCaiRui

1.生成任意大小的文件

代码语言:javascript
复制
[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表示块数

代码语言:javascript
复制
[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中,但不包含在其他指定文件中的那些行。

代码语言:javascript
复制
[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.创建不可修改的文件

代码语言:javascript
复制
chattr +i file
chattr -i file

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

4.使用回环文件

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

代码语言:javascript
复制
[root@cai tmp]# dd if=/dev/zero of=looback.img bs=1G count=1

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

代码语言:javascript
复制
[root@cai tmp]# mkfs.ext4 looback.img 

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

代码语言:javascript
复制
file loobackuo.img

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

代码语言:javascript
复制
[root@cai tmp]# mkdir /mnt/looback
[root@cai tmp]# mount -o loop looback.img /mnt/looback/

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

代码语言:javascript
复制
umount /mnt/looback

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

代码语言:javascript
复制
[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)用下列命令来修补

代码语言:javascript
复制
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)下列命令撤销做出的修改

代码语言:javascript
复制
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)统计行数

代码语言:javascript
复制
wc -l file

(2)统计单词数

代码语言:javascript
复制
wc -w file

(3)统计字符数

代码语言:javascript
复制
wc -c file

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.生成任意大小的文件
  • 2.文本文件的交集与差集(comm)
  • 3.创建不可修改的文件
  • 4.使用回环文件
  • 5.查找文件差异并进行修补(diff)
  • 6.只列出目录的各种办法
  • 7.统计文件的行数、单词数和字符数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档