Skip to content
Linux Basics: Keep Earlier File Versions with GNU cp Backups

Linux Basics: Keep Earlier File Versions with GNU cp Backups

October 30, 2026

When replacing a local configuration draft, keeping the previous destination makes a mistaken edit easier to recover from. GNU cp --backup=numbered retains successive destination versions alongside the current file. This example uses disposable text files, so it never changes a real configuration.

Prepare a scratch directory

Tested on Linux with GNU Bash 5.2.37 and GNU coreutils 9.7. You need Bash, a text editor, and the utilities shown below. The script creates and removes its own temporary fixture directory.

Keep the same terminal open. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-gnu-cp-numbered-backups.sh:

#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
printf 'version one\n' > current.txt
printf 'version two\n' > candidate.txt
cp --backup=numbered -- candidate.txt current.txt
printf 'version three\n' > candidate.txt
cp --backup=numbered -- candidate.txt current.txt
for file in current.txt current.txt.~1~ current.txt.~2~; do
    printf '%s: ' "$file"
    cat -- "$file"
done
cmp -- candidate.txt current.txt
test "$(cat current.txt.~1~)" = 'version one'
test "$(cat current.txt.~2~)" = 'version two'

Run it:

bash demonstrate-gnu-cp-numbered-backups.sh

Observed output:

current.txt: version three
current.txt.~1~: version one
current.txt.~2~: version two

Read the result

current.txt.~1~ holds the destination before the first copy. The second copy creates current.txt.~2~ from the next destination version. Neither backup is a copy of the newest candidate: the current file receives that candidate. The explicit numbered choice keeps producing numbered backups even when no older backup exists. A bare --backup can choose a different backup policy, so spell out the policy when it matters.

Apply it carefully

These adjacent files are convenient local recovery points, not an independent backup system. Disk failure, directory deletion, or an overly broad cleanup can destroy every version together. Keep important recovery copies elsewhere and set a retention policy. Do not assume the copy and backup operation is an atomic transaction, or use it to coordinate concurrent writers. Inspect the numbered file before restoring it, and account separately for permissions and other metadata when copying real configurations.

The command reference documents the available options.

Clean up

Remove the saved script, return to the starting directory, and remove the empty practice directory:

rm -- demonstrate-gnu-cp-numbered-backups.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect what remains rather than deleting unknown files.

Acknowledgements

This post was created with assistance from AI.

Last updated on