2

Moving directory without modifying timestamp says that mv changes the timestamp of a directory.

When I mv a file from an external hard drive to another external hard drive, it also changes the timestamp of the directory.

When I mv a directory within a local filesystem, it doesn't change the timestamp of the directory.

When does mv change the timestamp and other attributes of a file and when does it not? Thanks.

  • This question could have been improved by illustrating cases in which mv will modify timestamps and showing which timestamps it will affect from your observations. I think that would make it much clear and improve it greatly. (I didn't downvote the question, just giving some constructive criticism to help improve it.) – filbranden Feb 27 '19 at 5:09
  • @filbranden I have tried to explain the cases. Not sure what other information to provide. – Tim Feb 27 '19 at 15:25
  • an example of mv command with ls -l around it to show how timestamps are changing when that would be unexpected... Including information about filesystems and filesystem types, when that's relevant. – filbranden Feb 27 '19 at 15:36
6

I remember running into this on other systems, and having different effects than testing with it now shows. I think these are implementation differences. I'm describing them as such below.

Most implementations of mv attempt to not change the timestamp of a file when it moves it. However, may not consider the effects of after portions of the move.

This basically means:

  • If you are moving a file or directory within a filesystem, the timestamps on that file or directory won't change.

  • If you move a file to a different filesystem, its timestamp will be reset to the current time, because this is secretly a copy. However, mv is generally smart enough to restore the old timestamp to the best of its ability, so access time and modify time will "remain" the same, but the change timestamp will be updated to the current time.

  • If you move an empty directory to a different filesystem, the same rule applies.

  • If you move a directory with contents to a different filesystem, mv will mkdir a new directory, replicate the permissions and ownership, and restore the original time... and then move the contents of that directory into it, updating its timestamp. Some implementations fix this (like GNU coreutils mv 8.29), and others don't (like the ones I remember testing this behavior on before).

  • mv will always update the timestamp on the directory it moves files/directories from and the directory into which it moves files/directories. (Basically, it modifies these directories, so of course it updates their modify timestamp, so long as the filesystem in question has modify timestamps. As far as I'm aware, that's all of them.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.