Linux shell编程学习笔记48:touch命令

发布于:2024-05-03 ⋅ 阅读:(25) ⋅ 点赞:(0)

0 前言

touch是csdn技能树Linux基础练习题中最常见的一条命令,这次我们就来研究它的功能和用法。

1. touch命令的功能、格式和选项说明

我们可以使用命令 touch --help 来查看touch命令的帮助信息。

purpleEndurer @ bash ~ $ touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note that the -d and -t options accept different time-date formats.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report touch translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'touch invocation'

1.1 touch命令的功能

 touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间。

如果指定的文件或目录不存在,则可以创建同名的空文件(取决于命令指定的选项)。

注意:这里的文件或目录,可以是一个文件或目录,也可以是一个文件集或目录集。

文件集的格式: 文件1 文件2 ……

目录集的格式: 目录1 目录2……

1.2 touch命令的格式

touch [选项]... 文件或目录...

 1.3 touch命令的选项说明

选项 说明
-a 改变文件或目录的读取时间记录

-c

--no-create

如果指定的文件或目录不存在,不创建同名的空文件

-d

--date=STRING

解析 STRING 并使用它来代替当前时间
-f 可以忽略,是为了与其他 unix 系统的兼容性而保留

-h

--no-dereference

影响每个符号链接,而不是任何引用的文件

(仅在可以更改符号链接时间戳的系统上有用)

-m 改变文件或目录的修改时间记录

-r

--reference=FILE

使用指定文件的时间记录
-t STAMP 设定文件或目录的时间记录,格式是[[CC]YY]MMDDhhmm[.ss]
--time=WORD

更改指定时间:

WORD 是 access、atime 或 use:等价于 -a

WORD 是 modify 或 mtime:相当于 -m

--help 显示帮助信息
--version 显示版本信息

2 touch命令使用实例

2.1 touch 命令 创建空文件

purpleEndurer @ bash ~/test $ ls -l
total 0
purpleEndurer @ bash ~/test $ touch 1.txt
purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:26 1.txt
purpleEndurer @ bash ~/test $ 

我们先使用ls -l 命令 查看~/test 下没有文件

然后我们使用 touch 1.txt 修改文件1.txt的时间属性,由于文件1.txt不存在,于是创建了名为1.txt的文件。

我们再用使用ls -l 命令 查看 可以看到文件 1.txt。

2.2 touch 命令更新指定文件时间属性,如果指定文件不存在时不创建同名的空文件。

purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:26 1.txt
purpleEndurer @ bash ~/test $ touch -c 1.txt 2.txt
purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:41 1.txt
purpleEndurer @ bash ~/test $ 

先用 ls -l 命令查看,当前目录下只有文件1.txt

执行命令 touch -c 1.txt 2.txt 后 我们再用 ls -l 命令查看

可以看到 文件1.txt的时间属性已更新,而名为2.txt的文件原选并不存在,也没有被touch命令创建,这是因为我信指点定了 -c 选项。

2.3 用指定的时间更新指定文件的时间属性,指定文件不存在时则创建

purpleEndurer @ bash ~/test $ touch -d "2020-02-20 12:20" 2.txt
purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:59 1.txt
-rw-rw-r-- 1 csdn csdn 0 2月  20  2020 2.txt
purpleEndurer @ bash ~/test $ stat 2.txt
  File: ‘2.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 4ch/76d Inode: 1714885     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    csdn)   Gid: ( 1000/    csdn)
Access: 2020-02-20 12:20:00.000000000 +0800
Modify: 2020-02-20 12:20:00.000000000 +0800
Change: 2024-05-02 23:15:34.397217850 +0800
 Birth: -

我们先使用命令touch -d "2020-02-20 12:20" 2.txt 来更新文件2.txt的时间属性,指定的时间为 2020-02-20 12:20

然后我们使用 ls -l命令查看当前目录下的文件信息,可以看到 文件2.txt 的时间属性是我们指定的2月20日。

接着我们使用stat 2.txt来查看2.txt的时间属性。

2.4 使用其它文件的时间属性来更新指定文件的时间属性。

purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:59 1.txt
-rw-rw-r-- 1 csdn csdn 0 2月  20  2020 2.txt
purpleEndurer @ bash ~/test $ touch -r 2.txt 1.txt
purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 2月  20  2020 1.txt
-rw-rw-r-- 1 csdn csdn 0 2月  20  2020 2.txt

我们使用 命令 ls -l 查看当前目录下的文件 1.txt 和 2.txt的信息,两者的时间属性不同。

然后我们使用命令 touch -r 2.txt 1.txt,用文件2.txt的时间属性来更新文件1.txt的时间属性。

接着我们再次使用 命令 ls -l 查看当前目录下的文件 1.txt 和 2.txt的信息,可以看到文件1.txt的时间属性跟文件2.txt相同。


网站公告

今日签到

点亮在社区的每一天
去签到