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,如需转载请自行联系原作者






相关文章
|
8天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
50 23
Linux系统之whereis命令的基本使用
|
22天前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
34 11
|
2月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
143 8
|
2月前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
213 20
|
2月前
|
网络协议 Linux 应用服务中间件
kali的常用命令汇总Linux
kali的常用命令汇总linux
129 7
|
2月前
|
Linux Shell
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
101 14
Linux 10 个“who”命令示例
|
3月前
|
监控 网络协议 Linux
Linux netstat 命令详解
Linux netstat 命令详解
|
3月前
|
Linux 数据库
Linux中第一次使用locate命令报错?????
在Linux CentOS7系统中,使用`locate`命令时出现“command not found”错误,原因是缺少`mlocate`包。解决方法是通过`yum install mlocate -y`或`apt-get install mlocate`安装该包,并执行`updatedb`更新数据库以解决后续的“can not stat”错误。
62 9
|
3月前
|
运维 监控 网络协议
运维工程师日常工作中最常用的20个Linux命令,涵盖文件操作、目录管理、权限设置、系统监控等方面
本文介绍了运维工程师日常工作中最常用的20个Linux命令,涵盖文件操作、目录管理、权限设置、系统监控等方面,旨在帮助读者提高工作效率。从基本的文件查看与编辑,到高级的网络配置与安全管理,这些命令是运维工作中的必备工具。
297 3
|
3月前
|
Linux
在 Linux 系统中,`find` 命令
在 Linux 系统中,`find` 命令
57 1