通过减去4:00小时来更改文件创建日期

问题描述 投票:0回答:1

我在c:/users/text.txt有一个文件这个text.txt有一个文件创建日期,如何获取text.txt创建日期并使用shell脚本减去-4:00

last_update=$(stat -c "%n %y" $file)

这句话给了我文件创建日期。我怎么能从它减去-4:00?

假设text.txt文件是在04/04/2019 4:00创建的,我想将其更改为04/04/2019 12:00

linux shell
1个回答
2
投票

%y不是创作,而是最后修改日期。

获取自Epoch以来的秒数并从中减去4小时,使用date将其转换为人类可读形式,使用touch更改文件的访问和修改时间:

$ stat file
  File: file
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: b31ch/45852d    Inode: 65386       Links: 1
Access: (0600/-rw-------)  Uid: (10138/ u0_a138)   Gid: (10138/ u0_a138)
Access: 2019-04-04 12:34:56.172954982 +0300
Modify: 2019-04-04 12:34:56.172954982 +0300
Change: 2019-04-04 12:34:56.172954982 +0300
 Birth: -
$
$ stat -c '%y' file
2019-04-04 12:34:56.172954982 +0300
$ stat -c '%Y' file
1554370496
$ date -d @$(( $(stat -c '%Y' file) - 4*60*60 ))
Thu Apr  4 08:34:56 +03 2019
$ touch -d "$( date -d @$(( $(stat -c '%Y' file) - 4*60*60 )) )" file
$
$ stat file
  File: file
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: b31ch/45852d    Inode: 65386       Links: 1
Access: (0600/-rw-------)  Uid: (10138/ u0_a138)   Gid: (10138/ u0_a138)
Access: 2019-04-04 08:34:56.000000000 +0300
Modify: 2019-04-04 08:34:56.000000000 +0300
Change: 2019-04-04 12:37:14.492954929 +0300
 Birth: -

有关详细信息,请参阅stat(1)date(1)touch(1)

© www.soinside.com 2019 - 2024. All rights reserved.