15

I would like to move a directory from one location to another but when I do I can see that the timestamp gets changed. Is there any way to retain the timestamp as original?

Have looked at the man page of mv but couldn't find any existing options.

2 Answers 2

13

Use cp as following, mv doesn't do.

cp -r -p /path/to/sourceDirectory /path/to/destination/

from man cp:

-p
    same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
    preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

then after copy done, delete the sourceDirectory.

6
  • 1
    It will leave the original directory as-is which needs to be deleted too Commented Feb 5, 2018 at 4:34
  • Yes, this is what cp does, you need do a deletion after copy. Commented Feb 5, 2018 at 4:48
  • 1
    Yes. I am more like looking for a single line mv alternative if available. Commented Feb 5, 2018 at 4:55
  • 3
    there is no AFAIK, only option is using rsync with --remove-source-files option to delete files only but this will not delete directory itself, so you need do directory deletion too after that. Commented Feb 5, 2018 at 5:02
  • 3
    So cp -r --preserve=all is the best I can get? Commented Jan 24, 2020 at 11:57
4

On linux, this works:

timestamp=$(stat -c %y foldername)
mv foldername new_foldername
touch -d "$timestamp" new_foldername

The stat -c %y command returns the modification date of the folder, in readable form. That value is retained and used with the command touch -d "$timestamp" to set the time of the new folder.

1
  • 2
    Welcome to the site, and thank you for your contribution. May I recommend that you use the new recommended $( ... ) style for command substitutions instead of backquotes, and not use all-uppercase-variable names unless they are environment variables? Commented Mar 17, 2021 at 9:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.