Quick File Backup from Command Line
In this article
To make a quick backup of a file from the command line, use the cp command on Linux/macOS or copy on Windows. For example, cp important.txt important.txt.bak creates a copy in the same directory, and copy important.txt important.txt.bak does the same on Windows.
Why Use the Command Line for Backups?
The command line is fast, scriptable, and works consistently across many systems. Unlike graphical file managers, it lets you copy files with precise control over names, destinations, and options. It's especially useful when you need to back up many files, automate backups with cron or Task Scheduler, or work on a remote server where there is no desktop interface.
Command-line tools are also lightweight—they don't require opening a file manager window or dragging icons. For a single file, a one-line command is often quicker than navigating menus.
Basic Copy Commands on Linux and macOS
The standard command is cp, which stands for "copy." Its basic syntax is:
cp source_file destination_file
Common variations:
cp file.txt file.txt.bak— creates a backup with a.bakextension in the same folder.cp file.txt /path/to/backup/— copies to a different directory, keeping the original name.cp -i file.txt file.txt.bak— prompts before overwriting an existing file (interactive mode).cp -v file.txt file.txt.bak— verbose mode, shows what is being copied.cp -p file.txt file.txt.bak— preserves permissions, timestamps, and ownership (when allowed).
If you want to back up a directory and its contents, use cp -r (recursive). For example: cp -r project/ project_backup/.
Basic Copy Commands on Windows (Command Prompt and PowerShell)
Windows offers two main command-line environments: Command Prompt (cmd.exe) and PowerShell. Both can copy files, but the syntax differs.
Command Prompt (cmd)
copy file.txt file.txt.bak— basic copy.copy /Y file.txt file.txt.bak— overwrite without asking.copy /-Y file.txt file.txt.bak— ask before overwriting.copy /V file.txt file.txt.bak— verify the copy by comparing files.
To copy a directory tree, use xcopy or robocopy. robocopy is more powerful and recommended for backups: robocopy source_dir backup_dir /E copies all subdirectories, including empty ones.
PowerShell
PowerShell uses Copy-Item (alias copy or cp). Examples:
Copy-Item file.txt file.txt.bakCopy-Item file.txt -Destination backup.txtCopy-Item folder -Recurse -Destination folder_backup
PowerShell also supports -Force to overwrite read-only files, and -WhatIf to preview what would happen.
Choosing a Backup File Name
A good backup name should tell you what it is and when it was made. Common patterns:
file.txt.bak— simple, but only one backup per file.file.txt.2025-01-15— includes the date.file.txt.20250115_1530— date and time (HHMM).file.txt.old— indicates previous version.
To add a timestamp automatically in Linux/macOS, you can use the date command in a shell script:
cp file.txt file.txt.$(date +%Y%m%d_%H%M%S)
On Windows Command Prompt, you can use environment variables like %DATE% and %TIME%, but they include spaces and slashes, so it's often easier to use PowerShell: Copy-Item file.txt file_$(Get-Date -Format yyyyMMdd_HHmmss).bak.
Making a Timestamped Backup with a One-Liner
If you want a quick backup with a timestamp, you can use the following commands:
- Linux/macOS:
cp file.txt file.txt.$(date +%Y%m%d) - PowerShell:
Copy-Item file.txt file_$(Get-Date -Format yyyyMMdd).bak - Command Prompt: This is trickier because
%DATE%includes slashes. A simple workaround is to usecopy file.txt file.txt.bakand rely on the directory listing to show the date.
These commands create a new backup each time you run them, so you keep a history of versions.
Common Mistakes and How to Avoid Them
- Overwriting without noticing: If the destination file already exists,
cpandcopywill overwrite it by default (unless you use-ior/-Y). Always check if a backup with that name already exists. - Copying the file into itself: If you try to copy a file to the same path, you'll get an error like "cp: 'file.txt' and 'file.txt' are the same file." Use a different name or directory.
- Forgetting the recursive flag: When copying directories, you must use
-ron Linux/macOS or/Ewithrobocopyon Windows. Without it, you'll get an error or only copy the top folder. - Spaces in filenames: If your filename contains spaces, quote it:
cp "my file.txt" "my file.txt.bak". - Permissions: On Linux/macOS, if you don't have write permission to the destination directory, the copy will fail. Use
sudoonly if you understand the risks.
Alternatives to cp and copy
Depending on your needs, other tools might be better:
rsync(Linux/macOS, also available on Windows via WSL or Cygwin) — synchronizes files and directories, only copying changed parts. Great for large backups.tar— creates an archive (compressed or not) of one or more files. Useful for bundling multiple files into a single backup.git— if you're backing up source code, use version control to track changes and revert easily.dd— makes a byte-for-byte copy of a file or entire disk. Use with caution.
For quick, simple backups, cp or copy is usually enough. For automated or recurring backups, consider writing a small script and scheduling it.
Frequently Asked Questions
How do I back up a file without overwriting the previous backup?
Use a timestamp in the backup name, as shown above. For example, cp file.txt file.txt.$(date +%Y%m%d_%H%M%S) creates a unique name each time. Alternatively, use cp -b (Linux) to create a backup of the destination file before overwriting, but that only keeps one previous version.
What's the difference between cp and rsync for backups?
cp copies a file or directory in one go. rsync is more advanced: it can copy only changed parts, preserve permissions, and synchronize directories. For a single file, cp is simpler; for a whole folder or repeated backups, rsync is more efficient.
Can I back up a file to a remote server from the command line?
Yes. Use scp (secure copy) or rsync over SSH. For example: scp file.txt user@server:/path/to/backup/. On Windows, you can use pscp or install OpenSSH.
How do I back up multiple files at once?
You can list multiple sources: cp file1.txt file2.txt backup_dir/ (the last argument must be a directory). Or use a wildcard: cp *.txt backup_dir/. For directories, use cp -r.
What does the -p flag in cp do?
The -p flag preserves the original file's modification time, access time, and permissions (and ownership if you have the rights). This is useful when you want the backup to have the same metadata as the original.
Is it safe to use sudo with cp for backups?
It can be safe if you need to copy a file you don't have read permission for, but be careful: using sudo can overwrite system files if you mistype a path. Always double-check the destination. For normal user files, you shouldn't need sudo.