Linux命令 - 覆盖 > 和 追加 >>

Linux命令 - 覆盖 > 和 追加 >>

1.语法:

将列表的内容覆盖到文件中:ll >文件

将列表的内容追加到文件尾部:ll >>文件

将文件1的内容覆盖到文件2中:cat 文件1 > 文件2

将文件1的内容追加到文件2的尾部:cat 文件1 >> 文件2

将内容写入覆盖到文件中:echo “内容” > 文件

将内容写入追加到文件的尾部: echo “内容” >> 文件

2.功能:

指令 > : 如果文件存在,将原来文件的内容覆盖;原文件不存在则创建文件,再添加信息。

指令 >> : 不会覆盖原文件内容,将内容追加到文件的尾部。

[root@localhost test]# ll
总用量 28
-rw-r--r--. 2 root root  96 518 10:15 color.sh
lrwxrwxrwx. 1 root root   8 518 14:57 linkcolor -> color.sh
-rw-r--r--. 2 root root  96 518 10:15 lncolor
-rw-r--r--. 1 root root 156 514 17:00 log1.txt
-rw-r--r--. 1 root root 592 514 17:00 log2.txt
-rw-r--r--. 1 root root 655 514 17:06 log3.txt
-rw-r--r--. 1 root root 156 518 14:32 log4.txt
-rw-r--r--. 1 root root 272 518 14:28 log.txt
[root@localhost test]# rm -rf color.sh 
[root@localhost test]# ll
总用量 24
lrwxrwxrwx. 1 root root   8 518 14:57 linkcolor -> color.sh
-rw-r--r--. 1 root root  96 518 10:15 lncolor
-rw-r--r--. 1 root root 156 514 17:00 log1.txt
-rw-r--r--. 1 root root 592 514 17:00 log2.txt
-rw-r--r--. 1 root root 655 514 17:06 log3.txt
-rw-r--r--. 1 root root 156 518 14:32 log4.txt
-rw-r--r--. 1 root root 272 518 14:28 log.txt
[root@localhost test]# touch color.sh
[root@localhost test]# ll
总用量 24
-rw-r--r--. 1 root root   0 518 15:15 color.sh
lrwxrwxrwx. 1 root root   8 518 14:57 linkcolor -> color.sh
-rw-r--r--. 1 root root  96 518 10:15 lncolor
-rw-r--r--. 1 root root 156 514 17:00 log1.txt
-rw-r--r--. 1 root root 592 514 17:00 log2.txt
-rw-r--r--. 1 root root 655 514 17:06 log3.txt
-rw-r--r--. 1 root root 156 518 14:32 log4.txt
-rw-r--r--. 1 root root 272 518 14:28 log.txt
[root@localhost test]# cat linkcolor 
[root@localhost test]# cat lncolor 
echo -e "\033[35mHello World\033[0m"
echo -e "\033[46;37mHello World\033[0m"
echo "Hello World"

3.常用范例:

例一:将ls查看信息写入到文件中

命令:ll > log.txt

[root@localhost test]# cat log.txt 
Hello World
Hello World2
[root@localhost test]# ll
总用量 20
-rw-r--r--. 1 root root  96 518 10:15 color.sh
-rw-r--r--. 1 root root 156 514 17:00 log1.txt
-rw-r--r--. 1 root root 592 514 17:00 log2.txt
-rw-r--r--. 1 root root 655 514 17:06 log3.txt
-rw-r--r--. 1 root root  25 518 10:22 log.txt
[root@localhost test]# ll > log.txt 
[root@localhost test]# cat log.txt 
总用量 16
-rw-r--r--. 1 root root  96 518 10:15 color.sh
-rw-r--r--. 1 root root 156 514 17:00 log1.txt
-rw-r--r--. 1 root root 592 514 17:00 log2.txt
-rw-r--r--. 1 root root 655 514 17:06 log3.txt
-rw-r--r--. 1 root root   0 518 14:28 log.txt

例二:将log1.txt的内容覆盖到log4.txt中

命令:cat log1.txt > log4.txt

没有log4.txt文件则会自动创建。

[root@localhost test]# cat log1.txt 
Thu Feb 25 09:46:34 2021
Create Relation ADR_CONTROL
Create Relation ADR_INVALIDATION
Create Relation INC_METER_IMPT_DEF
Create Relation INC_METER_PK_IMPTS
[root@localhost test]# ll
总用量 20
-rw-r--r--. 1 root root  96 518 10:15 color.sh
-rw-r--r--. 1 root root 156 514 17:00 log1.txt
-rw-r--r--. 1 root root 592 514 17:00 log2.txt
-rw-r--r--. 1 root root 655 514 17:06 log3.txt
-rw-r--r--. 1 root root 272 518 14:28 log.txt
[root@localhost test]# cat log1.txt > log4.txt
[root@localhost test]# ll
总用量 24
-rw-r--r--. 1 root root  96 518 10:15 color.sh
-rw-r--r--. 1 root root 156 514 17:00 log1.txt
-rw-r--r--. 1 root root 592 514 17:00 log2.txt
-rw-r--r--. 1 root root 655 514 17:06 log3.txt
-rw-r--r--. 1 root root 156 518 14:32 log4.txt
-rw-r--r--. 1 root root 272 518 14:28 log.txt
[root@localhost test]# cat log4.txt 
Thu Feb 25 09:46:34 2021
Create Relation ADR_CONTROL
Create Relation ADR_INVALIDATION
Create Relation INC_METER_IMPT_DEF
Create Relation INC_METER_PK_IMPTS

例三:在log.txt追加内容

命令:echo Hello World2 >> log.txt

[root@localhost test]# cat log.txt 
Hello World
[root@localhost test]# echo Hello World2 >> log.txt 
[root@localhost test]# cat log.txt 
Hello World
Hello World2

————————————————
版权声明:本文为CSDN博主「EstherLty」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_45988641/article/details/116987515

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值