Linux/Unix: How to copy a directory and save the date/time file information

If you need to copy a directory on Unix/Linux and want to preserve the date/time information while copying the directory and files, use the -p option to save the date/time information, and the -r option to copy the directory properly. For instance, I just used this cp command to copy a directory named OldDir to a new directory named NewDir, while retaining all of the date/time file information:

$ cp -pr OldDir NewDir

I just ran that command from the MacOS Terminal, but it will work the same way on Linux systems. Here’s part of the man page from the cp -p option on MacOS:

Cause cp to preserve the following attributes of each source file in the copy: modification time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions. Access Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also be preserved.

In summary, if you ever need to copy a directory on a Unix/Linux system and save the date/time information for all of the files, the -pr options are what you need.

“Treat every moment as your last.
It is not preparation for something else.”
~ Shunryu Suzuki