1

I am using rsync-3.2.3 on Fedora 33 (GNOME).

But how can I keep the access time (atime) for my files and folders?

I can only keep the modified time (mtime) with this command:

rsync -t
  • Are you talking about preserving atime for the source files (i.e. pretending you haven't read them) or for the destination? – Chris Davies Jan 21 '21 at 14:14
  • For the source and destination. I want to transfer my files and folders (internal disks) to my external USB drive (SD cards, 1 TB hard drive etc.) as a clone (copy). – Laradu9sna7al Jan 21 '21 at 14:51
  • Unfortunately I only managed to keep for modified time (mtime), but for access time (atime) I didn't find any rsync command. Is it possible that rsync does not support this and I have to create a report on github because of feature request? https://github.com/WayneD/rsync/issues – Laradu9sna7al Jan 21 '21 at 14:51

2 Answers2

3

You can ask to preserve atime (access time) on the source with the --noatime flag, but on filesystems mounted with relatime (the modern default) or noatime this already isn't strictly necessary

rsync -av --noatime src/ dstHost:dst/

I know of no option to preserve atime on the destination as a copy of the source natively within rsync. If you have access to the target system you might be able to iterate across the copied tree. Something like this could work on a GNU/Linux type system

( cd src/ && find -type f -print0 ) |
    ssh dstHost 'cd dst && while IFS= read -r -d "" f; do  touch -a -d "@$(stat -c %Y "$f")" "$f"; done'

Or if you are processing a copy between two local filesystems

( cd src/ && find -type f -print0 ) |
    ( cd dst && while IFS= read -r -d "" f; do  touch -a -d "@$(stat -c %Y "$f")" "$f"; done )

Basically these two snippets do the same thing: for each file in the source, find the corresponding file in the destination and update its atime to match its mtime.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • I understand and do you also know about change time (ctime)? Is there also here a rsync command for keep? – Laradu9sna7al Jan 21 '21 at 17:25
  • 1
    @Laradu9sna7al There's no POSIX-like interface to set ctime. You'd either have to use an interface outside the filesystem, or you'd have to fake it by resetting the system time. Neither are appropriate for a tool like rsync. See also https://unix.stackexchange.com/questions/36021/how-can-i-change-change-date-of-file – BowlOfRed Jan 21 '21 at 18:11
  • @BowlOfRed Very interesting then I find change time (ctime) useless for me. It shows by default in GNU/Linux but I can't keep it when copying Is there a terminal command to disable it? – Laradu9sna7al Jan 21 '21 at 19:18
  • @Laradu9sna7al if you change ctime you'll update mtime. And when you change mtime you reset ctime. You cannot control both. I've updated my answer to show a possible method for updating atime from mtime. On some versions of stat you might be able to use this for managing btime too – Chris Davies Jan 21 '21 at 20:47
  • @Laradu9sna7al, I'm not sure what disabling it would mean. The space for it is allocated already. It's never written unless something else is written, so it doesn't take any additional I/O. There's no resources that could be returned other than a microscopic amount of CPU. It can be copied if the filesystem is copied (such as via dump), but can't be set on a mounted filesystem (which is what rsync uses). – BowlOfRed Jan 21 '21 at 23:58
  • @BowlOfRed I meant disabling this file system timestamp, it is no longer stored because there is no use for me. Is there such a possibility or must a kernel driver be written for it? For example, when you run a stat command, it doesn't show anything at change time, just an empty character (-). – Laradu9sna7al Jan 22 '21 at 01:29
  • @Laradu9sna7al All the timestamps and other metadata are kept in a block of fixed size and fixed format. That block is read and written as a unit. Without changing the filesystem itself, there is no way to modify the format of that block. Further, POSIX interfaces assume that data exists. You could have the filesystem fake the data (e.g. return the mtime instead for all operations), but "turning it off" isn't really possible. https://www.gnu.org/software/coreutils/manual/html_node/File-timestamps.html#:~:text=Standard%20POSIX%20files%20have%20three,to%20the%20file's%20meta%2Dinformation. – BowlOfRed Jan 22 '21 at 04:19
  • On my rsync 3.2.7, --noatime has become --open-noatime – xenoid Apr 26 '23 at 00:40
0

Since rsync version 3.2.0, there are two flags that affect atimes:

  • --atimes, -U preserve access (use) times
  • --open-noatime avoid changing the atime on opened files

The full description of these is:

       --atimes, -U
              This  tells  rsync to set the access (use) times of the destina‐
              tion files to the same value as the source files.
          If repeated, it also sets the --open-noatime option,  which  can
          help you to make the sending and receiving systems have the same
          access times on the transferred files  without  needing  to  run
          rsync an extra time after a file is transferred.

          Note  that  some  older rsync versions (prior to 3.2.0) may have
          been built with a pre-release --atimes patch that does not imply
          --open-noatime when this option is repeated.

   --open-noatime
          This  tells rsync to open files with the O_NOATIME flag (on sys‐
          tems that support it) to avoid changing the access time  of  the
          files  that  are being transferred.  If your OS does not support
          the O_NOATIME flag then rsync will silently ignore this  option.
          Note  also  that  some filesystems are mounted to avoid updating
          the atime on read access even without the O_NOATIME  flag  being
          set.

So my reading of these (which testing has borne out) is that the following will both keep rsync from updating the atime on src, and will copy the atime of src to dest:

rsync -UU <src> <dest>
eil
  • 373
  • 1
  • 3
  • 9