Linux touch命令

touch命令
1、 介绍
touch - change file timestamps 用来修改文件时间戳,或者新建一个不存在的文件。
2、 命令格式
touch [选项]… 文件…
3、 命令功能
用来修改文件时间戳,或者新建一个不存在的文件。
4、 命令参数
-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(使用指定的日期时间,而非现在的时间)

-h, --no-dereference
affect each symbolic link instead of any referenced file (useful only on systems that can chahe 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(输出版本信息,并退出)

5、 命令范例
范例1:创建不存在的文件
[root@lianxi tmp]# touch file.log
[root@lianxi tmp]# ls
1d 2d 3d 4d file.log TEST

范例2:修改文件的access访问时间,如刚刚创建的file.log文件的access时间
[root@lianxi tmp]# stat file.log

File: `file.log’ Size: 0 Blocks: 0 IO Block: 4096
regular empty file Device: 803h/2051d Inode: 392496 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-08 15:26:08.855294596 +0800
Modify: 2018-12-08 15:26:08.855294596 +0800
Change: 2018-12-08 15:26:08.855294596 +0800

[root@lianxi tmp]# touch -a file.log
[root@lianxi tmp]# stat file.log
File: `file.log’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 392496 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-08 15:31:53.304290630 +0800
Modify: 2018-12-08 15:26:08.855294596 +0800
Change: 2018-12-08 15:31:53.304290630 +0800

范例3:修改文件的modify修改时间,如file.log文件的modify时间。
[root@lianxi tmp]# stat file.log

File: `file.log’ Size: 0 Blocks: 0 IO Block: 4096
regular empty file Device: 803h/2051d Inode: 392496 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-08 15:31:53.304290630 +0800
Modify: 2018-12-08 15:26:08.855294596 +0800
Change: 2018-12-08 15:31:53.304290630 +0800

[root@lianxi tmp]# touch -m file.log
[root@lianxi tmp]# stat file.log
File: `file.log’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 392496 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-08 15:31:53.304290630 +0800
Modify: 2018-12-08 15:41:57.971303945 +0800
Change: 2018-12-08 15:41:57.971303945 +0800

范例4:使用指定的时间更改文件或目录的access或者modify时间,如修改file.log文件时间为2018-12-10 15:50:00,若修改modify时间,只需将命令中的a改成m即可。
[root@lianxi tmp]# stat file.log

File: `file.log’ Size: 0 Blocks: 0 IO Block: 4096
regular empty file Device: 803h/2051d Inode: 392496 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-08 15:31:53.304290630 +0800
Modify: 2018-12-08 15:41:57.971303945 +0800
Change: 2018-12-08 15:41:57.971303945 +0800

[root@lianxi tmp]# touch -at 201812101550.00 file.log
[root@lianxi tmp]# stat file.log
File: `file.log’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 392496 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-10 15:50:00.000000000 +0800
Modify: 2018-12-08 15:41:57.971303945 +0800
Change: 2018-12-08 15:47:39.187278782 +0800

范例5:使用指定文件的时间戳更新文件的时间戳,例如使用1d文件夹的时间戳更新file.log的时间
[root@lianxi tmp]# stat file.log 1d

File: `file.log’ Size: 0 Blocks: 0 IO Block: 4096
regular empty file Device: 803h/2051d Inode: 392496 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-10 15:50:00.000000000 +0800
Modify: 2018-12-08 15:41:57.971303945 +0800
Change: 2018-12-08 15:47:39.187278782 +0800

File: `1d’ Size: 4096 Blocks: 8 IO Block: 4096
directory Device: 803h/2051d Inode: 392450 Links: 2 Access:
(0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-08 12:42:00.606295509 +0800
Modify: 2018-12-08 00:44:12.000000000 +0800
Change: 2018-12-08 03:14:08.280329959 +0800

[root@lianxi tmp]# touch -r 1d file.log
[root@lianxi tmp]# stat file.log 1d
File: `file.log’ Size: 0 Blocks: 0 IO Block: 4096
regular empty file Device: 803h/2051d Inode: 392496 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-08 12:42:00.606295509 +0800
Modify: 2018-12-08 00:44:12.000000000 +0800
Change: 2018-12-08 15:59:30.750288768 +0800

File: `1d’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h/2051d Inode: 392450 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-12-08 12:42:00.606295509 +0800
Modify: 2018-12-08 00:44:12.000000000 +0800
Change: 2018-12-08 03:14:08.280329959 +0800

注:文件时间类型区别
access 表示最后一次访问(仅仅是访问,没有改动)文件的时间
modify 表示最后一次修改文件的时间
change 表示最后一次对文件属性改变的时间,包括权限,大小,属性等等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值