目录
7. shell命令--touch
功能说明
touch 是 Linux 和其他 Unix-like 操作系统中的一个常用命令,它主要用于更改文件的时间戳,或者如果文件不存在,则创建空文件。
功能说明总结一下:
- 更改文件时间戳:touch 命令可以更改指定文件或目录的访问时间和修改时间。如果不带任何选项使用,它仅将文件的时间戳更新为当前时间。
- 创建空文件:如果指定的文件不存在,touch 命令会创建一个新的空文件。
语法格式
touch [选项] 文件名...
SYNOPSIS
touch [OPTION]... FILE...
选项说明
- -a 或 --time=atime:只更改访问时间。
- -c 或 --no-create:如果文件不存在,则不创建新文件。
- -m 或 --time=mtime:只更改修改时间。
- -r 参考文件 或 --reference=参考文件:使用指定参考文件的时间戳,而不是当前时间。
- -t [[CC]YY]MMDDhhmm[.ss]:使用指定的时间,而不是当前时间。CC 是可选的世纪字段,YY 是年份的后两位,MM 是月份,DD 是日期,hh 是小时(24小时制),mm 是分钟,ss 是秒(可选)。
实践操作
1. 文件本来不存在的情况创建文件
mkdir -p /test/touch #创建测试目录
cd /test/touch #切换到测试目录
touch file1 #创建一个文件 file1
ls #查看一下
touch a.txt b.txt #创建多个文件
ls #查看一下
touch {1..3}.txt #结合这个大括号 {} 有序序列,批量创建文件
ls #查看一下
2. 更改文件的时间戳属性
stat file1 #查看文件时间戳属性
touch -a file1 #参数 -a 更改文件的最后访问时间
stat file1 #查看文件时间戳属性
touch -m file1 #参数 -m 更改文件的最后修改时间
stat file1 #查看文件时间戳属性
touch file2 #创建文件 file2
stat file1 #查看文件 file1 时间戳属性
stat file2 #查看文件 file2 时间戳属性
touch -r file2 file1 #将 file1的时间戳设置为与文件 file2 的相同,注意文件位置
stat file1 #查看文件 file1 时间戳属性
stat file2 #查看文件 file2 时间戳属性
3. 指定时间属性创建/修改文件
ls -lh file1 #查看一下文件 file1 的属性
touch -d 20201010 file1 #使用参数 -d ,将文件1改成指定时间
ls -lh file1 #查看一下文件 file1 的属性
ls -lh a.txt #查看一下文件 a.txt 的属性
touch -r a.txt file1 #使用参数 -r ,将文件 file1 的属性跟文件 a.txt 的一样
ls -lh file1 #查看一下文件 file1 的属性
touch -t 201904161914.50 file1 #使用参数 -t ,让时间变成指定的时间
ls -lh file1 #查看一下文件 file1 的属性
ls -lh --full-time file1 #使用这种方式查看结果
touch `hostname`_`date +%F`.txt #用这种方式,也可创建一些按指定格式的文件
ls -lh *.txt
hostname
date +%F
命令示例:1. 文件本来不存在的情况创建文件
mkdir -p /test/touch
cd /test/touch
touch file1
ls
touch a.txt b.txt
ls
touch {1..3}.txt
ls
输出结果:
[root@MineGi ~]# mkdir -p /test/touch
[root@MineGi ~]# cd /test/touch
[root@MineGi /test/touch]# touch file1
[root@MineGi /test/touch]# ls
file1
[root@MineGi /test/touch]# touch a.txt b.txt
[root@MineGi /test/touch]# ls
a.txt b.txt file1
[root@MineGi /test/touch]# touch {1..3}.txt
[root@MineGi /test/touch]# ls
1.txt 2.txt 3.txt a.txt b.txt file1
[root@MineGi /test/touch]#
命令示例:2. 更改文件的时间戳属性
stat file1
touch -a file1
stat file1
输出结果:
[root@MineGi /test/touch]# stat file1
文件:"file1"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:268644820 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2024-11-23 14:05:04.192699848 +0800
最近更改:2024-11-23 14:05:04.192699848 +0800
最近改动:2024-11-23 14:05:04.192699848 +0800
创建时间:-
[root@MineGi /test/touch]# touch -a file1
[root@MineGi /test/touch]# stat file1
文件:"file1"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:268644820 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2024-11-23 14:05:17.865556729 +0800
最近更改:2024-11-23 14:05:04.192699848 +0800
最近改动:2024-11-23 14:05:17.865556729 +0800
创建时间:-
[root@MineGi /test/touch]#
命令示例:
stat file1
touch -m file1
stat file1
输出结果:
[root@MineGi /test/touch]# stat file1
文件:"file1"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:268644820 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2024-11-23 14:05:17.865556729 +0800
最近更改:2024-11-23 14:05:04.192699848 +0800
最近改动:2024-11-23 14:05:17.865556729 +0800
创建时间:-
[root@MineGi /test/touch]# touch -m file1
[root@MineGi /test/touch]# stat file1
文件:"file1"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:268644820 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2024-11-23 14:05:17.865556729 +0800
最近更改:2024-11-23 14:06:19.792908512 +0800
最近改动:2024-11-23 14:06:19.792908512 +0800
创建时间:-
[root@MineGi /test/touch]#
命令示例:
touch file2
stat file1
stat file2
输出结果:
[root@MineGi /test/touch]# touch file2
[root@MineGi /test/touch]# stat file1
文件:"file1"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:268644820 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2024-11-23 14:05:17.865556729 +0800
最近更改:2024-11-23 14:06:19.792908512 +0800
最近改动:2024-11-23 14:06:19.792908512 +0800
创建时间:-
[root@MineGi /test/touch]# stat file2
文件:"file2"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:268644826 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2024-11-23 14:07:16.167318419 +0800
最近更改:2024-11-23 14:07:16.167318419 +0800
最近改动:2024-11-23 14:07:16.167318419 +0800
创建时间:-
[root@MineGi /test/touch]#
命令示例:
touch -r file2 file1
stat file1
stat file2
输出结果:
[root@MineGi /test/touch]# touch -r file2 file1
[root@MineGi /test/touch]# stat file1
文件:"file1"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:268644820 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2024-11-23 14:07:16.167318419 +0800
最近更改:2024-11-23 14:07:16.167318419 +0800
最近改动:2024-11-23 14:08:03.016828031 +0800
创建时间:-
[root@MineGi /test/touch]# stat file2
文件:"file2"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:268644826 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2024-11-23 14:07:16.167318419 +0800
最近更改:2024-11-23 14:07:16.167318419 +0800
最近改动:2024-11-23 14:07:16.167318419 +0800
创建时间:-
[root@MineGi /test/touch]#
命令示例:3. 指定时间属性创建/修改文件
ls -lh file1
touch -d 20201010 file1
ls -lh file1
ls -lh a.txt
touch -r a.txt file1
ls -lh file1
touch -t 201904161914.50 file1
ls -lh file1
ls -lh --full-time file1
touch `hostname`_`date +%F`.txt
ls -lh `hostname`_`date +%F`.txt
hostname
date +%F
输出结果:
[root@MineGi /test/touch]# ls -lh file1
-rw-r--r-- 1 root root 0 11月 23 14:07 file1
[root@MineGi /test/touch]# touch -d 20201010 file1
[root@MineGi /test/touch]# ls -lh file1
-rw-r--r-- 1 root root 0 10月 10 2020 file1
[root@MineGi /test/touch]# ls -lh a.txt
-rw-r--r-- 1 root root 0 11月 23 13:58 a.txt
[root@MineGi /test/touch]# touch -r a.txt file1
[root@MineGi /test/touch]# ls -lh file1
-rw-r--r-- 1 root root 0 11月 23 13:58 file1
[root@MineGi /test/touch]# touch -t 201904161914.50 file1
[root@MineGi /test/touch]# ls -lh file1
-rw-r--r-- 1 root root 0 4月 16 2019 file1
[root@MineGi /test/touch]# ls -lh --full-time file1
-rw-r--r-- 1 root root 0 2019-04-16 19:14:50.000000000 +0800 file1
[root@MineGi /test/touch]# touch `hostname`_`date +%F`.txt
[root@MineGi /test/touch]# ls -lh `hostname`_`date +%F`.txt
-rw-r--r-- 1 root root 0 11月 23 14:11 MineGi_2024-11-23.txt
[root@MineGi /test/touch]# hostname
MineGi
[root@MineGi /test/touch]# date +%F
2024-11-23
[root@MineGi /test/touch]#
注意事项
- 如果使用 -c 选项并且文件不存在,touch 命令不会创建新文件,也不会产生任何错误消息。
- 当更改文件的时间戳时,只有文件的访问时间和修改时间会被更改,文件的创建时间(inode change time)通常不会被更改。
touch 命令是 Linux 和其他 Unix-like 操作系统中非常实用的工具,它不仅可以用于创建空文件和更新文件时间戳,还可以结合其他命令实现更复杂的操作。通过掌握其基本语法、常用选项以及特性和使用场景,我们可以更加高效地管理和操作文件系统中的文件。希望本文的详细介绍和示例能够帮助你更好地理解和使用 touch 命令!