Copy a Directory Tree
Copy a Directory Tree
A term folder contains more than one entry: perhaps class lists, marks and subfolders for each class. Copying the folder usefully means copying that tree of contents too. The word recursive means the operation continues into child directories rather than stopping at the first directory name.
We will use cp -R to create a separate term backup. The destination's existing state matters: copying to a new path and copying into an already-existing directory can produce different nesting, so predict the resulting paths before relying on the backup.
What you’ll learn
- Copy a directory and its contents recursively.
- Predict the destination layout when a target directory already exists.
A directory copy includes its children
Source tree
The class list belongs to the term folder.
term/students.txtRecursive copy
cp -R visits the directory entries and copies the tree.
term → term-backupResult
When the target did not already exist, the copied list has this path.
term-backup/students.txt
Copy a term folder with its class list
Prepare the example
Start in a new practice folder where neither term nor term-backup exists. The first two lines prepare a source directory with a student list. The destination is deliberately absent, which makes the resulting layout easy to follow.
Bash · Linux terminal
mkdir term
printf 'Diya\n' > term/students.txt
cp -R term term-backup
cat term-backup/students.txtExpected result
Diyamkdir term creates the parent, and printf saves Diya's name as term/students.txt. cp -R term term-backup then visits the source directory and copies its tree into the new destination. We read term-backup/students.txt to verify that its child was copied too.
If term-backup were already a directory, the usual command would place the source directory inside it, giving a path such as term-backup/term/students.txt. That extra level is a common surprise. Decide whether you want the directory itself or particular contents copied, and verify the final tree rather than judging the operation only by whether it printed an error.
Choose the copied tree and predict its layout
Copying a directory means copying not only the directory name but also the files and subdirectories inside it. The cp command needs the recursive option for this job.
Step 1: Understand the cp -r Command
In Linux, the cp command is used to copy files. But by default, it doesn't copy directories. So we add the -r option, which stands for "recursive."
cp -r [source_directory] [destination]
This tells Linux: "Please copy everything in the source directory, including its contents, subdirectories, and files, into the destination."
Example 1: Copying a folder to your home directory
Let’s say we have a directory called project in the current folder, and we want to copy it to our home directory.
cp -r project ~/
This will create a copy of project inside your home folder.
Example 2: Copy a folder and rename it
What if you want to copy the directory but give the copy a different name?
cp -r project project_backup
This creates a new folder named project_backup that’s an exact copy of project.
Make recursive copies easier to verify
- Use
-v(verbose) to see what files are being copied:
cp -rv project project_backup
sudo.Practice copying a test directory tree
Create a test folder with some dummy files and try copying it around using different names and paths.
Copy complete directory trees with cp -r
- Use
cp -rto copy directories and their contents - You can rename the copy or move it to a different location
- Add
-vto see what’s happening
Try it, then check the worked answer
Your task: Copy term when term-backup does not exist.
Start in a new practice folder where neither term nor term-backup exists. The first two lines prepare a source directory with a student list. The destination is deliberately absent, which makes the resulting layout easy to follow.
Bash · complete example for this question
mkdir term
printf 'Diya\n' > term/students.txt
cp -R term term-backup
cat term-backup/students.txtExpected result
DiyaWorked answer
cp -R term term-backup
Use the recursive form when the source is a directory tree. A plain file-copy command does not express this traversal, while mv relocates the source rather than making an independent backup. Check a representative child file in the destination after the copy.
Check your decision
Predict the destination tree before copying
Recursive copying follows directory contents, but the destination still influences the layout. Know whether that destination exists, then check the resulting paths. If ownership, timestamps or links must be preserved for a real backup, review the appropriate preservation options rather than assuming every copy has identical metadata.
Reaching the recap and completing the quizzes are separate milestones.