How to preserve files original creation date?
Categories:
Preserving File Creation Dates During Transfers and Copies

Learn how to maintain the original creation and modification timestamps of files when moving or copying them across different systems, especially using SSH.
When working with files, especially in development, backup, or archival scenarios, preserving the original creation and modification dates is crucial. Standard copy or move operations often update these timestamps to the time of the operation, which can lead to data integrity issues or confusion. This article explores various methods to ensure these critical metadata attributes remain intact, focusing on command-line tools commonly used in SSH environments.
Understanding File Timestamps
Files on Unix-like systems (including Linux and macOS) typically have three primary timestamps:
- atime (access time): The last time the file's data was accessed (read).
- mtime (modification time): The last time the file's data content was modified.
- ctime (change time): The last time the file's metadata (e.g., permissions, ownership, or mtime) was changed. This is often referred to as 'status change time'.
It's important to note that the concept of a 'creation time' (btime or birth time) is not universally supported or easily accessible across all Unix-like file systems. While some modern file systems like ext4 and XFS do store a creation timestamp, older ones or network file systems might not expose it directly or reliably. When we talk about preserving 'creation date' in a general sense, we often refer to mtime as it's the most commonly preserved and relevant timestamp for tracking content changes.

File Timestamps and Their Associated Operations
Preserving Timestamps with cp
The cp command is the most basic tool for copying files. By default, cp updates the modification time of the destination file to the current time. However, it offers options to preserve timestamps.
cp -p source_file destination_file
cp -a source_directory/ destination_directory/
Using cp -p to preserve modification times and cp -a for archiving.
The -p option preserves the modification time, access time, and file mode (permissions). The -a option, often called 'archive mode', is a more comprehensive option that preserves permissions, ownership, timestamps, and recursively copies directories. It's equivalent to -dR --preserve=all.
cp -p and cp -a are effective for local copies, they do not preserve the 'creation time' (btime) if the underlying filesystem supports it. They primarily focus on mtime and atime.Preserving Timestamps with rsync over SSH
rsync is a powerful utility for synchronizing files and directories, especially across networks using SSH. It's highly efficient and provides robust options for preserving file attributes.
rsync -avz /path/to/source/ user@remote_host:/path/to/destination/
Using rsync to copy files over SSH while preserving attributes.
Let's break down the rsync options:
-a(archive mode): This is a combination of several options (-rlptgoD), which ensures that symbolic links, permissions, modification times, group, owner, and device files are preserved. This is the most common and recommended option for preserving file attributes.-v(verbose): Shows progress and details of the transfer.-z(compress): Compresses file data during transfer, which can speed up transfers over slow networks.
rsync is generally the preferred method for remote transfers due to its efficiency (it only transfers changed parts of files) and comprehensive attribute preservation.
rsync, be mindful of the trailing slash on source paths. /path/to/source/ copies the contents of the directory, while /path/to/source copies the directory itself into the destination.Using tar and ssh for Remote Transfers
Another robust method for transferring files and preserving attributes, especially when dealing with complex directory structures or when rsync is not available, is to combine tar with ssh.
tar -cf - /path/to/source_directory | ssh user@remote_host 'tar -xf - -C /path/to/destination_directory'
Piping tar output over SSH to preserve file attributes.
Here's how this command works:
tar -cf - /path/to/source_directory: Creates a tar archive (-c) ofsource_directory. The-f -tellstarto write the archive to standard output.| ssh user@remote_host: Pipes the standard output of thetarcommand to the standard input of ansshsession on the remote host.'tar -xf - -C /path/to/destination_directory': On the remote host,tarextracts (-x) the archive from its standard input (-f -) into the specified destination directory (-C).
This method preserves permissions, ownership, and modification times. It's particularly useful for transferring entire directory trees efficiently.
tar over SSH. Ensure the target directory exists and has appropriate permissions, as tar will extract directly into it.Restoring Creation Time (btime) with touch (Advanced)
While cp, rsync, and tar effectively preserve mtime and atime, explicitly setting a 'creation time' (btime) is more challenging due to varying filesystem support and tool capabilities. If you have access to the original file's creation time (e.g., from stat output on a supporting filesystem), you might be able to restore it using touch on some systems, though this is not universally reliable.
First, get the birth time from the source file (if available):
stat -c %W source_file # Linux (ext4, XFS)
stat -f %B source_file # macOS
Retrieving birth time using stat.
The output will be a Unix timestamp. You can then attempt to set it, but touch primarily modifies mtime and atime. Some advanced tools or filesystem-specific utilities might offer more direct control over btime, but this is beyond the scope of general cross-platform file management.
mtime is sufficient as it indicates the last content modification. btime is often less critical for day-to-day operations and harder to manage consistently across different systems.