cp (copy files and directories) in Linux CLI - Time & Space Complexity
When copying files or directories, it is important to understand how the time taken grows as the amount of data increases.
We want to know how the copy command's work changes when copying more or larger files.
Analyze the time complexity of the following copy command.
cp -r source_dir/ destination_dir/
This command copies all files and folders inside source_dir recursively into destination_dir.
Look at what repeats during the copy process.
- Primary operation: Reading and writing each file's data.
- How many times: Once for every file and folder inside the source directory, including nested ones.
The time to copy grows as the total number of files and their sizes increase.
| Input Size (n files) | Approx. Operations |
|---|---|
| 10 | Copy 10 files, reading and writing each once. |
| 100 | Copy 100 files, about 10 times more work than 10 files. |
| 1000 | Copy 1000 files, about 100 times more work than 10 files. |
Pattern observation: The work grows roughly in direct proportion to the number of files and their sizes.
Time Complexity: O(n)
This means the time to copy grows linearly with the number of files and their total size.
[X] Wrong: "Copying a directory is always fast because it just copies folder names."
[OK] Correct: The command copies every file inside, not just folder names, so the time depends on all file sizes and counts.
Understanding how copying scales helps you reason about file operations in scripts and automation, a useful skill in many real-world tasks.
"What if we copy only a single large file instead of many small files? How would the time complexity change?"
Practice
cp command do in Linux?Solution
Step 1: Understand the purpose of
Thecpcpcommand is used to copy files or directories, not to delete or move them.Step 2: Compare with other commands
Deleting is done byrm, moving bymv, and listing byls. Socpcopies files or directories.Final Answer:
Copies files or directories from one location to another -> Option BQuick Check:
cp= copy files/directories [OK]
- Confusing cp with mv (move)
- Thinking cp deletes files
- Mixing cp with ls (list)
docs and all its contents to backup?Solution
Step 1: Identify the option to copy directories recursively
The-roption tellscpto copy directories and their contents recursively.Step 2: Check other options
-dis for preserving links,-fforces overwrite but doesn't copy directories recursively, and no option copies only files.Final Answer:
cp -r docs backup -> Option AQuick Check:
Use-rto copy folders [OK]
- Omitting -r when copying directories
- Using -d or -f incorrectly
- Assuming cp copies folders without options
file1.txt exists and file2.txt also exists?cp file1.txt file2.txt cat file2.txt
Solution
Step 1: Understand default behavior of cp
By default,cpoverwrites the destination file if it exists without warning.Step 2: Analyze the commands
cp file1.txt file2.txtcopies file1.txt over file2.txt, replacing its contents. Thencat file2.txtshows the new contents (from file1.txt).Final Answer:
Contents of file1.txt will overwrite file2.txt, then displayed -> Option DQuick Check:
cp overwrites files by default [OK]
- Expecting error if destination exists
- Thinking files merge on copy
- Assuming cp refuses overwrite
cp -r myfolder backup/ but get an error: cp: omitting directory 'myfolder'. What is the likely cause?Solution
Step 1: Understand the error message
The error 'omitting directory' happens whencptries to copy a directory without the recursive-roption.Step 2: Check the command given
The question says you rancp -r myfolder backup/but error suggests-rwas missing or command was different.Final Answer:
You forgot the -r option to copy directories -> Option CQuick Check:
Missing -r causes 'omitting directory' error [OK]
- Assuming destination folder missing causes this error
- Typing -r incorrectly but not noticing
- Ignoring error message meaning
project/ to backup/, but only if the files in backup/ are older than those in project/. Which command achieves this?Solution
Step 1: Understand the options
-rcopies directories recursively,-ucopies only when source is newer than destination,-fforces overwrite,-iprompts before overwrite.Step 2: Choose the correct combination
To copy directories and update only newer files, use-ru. This copies recursively and updates only if source is newer.Final Answer:
cp -ru project/ backup/ -> Option AQuick Check:
Use -ru to copy recursively and update only newer files [OK]
- Using -r alone copies all files regardless of age
- Using -f overwrites without checking timestamps
- Using -i prompts but doesn't check file age
Ready to prove it?
Pass the test and this pattern moves unknown
