Moving files between directories doesn’t require a graphical interface when you know the right terminal command. Learning how to copy folder in linux is one of the most essential skills for anyone using the command line. Whether you are backing up data, organizing projects, or setting up server files, the cp command is your go-to tool.
This guide walks you through every step. You will learn the basic syntax, advanced options, and common pitfalls. By the end, you will copy folders like a pro.
Understanding The cp Command Basics
The cp command stands for “copy.” It works for both files and directories. But copying a folder requires an extra flag. Without it, cp will throw an error.
Here is the basic syntax:
cp [options] source destination
To copy a folder, you must use the -r or -R flag. This stands for recursive. It tells the command to copy the folder and everything inside it.
Example:
cp -r /path/to/source_folder /path/to/destination_folder
This copies the entire source folder into the destination. If the destination does not exist, it creates a new folder with the same name.
Why The Recursive Flag Is Necessary
Folders contain files and subfolders. A non-recursive copy would only attempt to copy the folder itself, which is not a file. The -r flag ensures all contents are duplicated.
Without -r, you will see an error like:
cp: omitting directory 'source_folder'
So always remember: folders need -r.
How To Copy Folder In Linux: Step-By-Step
Now let’s get practical. Follow these steps to copy any folder on your system.
Step 1: Open The Terminal
Press Ctrl + Alt + T on most Linux distributions. Or search for “Terminal” in your applications menu.
Step 2: Navigate To The Source Directory (Optional)
You can run the command from anywhere. But it helps to know your current location. Use pwd to print the working directory.
pwd
Step 3: Run The Copy Command
Suppose you have a folder named my_project in your home directory. You want to copy it to /backup.
cp -r ~/my_project /backup/
This creates /backup/my_project with all contents.
Step 4: Verify The Copy
Use ls to list the destination:
ls /backup/
You should see the folder name. For a deeper check, use tree or ls -R.
Advanced Options For Copying Folders
The cp command has many useful flags. Here are the most important ones for folder operations.
Preserve File Attributes With -p
When you copy a folder, you may want to keep timestamps, permissions, and ownership. Use the -p flag.
cp -rp source_folder destination_folder
This preserves modification times, access times, and file mode bits.
Verbose Output With -v
To see what is being copied, add -v. It prints each file name as it copies.
cp -rv source_folder destination_folder
This is great for large folders. You can monitor progress.
Interactive Mode With -i
If the destination already has a file with the same name, cp will overwrite it silently. To be prompted before overwriting, use -i.
cp -ri source_folder destination_folder
Type y to overwrite or n to skip.
Update Only With -u
Copy only when the source file is newer than the destination file. This is useful for incremental backups.
cp -ru source_folder destination_folder
It saves time by skipping unchanged files.
Copy With Backup –backup
If you want to keep the original destination files, use --backup. It renames existing files with a tilde (~) suffix.
cp -r --backup source_folder destination_folder
Common Scenarios For Copying Folders
Let’s look at real-world examples. These will help you understand how to copy folder in linux in different situations.
Copying A Folder To Another Location
Simple copy to a different directory:
cp -r /home/user/docs /mnt/backup/
This copies the docs folder into /mnt/backup.
Copying A Folder With A New Name
Specify a new name in the destination:
cp -r /home/user/docs /home/user/old_docs
This creates a copy named old_docs in the same parent directory.
Copying Multiple Folders At Once
List multiple sources before the destination:
cp -r folder1 folder2 folder3 /target/
All three folders are copied into /target.
Copying Folder Contents Without The Folder Itself
To copy only the contents (files and subfolders) of a folder, use a trailing slash on the source:
cp -r source_folder/* destination_folder/
This copies everything inside source_folder directly into destination_folder, without creating a source_folder subdirectory.
Using Wildcards With Folder Copying
Wildcards let you copy folders that match a pattern. The asterisk * matches any characters.
Example: Copy all folders starting with “data”:
cp -r data* /backup/
This copies data1, data_backup, etc.
Be careful: wildcards expand before the command runs. If no match exists, you might get an error.
Permissions And Ownership When Copying
When you copy a folder, the new copy inherits the permissions of the user running the command. To preserve original permissions, use -p as mentioned.
For ownership, you need root privileges. Use sudo:
sudo cp -rp source_folder destination_folder
This preserves both permissions and ownership.
Copying Folders Over SSH (Remote)
Sometimes you need to copy a folder to a remote server. Use scp or rsync.
Using scp
scp -r /local/folder user@remote:/remote/path/
This copies the folder over SSH.
Using rsync (Better For Large Transfers)
rsync -av /local/folder user@remote:/remote/path/
The -a flag archives (preserves attributes) and -v gives verbose output. rsync is faster because it only transfers differences.
Common Errors And How To Fix Them
Even experienced users make mistakes. Here are frequent errors when copying folders.
Error: “Omitting Directory”
You forgot the -r flag. Add it and try again.
Error: “Permission Denied”
You do not have write access to the destination. Use sudo or change permissions with chmod.
Error: “No Such File Or Directory”
The source path is wrong. Double-check the spelling and location.
Error: “Cannot Overwrite Non-directory With Directory”
The destination exists as a file, not a folder. Remove the file first or choose a different destination name.
Best Practices For Folder Copying
Follow these tips to avoid data loss and save time.
- Always use
-ior-n(no-clobber) to prevent accidental overwrites. - Use
-vfor large copies to see progress. - Test with a small folder first if you are unsure.
- Use
rsyncfor large or repeated copies—it is more efficient. - Keep backups of important folders before overwriting.
Alternatives To cp For Folder Copying
While cp is standard, other tools offer advantages.
rsync
As mentioned, rsync is ideal for syncing folders. It can resume interrupted transfers and compress data.
rsync -av --progress source_folder destination_folder
tar With Pipe
You can use tar to copy folders while preserving attributes:
tar cf - source_folder | (cd destination_folder && tar xf -)
This is useful for complex copies.
Graphical File Managers
If you prefer GUI, use Nautilus, Dolphin, or Thunar. Right-click the folder and select “Copy,” then paste in the target location.
How To Copy Folder In Linux With Symbolic Links
Symbolic links (symlinks) are special files that point to other files or folders. By default, cp -r copies the symlink itself, not the target. To follow symlinks and copy the actual files, use the -L flag.
cp -rL source_folder destination_folder
To preserve symlinks as links, use -P (the default).
Copying Large Folders Efficiently
For folders with thousands of files or gigabytes of data, performance matters.
- Use
rsyncwith--progressto monitor. - Avoid
-vif you want speed (less output). - Use
ioniceto reduce disk I/O impact:
ionice -c 2 -n 7 cp -r source_folder destination_folder
This lowers the copy priority.
Automating Folder Copies With Scripts
You can write a simple bash script to copy folders regularly.
#!/bin/bash
cp -ru /home/user/documents /mnt/backup/
Save as backup.sh, make it executable with chmod +x backup.sh, and run it with ./backup.sh. Add it to cron for automation.
Frequently Asked Questions
How do I copy a folder and all its contents in Linux?
Use the cp -r command. For example: cp -r source_folder destination_folder. The -r flag ensures recursive copying.
What is the difference between cp -r and cp -a?
cp -a (archive) is like cp -r but also preserves permissions, ownership, timestamps, and symlinks. It is equivalent to cp -dR --preserve=all.
Can I copy a folder without overwriting existing files?
Yes. Use the -n (no-clobber) flag: cp -rn source_folder destination_folder. This skips files that already exist in the destination.
How do I copy a folder from one Linux server to another?
Use scp -r or rsync -av. Both work over SSH. Example: scp -r /local/folder user@remote:/remote/path/.
Why does my copy say “omitting directory”?
You omitted the -r flag. Add it to copy directories. The error means cp refuses to copy a folder without recursive mode.
Conclusion
Now you know how to copy folder in linux using the command line. The cp -r command is simple but powerful. With flags like -p, -v, and -i, you can handle almost any copying task.
Practice with small folders first. Experiment with rsync for larger jobs. Over time, you will copy folders quickly and without errors. The terminal gives you full control—use it wisely.
Remember: always double-check your source and destination paths. A wrong command can overwrite important data. But with the knowledge from this guide, you are ready to manage your files like a seasoned Linux user.