Copy and Rename Files in Linux Terminal
In this article
Copying and renaming files from the Linux terminal is a fast, scriptable way to manage files without opening a file manager. The core commands are cp (copy) and mv (move/rename). You can copy a file to a new name in the same folder, or to a different folder with a new name, all in one command. You only need a terminal window and basic file paths.
What You'll Need
- A Linux system with a terminal (any distribution works).
- Basic knowledge of navigating directories with
cdand listing files withls. - Write permissions on the source and destination folders (usually true for your home directory).
Step-by-Step
- Open a terminal window. Press
Ctrl+Alt+Ton most desktop Linux setups, or search for “Terminal” in your applications menu. - Navigate to the folder containing the file you want to copy. Use
cd(change directory). For example:cd ~/Documents. If the file is already in your home folder, you can skip this step. - Verify the file exists. Run
ls -lto list files with details. You should see the file name and its permissions. If you see a “Permission denied” error, you may need to usesudo(see Common Mistakes). - Copy and rename in the same folder. Use the
cpcommand with the original name first, then the new name. For example:cp report.txt report_backup.txt. This creates a copy ofreport.txtnamedreport_backup.txtin the current directory. The original file remains unchanged. - Copy to a different folder with a new name. Specify the destination folder and the new name. For example:
cp report.txt /home/user/backups/report_2025.txt. This copies the file to thebackupsfolder and renames it. Ensure the destination folder exists; otherwise, you’ll get an error. - Rename without copying (move). To rename a file in place (keeping it in the same folder), use
mvinstead ofcp. For example:mv oldname.txt newname.txt. This changes the file’s name without creating a duplicate. - Move and rename in one step. Use
mvwith a destination path that includes the new name. For example:mv notes.txt /home/user/archive/notes_final.txt. This moves the file to thearchivefolder and renames it. - Use relative paths if you’re not in the folder. You don’t have to
cdfirst. For example, from your home folder, you can runcp Documents/report.txt Documents/report_backup.txt. - Copy multiple files at once (optional). If you want to copy several files to a new folder but keep their names, use
cp file1.txt file2.txt /destination/folder/. To rename them individually, you’ll need a loop or a separate command for each. - Check the result. Run
ls -lin the destination folder (or current folder) to confirm the new file exists with the correct name and size.
Common Mistakes
- Forgetting the destination folder exists. If you specify a folder that doesn’t exist,
cpwill give an error like “No such file or directory”. Create it first withmkdir /path/to/folder. - Using
mvwhen you meant to keep the original.mvdeletes the original. If you want a copy, usecp. - Overwriting an existing file without warning. By default,
cpandmvwill silently overwrite a file with the same name. To avoid this, use the-i(interactive) flag:cp -i source destormv -i source dest. It will ask before overwriting. - Spaces in file names. If a file name contains spaces, you must quote it:
cp "my report.txt" "my report backup.txt". Without quotes, the shell treats each word as a separate argument. - Using
sudounnecessarily. If you get a permission error, it might be because the file is owned by another user or in a system directory. Only usesudoif you are certain you need it; otherwise you risk changing ownership or permissions incorrectly.
Tips
- Use tab completion. Type the first few letters of a file name and press
Tabto auto-complete it. This avoids typos and handles spaces automatically. - Add a date to the new name. For backups, a common pattern is
cp report.txt report_$(date +%Y%m%d).txt. This creates a file likereport_20250315.txt. The$(date ...)part runs the date command and inserts the result. - Copy with permissions and timestamps. Use
cp -pto preserve the original file’s modification time and permissions. This is useful for backups. - Use
lsto confirm. After any copy or rename, runls -lto see the file size, date, and name. It’s a quick sanity check. - Learn the difference between
cpandmv.cpduplicates,mvmoves (or renames). If you’re unsure, ask yourself: “Do I want the original to remain?”
Troubleshooting
If the command does not work as expected, try these checks in order:
- Check for typos. Run
lsto see the exact file name. A single wrong letter or missing extension will cause a “No such file” error. - Check the destination path. Use
pwdto see your current directory, andls /path/to/destinationto confirm the folder exists. If not, create it withmkdir -p /path/to/destination(the-pcreates parent folders too). - Look at the error message. If it says “Permission denied”, you don’t have write access to the destination. Try copying to your home folder first (e.g.,
cp file ~/). If you must write to a system folder, you may needsudo, but be careful—usingsudowithcpcan overwrite system files if you’re not precise. - If the file appears but is empty or truncated. Check if the source file is still being written by another program. Wait a moment and try again. Also, ensure you have enough disk space with
df -h. - If you get “Argument list too long”. This happens when copying many files at once. Use a loop instead:
for f in *.txt; do cp "$f" "backup_$f"; done. This copies each .txt file with a “backup_” prefix. - If you accidentally overwrote a file. Unfortunately, there is no built-in undo in the terminal. Check if you have a backup in your trash or a version control system. To avoid this in the future, always use
-iwhen you’re unsure.
Frequently Asked Questions
Can I copy a file and rename it in one command?
Yes. cp original.txt newname.txt copies and renames in one step, as long as the destination is the same folder or you include a full path with the new name.
What is the difference between cp and mv?
cp creates a duplicate of the file, leaving the original intact. mv moves the file to a new location or renames it, removing the original. Use mv when you want to rename without keeping a copy.
How do I copy a file from one folder to another and rename it?
Use cp /source/folder/file.txt /destination/folder/newfile.txt. The destination path can include a different file name. Ensure the destination folder exists.
What if the file name has spaces?
Quote the file name with double quotes, for example: cp "my file.txt" "my file copy.txt". Or use tab completion to insert the name correctly.
Can I copy a file and keep the original permissions?
Yes, use cp -p source dest. This preserves the original file’s modification time, access time, and permissions (but not ownership, unless you are root).
How do I avoid overwriting an existing file?
Use the -i flag with cp or mv. For example, cp -i source dest will prompt you before overwriting. You can also use cp -n to never overwrite an existing file.