代码之家  ›  专栏  ›  技术社区  ›  Robert Murphy

ext4在不更改目录时间戳的情况下将文件复制到目录

  •  1
  • Robert Murphy  · 技术社区  · 8 年前

    我想将一个文件复制到一个目录,而不更改ext4文件系统上目录的修改时间戳。一个脚本中有许多文件和目录。

    我研究过rsync和cp选项。

    因此,为了解决这个问题,我应该如何在ext4文件系统上复制一个文件,并在目标目录上保留时间戳?

    有许多方法可以复制文件并保留其属性,但它们可以修改父目录时间戳。需要的是记录时间戳并在复制后应用它的两个步骤。在引用的问题中没有提到。 Giving a file/directory the same modification date as another

    3 回复  |  直到 8 年前
        1
  •  1
  •   that other guy    8 年前

    # Save current modification time
    timestamp=$(stat -c @%Y mydir)
    
    [.. copy/sync files ..]
    
    # Restore that timestamp
    touch -d "$timestamp" mydir
    
        2
  •  0
  •   Lakindu Akash    8 年前

    看看这个 guid . 如果要保留原始时间戳,请使用

    $ touch -r <original_file> <new_file> .

    这将从另一个文件复制属性。如果需要更改特定属性,请使用以下命令。

    为了 访问时间

    $ touch -a --date="1988-02-15" file.txt
    

    为了 修改时间 :

    $ touch -m --date="2020-01-20" file.txt
    

    要查看文件或文件夹的状态,请使用 stat <file>

        3
  •  0
  •   user9854516    8 年前

    $ cp --preserve=timestamps oldfile newfile

    $ cp -p oldfile newfile 会的。 页面清楚地说明了这一点。