linux 命令详解

本文主要内容来自Linux man 手册

命令名称:

touch 改变文件时间戳(timestamps)

命令用法:

touch [选项]... 文件...            []表示可选参数

命令概述:

更新每个文件的访问时间和修改时间为当前时间。
如果没有使用-c或-h选项,且文件不存在,则touch会创建这些文件。

命令参数:

-a    只改变访问时间-c,--no-create不创建任何文件-d,--data=STRING    解析STRING,并将该时间作为touch要更新的时间,而不是当前时间,STRING格式可以参考date命令-f   忽略(无用参数)-h,--no-dereference 不援引只影响符号链接本身,而不修改链接指向的链接源文件的时间戳(前提是系统支持修改链接文件的时间戳-m  只改变文件的修改时间-r,--referenc=FILE使用这个文件的时间,而不是当前时间,即将其他文件的时间戳改得和这个文件相同-t   STAMP使用[[CC]YY]MMDDhhmm[.ss]而不是当前时间--time=WORD修改特定的时间:WORD为access、atime或use时,等同于-a,为modify或mtime时,等同于-m--help 显示帮助信息--version 显示版本信息

示例:

1. touch 文件…

不带任何参数,修改文件时间戳。如果文件不存在,则创建。

xiaohui@ubuntu:~/work/touch_learn$ touch a.c
xiaohui@ubuntu:~/work/touch_learn$ date && ls -l a.c
2021年 07月 19日 星期一 09:19:36 CST
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 09:19 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c --time=use
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 09:19 a.c
xiaohui@ubuntu:~/work/touch_learn$ date && touch a.c
2021年 07月 19日 星期一 09:20:41 CST
xiaohui@ubuntu:~/work/touch_learn$ ls -l
total 0
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 09:20 a.c
xiaohui@ubuntu:~/work/touch_learn$ 

2. touch -a 文件…

只更新文件的访问时间。

xiaohui@ubuntu:~/work/touch_learn$ ls -l --time=access
total 0
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 09:20 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l --time=ctime
total 0
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 09:20 a.c
xiaohui@ubuntu:~/work/touch_learn$ touch a.c -a
xiaohui@ubuntu:~/work/touch_learn$ ls -l --time=ctime
total 0
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 09:24 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l --time=atime
total 0
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 09:24 a.c

ls --time=ctime显示的文件的变化时间,这个变化是属性的变化(这里是因为时间属性变了),而ls -l显示的是文件内容被修改的时间。

3. touch -c 文件…

同–no-create,如果文件不存在,也不创建该文件

xiaohui@ubuntu:~/work/touch_learn$ ls
a.c
xiaohui@ubuntu:~/work/touch_learn$ touch a.c b.c -c
xiaohui@ubuntu:~/work/touch_learn$ ls
a.c
xiaohui@ubuntu:~/work/touch_learn$

4. touch -d STRING 文件…

同–data=STRING,将STRING作为touch后的时间戳。STRING的格式可以参考date命令。

xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 6月   1 12:01 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 6月   1 12:01 a.c
xiaohui@ubuntu:~/work/touch_learn$ touch -d "2021-7-5 20:20" a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月   5 20:20 a.c
xiaohui@ubuntu:~/work/touch_learn$

5. touch -h 文件…

同–no-dereference,只修改链接文件的时间戳,不影响链接源文件。

xiaohui@ubuntu:~/work/touch_learn$ ls -l
total 4
-rw-rw-r-- 1 xiaohui xiaohui 4 7月   5 20:20 a.c
lrwxrwxrwx 1 xiaohui xiaohui 3 7月  19 16:20 ln.c -> a.c
xiaohui@ubuntu:~/work/touch_learn$ date && touch ln.c -h
2021年 07月 19日 星期一 16:22:58 CST
xiaohui@ubuntu:~/work/touch_learn$ ls -l
total 4
-rw-rw-r-- 1 xiaohui xiaohui 4 7月   5 20:20 a.c
lrwxrwxrwx 1 xiaohui xiaohui 3 7月  19 16:22 ln.c -> a.c
xiaohui@ubuntu:~/work/touch_learn$

6. touch -m 文件…

只改变文件的修改时间。

xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月   5 20:20 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c --time=use
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  19 16:19 a.c
xiaohui@ubuntu:~/work/touch_learn$ touch a.c -m
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c --time=ctime
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  19 16:24 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c --time=use
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  19 16:19 a.c
xiaohui@ubuntu:~/work/touch_learn$ 

7. touch -r FILE 文件…

同–reference=FILE,根据FILE的时间戳来更新其他文件。

xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  19 16:24 a.c
xiaohui@ubuntu:~/work/touch_learn$ touch -r a.c new.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l new.c
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 16:24 new.c
xiaohui@ubuntu:~/work/touch_learn$ touch now.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l now.c
-rw-rw-r-- 1 xiaohui xiaohui 0 7月  19 21:07 now.c
xiaohui@ubuntu:~/work/touch_learn$

8. touch -t STAMP 文件…

使用[[CC]YY]MMDDhhmm[.ss],而不是当前时间,和-d类似,但是-t和-d接受的时间格式是不同的。

xiaohui@ubuntu:~/work/touch_learn$ touch -t 2107100513 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  10 05:13 a.c
xiaohui@ubuntu:~/work/touch_learn$

9. touch --time=TIME 文件…

改变文件的修改时间或访问时间:WORD为access、atime或use时,相当于-a,只修改访问时间;如果WORD为modify或mtime,相当于-m,只更新文件的修改时间。

xiaohui@ubuntu:~/work/touch_learn$ ls -l --time=access a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  10 05:13 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l --time=use a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  10 05:13 a.c
xiaohui@ubuntu:~/work/touch_learn$ date && touch --time=access a.c
2021年 07月 19日 星期一 21:35:19 CST
xiaohui@ubuntu:~/work/touch_learn$ ls -l --time=access a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  19 21:35 a.c
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  10 05:13 a.c
xiaohui@ubuntu:~/work/touch_learn$ date && touch --time=modify a.c
2021年 07月 19日 星期一 21:37:16 CST
xiaohui@ubuntu:~/work/touch_learn$ ls -l a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月  19 21:37 a.c
xiaohui@ubuntu:~/work/touch_learn$ 

man手册

以下为 touch 命令手册原文:

TOUCH(1)                         User Commands                        TOUCH(1)NAMEtouch - change file timestampsSYNOPSIStouch [OPTION]... FILE...DESCRIPTIONUpdate  the  access  and modification times of each FILE to the currenttime.A FILE argument that does not exist is created empty, unless -c  or  -his supplied.A  FILE  argument  string of - is handled specially and causes touch tochange the times of the file associated with standard output.Mandatory arguments to long options are  mandatory  for  short  optionstoo.-a     change only the access time-c, --no-createdo not create any files-d, --date=STRINGparse STRING and use it instead of current time-f     (ignored)-h, --no-dereferenceaffect each symbolic link instead of any referenced file (usefulonly on systems that can change the timestamps of a symlink)-m     change only the modification time-r, --reference=FILEuse this file's times instead of current time-t STAMPuse [[CC]YY]MMDDhhmm[.ss] instead of current time--time=WORDchange the specified time: WORD is access, atime, or use: equiv‐alent to -a WORD is modify or mtime: equivalent to -m--help display this help and exit--versionoutput version information and exitNote that the -d and -t options accept different time-date formats.DATE STRINGThe  --date=STRING  is  a mostly free format human readable date stringsuch as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29  16:21:42"  oreven  "next Thursday".  A date string may contain items indicating cal‐endar date, time of day, time zone, day of week, relative  time,  rela‐tive date, and numbers.  An empty string indicates the beginning of theday.  The date string format is more complex than is easily  documentedhere but is fully described in the info documentation.AUTHORWritten  by  Paul  Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,and Randy Smith.REPORTING BUGSGNU coreutils online help: <http://www.gnu.org/software/coreutils/>Report touch translation bugs to <http://translationproject.org/team/>COPYRIGHTCopyright © 2016 Free Software Foundation, Inc.   License  GPLv3+:  GNUGPL version 3 or later <http://gnu.org/licenses/gpl.html>.This  is  free  software:  you  are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.SEE ALSOFull documentation at: <http://www.gnu.org/software/coreutils/touch>or available locally via: info '(coreutils) touch invocation'GNU coreutils 8.25               February 2017                        TOUCH(1)

Linux命令详解之 touch相关推荐

  1. c linux time微秒_学习linux,看这篇1.5w多字的linux命令详解(6小时讲明白Linux)

    用心分享,共同成长 没有什么比每天进步一点点更重要了 本篇文章主要讲解了一些linux常用命令,主要讲解模式是,命令介绍.命令参数格式.命令参数.命令常用参数示例.由于linux命令较多,我还特意选了 ...

  2. Linux命令详解之 head和tail

    linux 命令详解 本文主要内容来自Linux man 手册 命令名称: head 输出文件的开头部分 tai 输出文件的结尾部分 命令用法: head/tail [选项]... [文件]... [ ...

  3. 《Linux命令详解手册》——Linux畅销书作家又一力作

    关注IT,更要关心IT人,让系统管理员以及程序员工作得更加轻松和快乐.鉴于此, 图灵公司引进了国外知名出版社John Wiley and Sons出版的Fedora Linux Toolbox: 10 ...

  4. linux下载命令 scp,linux命令详解之scp命令

    作用 scp命令常用于linux之间复制文件和目录. scp是secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 格式 从本地复制到远程 复制文件 sc ...

  5. linux中date使用方法,linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例)...

    linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例) 发布于 2016-02-07 15:58:40 | 108 次阅读 | 评论: 0 | 来源: 网友投递 LinuxLinux是一 ...

  6. RAR for Linux 命令详解

    RAR for Linux 命令详解 用法:  rar <命令>-<开关 1> -<开关 N> <压缩文件> <文件...> <@列表 ...

  7. Linux命令详解之 ls

    linux 命令详解 本文主要内容来自Linux man 手册 命令名称: ls ( list files / list directory contents )列举目录内容 命令用法: ls [选项 ...

  8. Linux命令详解之 mv

    linux 命令详解 本文主要内容来自Linux man 手册 命令名称: mv(move)移动/重命名文件 命令用法: mv [选项]... [-T] 源文件 目标文件 mv [选项]... 源文件 ...

  9. Linux命令详解之w命令

    Linux命令详解之w命令 1.命令详解 ··· NAMEw - Show who is logged on and what they are doing. w命令就是用来展示谁在登录,以及他们在做 ...

最新文章

  1. CSS 和 JS 动画哪个更快
  2. 转载:CSS hack技巧大全
  3. 使用POI读取excel文件内容
  4. 电脑常见故障处理_彩超常见故障及维修
  5. c语言用数组写密码程序,想程序高手求助--用C语言来编辑一个输入密码的程序...
  6. Qt工作笔记-QFileSystemModel创建文件系统信息模型
  7. 好看的表白墙LoveCards v1.0.4 源码(开源)
  8. 先有本地代码,后有远程仓库
  9. C++--第1课 - C到C++的升级
  10. (原創) 如何Real Time產生灰階影像? (SOC) (DE2-70) (TRDB-D5M)
  11. win10更换微软拼音字体
  12. 常用crc查表法_CRC8查表法
  13. Spigot 算法之一 计算调和级数的和
  14. 做到这3点,你也能成为一个高情商的人
  15. LinkedIn高级分析师王益:大数据时代的理想主义和现实主义(图灵访谈)
  16. 6-7 快速排序 (15 分)
  17. 自定义类 无极限树形结构菜单(繁杂版)
  18. STM32之W5500网络通信
  19. DDR2-DDR3-IP-CARE ---use
  20. iOS - 直播流程,视频推流,视频拉流,简介,SMTP、RTMP、HLS、 PLPlayerKit

热门文章

  1. TIA 博途编程环境下专有技术保护的实现-密码保护与取消
  2. NANDFLASH调试(二)
  3. Android仿微信地图定位和位置选择(下)
  4. 研究人员对特定行为的大脑信号模式进行分离和解码
  5. Python数据分析与挖掘实战——第三章
  6. ios开发中常用的数学函数
  7. 用通俗易懂的方式讲解:TF-IDF算法介绍及实现
  8. MySQL将一张表的数据插入到另一张表
  9. Conda 如何退出base环境
  10. IDEA插件-墨菲安全MurphySec Code Scan