Linux命令(14):touch命令

简介:

 通过touch这个命令,我们可以轻易修改文件的日期与时间,并且也可以创建一个空的文件。不过,要注意的是,即使我们复制一个文件时,复制了所有的属性,但也没有办法复制ctime这个属性的。ctime可以记录这个文件最近的状态(status)被改变的时间。无论如何,还是要告知大家,我们平时看的文件属性中,比较重要的还是属于那个mtime。我们经常关心的是这个文件的“内容”是什么时候被改动过的。

  无论如何,touch这个命令最常被使用的情况是:

创建一个空的文件;

将某个文件日期修改为目前的日期(mtime与atime)。


命令格式:

touch [选项]... 文件...

命令参数:

-a   仅修改访问时间。

-c   仅修改文件的时间,不建立任何文件。

-d  后面可以接欲修改的日期而不用当前的日期,可以使用各种不同的格式。

-f  不使用,是为了与其他 unix 系统的相容性而保留。

-m   只更改变动时间mtime。

-r  把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。

-t  使用指定的日期时间,而非现在的时间,格式与 date 指令相同。

命令示例:

1.将123.txt文件,日期调整为前二天。当前日期2016.4.22

1
2
3
4
5
6
7
8
[root@w zdw] # ll
total 4
-rw-r--r--. 1 root root 4 Apr 22 16:49 123.txt           22日
[root@w zdw] # touch -d "2 days ago" 123.txt
[root@w zdw] # ll 123.txt; ll --time=atime 123.txt; ll --time=ctime 123.txt
-rw-r--r--. 1 root root 4 Apr 20 16:49 123.txt
-rw-r--r--. 1 root root 4 Apr 20 16:49 123.txt
-rw-r--r--. 1 root root 4 Apr 22 16:49 123.txt

上面的示例,本来是22日的变成了20日(atime/mtime)

不过,ctime并没有跟着改变。

2.将123.txt日期改为2016/4/10 02:03

1
2
3
4
5
[root@w zdw] # touch -t 1604100203 123.txt                                      
[root@w zdw] # ll 123.txt; ll --time=atime 123.txt; ll --time=ctime 123.txt
-rw-r--r--. 1 root root 4 Apr 10 02:03 123.txt
-rw-r--r--. 1 root root 4 Apr 10 02:03 123.txt
-rw-r--r--. 1 root root 4 Apr 22 16:57 123.txt

日期atime与mtime都改变了,但是ctime则是记录目前的时间。

3.-c参数,如果文件不存在,则不创建

1
2
3
4
[root@w zdw] # touch -c 456.txt
[root@w zdw] # ll
total 4
-rw-r--r--. 1 root root 4 Apr 10 02:03 123.txt

4.-r参数,更新123.txt时间和456.txt时间戳相同,以123.txt时间为准

1
2
3
4
5
6
7
8
9
10
[root@w zdw] # touch 456.txt
[root@w zdw] # ll
total 4
-rw-r--r--. 1 root root 4 Apr 10 02:03 123.txt
-rw-r--r--. 1 root root 0 Apr 22 17:03 456.txt
[root@w zdw] # touch -r 123.txt 456.txt
[root@w zdw] # ll
total 4
-rw-r--r--. 1 root root 4 Apr 10 02:03 123.txt
-rw-r--r--. 1 root root 0 Apr 10 02:03 456.txt





      本文转自cix123  51CTO博客,原文链接:http://blog.51cto.com/zhaodongwei/1871243,如需转载请自行联系原作者






相关文章
|
1天前
|
安全 网络协议 Linux
linux必学的60个命令
Linux是一个功能强大的操作系统,提供了许多常用的命令行工具,用于管理文件、目录、进程、网络和系统配置等。以下是Linux必学的60个命令的概览,但请注意,这里可能无法列出所有命令的完整语法和选项,仅作为参考
160 2
|
1天前
|
Linux 程序员 计算机视觉
【linux 学习】在Linux中经常用到的cmake、make、make install等命令解析
【linux 学习】在Linux中经常用到的cmake、make、make install等命令解析
12 0
|
1天前
|
Linux
Linux的find命令使用
【5月更文挑战第11天】Linux的find命令使用
12 3
|
1天前
|
监控 Linux 数据处理
|
1天前
|
编解码 Ubuntu Linux
|
1天前
|
JSON Linux 数据格式
Linux命令发送http
请注意,`curl`命令非常灵活,可以根据您的需求进行多种配置和自定义。您可以查看 `curl`命令的文档以获取更多详细信息。
11 0
|
1天前
|
安全 Linux 测试技术
|
1天前
|
安全 Linux Windows
Linux中Shutdown命令使用介绍
Linux中Shutdown命令使用介绍
12 2
|
1天前
|
缓存 关系型数据库 Linux
Linux目录结构:深入理解与命令创建指南
Linux目录结构:深入理解与命令创建指南
|
1天前
|
数据挖掘 Linux vr&ar
Linux命令实战:解决日常问题的利器
Linux命令实战:解决日常问题的利器