文章目录
- 命令介绍
- 常用选项
- 命令示例
- (一)创建空文件
- (二)批量创建文件
- (三)修改文件的访问时间
命令介绍
touch 命令有两个功能:一是用于把已存在文件的时间戳更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来;二是用来创建新的空文件。
touch 命令会根据当前的系统时间更新指定文件的访问时间和修改时间。如果文件不存在,将会创建新的空文件,除非指定了”-c”或”-h”选项。
注意:在修改文件的时间属性的时候,用户必须是文件的属主,或拥有写文件的访问权限。
常用选项
选项 | 说明 |
---|---|
-a | 改变档案的读取时间记录 |
-m | 改变档案的修改时间记录 |
-r | 使用参考档的时间记录,与 --file 的效果一样 |
-c | 不创建新文件 |
-d | 设定时间与日期,可以使用各种不同的格式 |
-t | 设定档案的时间记录,格式与 date 命令相同 |
–no-create | 不创建新文件 |
–help | 显示帮助信息 |
–version | 列出版本讯息 |
命令示例
(一)创建空文件
[root@linuxcool ~]# touch file.txt
(二)批量创建文件
[root@linuxcool ~]# touch file{1..5}.txt
[root@linuxcool ~]# ls
file1.txt file2.txt file3.txt file4.txt file5.txt
(三)修改文件的访问时间
[root@linuxcool ~]# stat file.txtFile: file.txtSize: 0 Blocks: 0 IO Block: 512 regular empty fileDevice: 2h/2d Inode: 87538717757135273 Links: 1Access: (0666/-rw-rw-rw-) Uid: ( 1000/ root) Gid: ( 1000/ root)Access: 2019-04-07 00:25:34.729216500 +0800Modify: 2019-04-07 00:25:34.729216500 +0800Change: 2019-04-07 00:25:34.729216500 +0800Birth: -[root@linuxcool ~]# touch -a file.txt[root@linuxcool ~]# stat file.txtFile: file.txtSize: 0 Blocks: 0 IO Block: 512 regular empty fileDevice: 2h/2d Inode: 87538717757135273 Links: 1Access: (0666/-rw-rw-rw-) Uid: ( 1000/ root) Gid: ( 1000/ root)Access: 2019-04-07 00:26:13.004116000 +0800Modify: 2019-04-07 00:25:34.729216500 +0800Change: 2019-04-07 00:26:13.004116000 +0800Birth: -