Linux创建文件的几种方式

一、常规方式,touch


使用touch命令来创建一个空文件,或者多个文件。当文件存在时,只会修改文件的访问和修改时间,不会清空内容。

[root@centos7 a]#touch test01
[root@centos7 a]#ls
test01
[root@centos7 a]#touch test02 test03 test04
[root@centos7 a]#ls
test01  test02  test03  test0

批量创建

root@centos7 a]#touch test{001..050}
[root@centos7 a]#ls
test001  test007  test012  test018  test023  test029  test034  test04   test045
test002  test008  test013  test019  test024  test03   test035  test040  test046
test003  test009  test014  test02   test025  test030  test036  test041  test047
test004  test01   test015  test020  test026  test031  test037  test042  test048
test005  test010  test016  test021  test027  test032  test038  test043  test049
test006  test011  test017  test022  test028  test033  test039  test044  test050

[root@centos7 a]#touch test{a..d}
[root@centos7 a]#ls
testa  testb  testc  testd

二、创建单个,vi&vim

使用vi&vim编辑器,输入内容并保存退出,来创建一个文件

vi test1   示例1
vim test2  示例2

:wq 保存退出


三、重定向, > &>>

使用重定向符号>创建一个空文件

[root@centos7 a]#> testb
[root@centos7 a]#ls
testa  testb

使用>创建文件,需要注意,当文件存在时,会清空文件内容,并且不会提示。

[root@centos7 a]#cat testa
1
2
3
4
[root@centos7 a]#> testa
[root@centos7 a]#cat testa
[root@centos7 a]#

>>>基本一致,区别在于>>是追加,不会清空内容。


四、重定向延伸,ll >

将此目录列出的结果,重定向到文件中,如果没有文件,就直接创建。

[root@centos7 a]#ll > testd
[root@centos7 a]#ll
total 8
-rw-r--r--. 1 root root   8 Feb 23 17:38 testa
-rw-r--r--. 1 root root   0 Feb 23 17:35 testb
-rw-r--r--. 1 root root   0 Feb 23 17:40 testc
-rw-r--r--. 1 root root 188 Feb 23 17:47 testd

[root@centos7 a]#cat testd
total 8
-rw-r--r--. 1 root root 8 Feb 23 17:38 testa
-rw-r--r--. 1 root root 0 Feb 23 17:35 testb
-rw-r--r--. 1 root root 0 Feb 23 17:40 testc
-rw-r--r--. 1 root root 0 Feb 23 17:47 testd
                      

五、重定向延伸,echo >

使用echo配合重定向>符号来创建一个空文件。

注意因为echo默认带换行符创建空文件时需要带-n选项

创建文件同时还可以输入内容,此时不用加-n。

[root@centos7 a]#echo "" >test1
[root@centos7 a]#ls
test1
[root@centos7 a]#cat test1

[root@centos7 a]#
[root@centos7 a]#echo -n "" > testb
[root@centos7 a]#ls
test1  testb
[root@centos7 a]#cat testb

[root@centos7 a]#echo  "hello" > testb
[root@centos7 a]#cat testb
hello


六、重定向延伸,cat >

/dev/null 是一个特殊的设备文件,这个文件接收到任何数据都会被丢弃,俗称“黑洞”

写入到它的内容都会被丢弃,如果尝试从该文件读取内容,那么什么也读取不到。

[root@centos7 a]#cat /dev/null > testa
[root@centos7 a]#ls
testa
[root@centos7 a]#cat testa

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值