一、常規(guī)方式,touch
使用touch
命令來創(chuàng)建一個空文件,或者多個文件。當(dāng)文件存在時,只會修改文件的訪問和修改時間,不會清空內(nèi)容。
[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
批量創(chuàng)建
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
二、創(chuàng)建單個,vi&vim
使用vi
&vim
編輯器,輸入內(nèi)容并保存退出,來創(chuàng)建一個文件
vi test1 示例1
vim test2 示例2
:wq 保存退出
三、重定向, > &>>
使用重定向符號>
創(chuàng)建一個空文件
[root@centos7 a]#> testb
[root@centos7 a]#ls
testa testb
使用
>
創(chuàng)建文件,需要注意,當(dāng)文件存在時,會清空文件內(nèi)容,并且不會提示。
[root@centos7 a]#cat testa
1
2
3
4
[root@centos7 a]#> testa
[root@centos7 a]#cat testa
[root@centos7 a]#
>>
與>
基本一致,區(qū)別在于>>
是追加,不會清空內(nèi)容。
四、重定向延伸,ll >
將此目錄列出的結(jié)果,重定向到文件中,如果沒有文件,就直接創(chuàng)建。
[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配合重定向>
符號來創(chuàng)建一個空文件。
注意因為echo默認(rèn)帶換行符,創(chuàng)建空文件時需要帶-n選項
創(chuàng)建文件同時還可以輸入內(nèi)容,此時不用加-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
是一個特殊的設(shè)備文件,這個文件接收到任何數(shù)據(jù)都會被丟棄,俗稱“黑洞”文章來源:http://www.zghlxwxcb.cn/news/detail-440190.html
寫入到它的內(nèi)容都會被丟棄,如果嘗試從該文件讀取內(nèi)容,那么什么也讀取不到。文章來源地址http://www.zghlxwxcb.cn/news/detail-440190.html
[root@centos7 a]#cat /dev/null > testa
[root@centos7 a]#ls
testa
[root@centos7 a]#cat testa
到了這里,關(guān)于Linux創(chuàng)建文件的幾種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!