前言
- 標題:linux中 > 、>> 的用法
由於標題中不能有特殊符號>
,標題中的> 、>>
發佈後就被變爲了」 、」」
linux中>表示覆蓋原文件內容(文件的日期也會自動更新),>>表示追加內容(會另起一行,文件的日期也會自動更新)。
1、將history命令執行的結果保存到history.log文件中
[root@gxzs-solr1 ~]# history > history.log (history.log 文件 會自動生成)
[root@gxzs-solr1 ~]# cat history.log
2、執行命令 curl ‘xxx’ ,將其返回結果保存到 log.log 中
[root@gx-solr1 ~]# curl 'http://192.168.0.110:8983/solr/scan_detail/admin/file?_=1544066402749&contentType=text/plain;charset=utf-8&file=managed-schema&wt=json' > log.log
3、執行命令 cat /etc/hosts , 將其返回結果保存到 hosts.log 中
[root@slave1 ~]# cat /etc/hosts > hosts.log
[root@slave1 ~]# more hosts.log
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
注意:使用 > ,執行命令時,每次都會新生成一個 > 後面的文件,將之前生成的文件替換掉(文件創建時間也會跟着改變)。
4、使用 >> 向 hosts.log中追加 當前日期
[root@slave1 ~]# echo "當前日期是
date
" >> hosts.log
[root@slave1 ~]# ls -l hosts.log
-rw-r--r--. 1 root root 11 Dec 13 16:04 hosts.log # 查看hosts.log 的日期
[root@slave1 ~]# cat hosts.log # 查看hosts.log 文件的內容
I am OK!
[root@slave1 ~]# echo "當前日期是 `date`" >> hosts.log # 向hosts.log中追加 當前日期
[root@slave1 ~]# cat hosts.log # 再次查看hosts.log 文件的內容
I am OK!
當前日期是 Thu Dec 13 16:05:18 CST 2018
[root@slave1 ~]# ls -l hosts.log # 再次查看hosts.log 的日期
-rw-r--r--. 1 root root 56 Dec 13 16:05 hosts.log
[root@slave1 ~]#