Bird
Raised Fist0
Linux CLIscripting~10 mins

cp (copy files and directories) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - cp (copy files and directories)
Start: cp command
↓
Check source exists?
No→Error: source not found
Yes↓
Is source a file or directory?
↓
Copy file
↓
Place copy at destination
↓
End: copy complete
The cp command checks if the source exists, then copies files directly or directories recursively to the destination.
Execution Sample
Linux CLI
cp file1.txt backup/
cp -r folder1 backup_folder/
Copies a file to a folder and copies a directory recursively to another folder.
Execution Table
StepCommandSource Exists?Source TypeActionOutput
1cp file1.txt backup/YesFileCopy file1.txt to backup/file1.txt copied to backup/
2cp -r folder1 backup_folder/YesDirectoryCopy folder1 recursively to backup_folder/folder1 and contents copied to backup_folder/
3cp file2.txt backup/No-Errorcp: cannot stat 'file2.txt': No such file or directory
💡 Execution stops when all commands run or an error occurs if source does not exist.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
file1.txt in backup/NoYesYesYes
folder1 in backup_folder/NoNoYesYes
file2.txt existsNoNoNoNo
Key Moments - 2 Insights
Why does cp need the -r option for directories?
Because cp copies files by default. To copy directories and their contents, -r tells cp to copy recursively, as shown in step 2 of the execution_table.
What happens if the source file does not exist?
cp shows an error and stops copying that file, as seen in step 3 of the execution_table where file2.txt is missing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of step 1?
Afile1.txt copied to backup/
Bfolder1 and contents copied to backup_folder/
Ccp: cannot stat 'file2.txt': No such file or directory
DNo output
💡 Hint
Check the Output column in row for step 1 in execution_table.
At which step does cp copy a directory recursively?
AStep 1
BStep 2
CStep 3
DNone
💡 Hint
Look at the Source Type and Action columns in execution_table.
If file1.txt did not exist, what would happen at step 1?
Acp copies folder1 instead
Bcp copies file1.txt anyway
Ccp shows an error and stops copying file1.txt
Dcp creates an empty file1.txt
💡 Hint
Refer to step 3 in execution_table where missing file causes error.
Concept Snapshot
cp command copies files or directories.
Use cp source destination to copy files.
Add -r option to copy directories recursively.
If source missing, cp shows error.
Destination can be a folder or new file name.
Full Transcript
The cp command copies files or directories from a source to a destination. It first checks if the source exists. If the source is a file, it copies it directly. If the source is a directory, cp needs the -r option to copy it recursively, including all contents. If the source does not exist, cp shows an error and stops copying that item. For example, copying file1.txt to a backup folder copies the file. Copying folder1 with -r copies the whole directory and its contents. Trying to copy a missing file shows an error message. This helps keep your files safe by making copies where you want them.

Practice

(1/5)
1. What does the cp command do in Linux?
easy
A. Deletes files or directories permanently
B. Copies files or directories from one location to another
C. Moves files or directories to a new location
D. Lists files and directories in the current folder

Solution

  1. Step 1: Understand the purpose of cp

    The cp command is used to copy files or directories, not to delete or move them.
  2. Step 2: Compare with other commands

    Deleting is done by rm, moving by mv, and listing by ls. So cp copies files or directories.
  3. Final Answer:

    Copies files or directories from one location to another -> Option B
  4. Quick Check:

    cp = copy files/directories [OK]
Hint: Remember: cp = copy files or folders [OK]
Common Mistakes:
  • Confusing cp with mv (move)
  • Thinking cp deletes files
  • Mixing cp with ls (list)
2. Which of the following is the correct syntax to copy a directory named docs and all its contents to backup?
easy
A. cp -r docs backup
B. cp -d docs backup
C. cp docs backup
D. cp -f docs backup

Solution

  1. Step 1: Identify the option to copy directories recursively

    The -r option tells cp to copy directories and their contents recursively.
  2. Step 2: Check other options

    -d is for preserving links, -f forces overwrite but doesn't copy directories recursively, and no option copies only files.
  3. Final Answer:

    cp -r docs backup -> Option A
  4. Quick Check:

    Use -r to copy folders [OK]
Hint: Use -r to copy folders and contents [OK]
Common Mistakes:
  • Omitting -r when copying directories
  • Using -d or -f incorrectly
  • Assuming cp copies folders without options
3. What will be the output of the following commands if file1.txt exists and file2.txt also exists?
cp file1.txt file2.txt
cat file2.txt
medium
A. Both files will be merged and displayed
B. Error: file2.txt already exists, copy fails
C. file2.txt remains unchanged, contents of file1.txt not copied
D. Contents of file1.txt will overwrite file2.txt, then displayed

Solution

  1. Step 1: Understand default behavior of cp

    By default, cp overwrites the destination file if it exists without warning.
  2. Step 2: Analyze the commands

    cp file1.txt file2.txt copies file1.txt over file2.txt, replacing its contents. Then cat file2.txt shows the new contents (from file1.txt).
  3. Final Answer:

    Contents of file1.txt will overwrite file2.txt, then displayed -> Option D
  4. Quick Check:

    cp overwrites files by default [OK]
Hint: cp overwrites existing files unless told otherwise [OK]
Common Mistakes:
  • Expecting error if destination exists
  • Thinking files merge on copy
  • Assuming cp refuses overwrite
4. You run cp -r myfolder backup/ but get an error: cp: omitting directory 'myfolder'. What is the likely cause?
medium
A. You used -r but the command was typed incorrectly
B. The destination folder 'backup/' does not exist
C. You forgot the -r option to copy directories
D. You ran cp without -r option on a directory

Solution

  1. Step 1: Understand the error message

    The error 'omitting directory' happens when cp tries to copy a directory without the recursive -r option.
  2. Step 2: Check the command given

    The question says you ran cp -r myfolder backup/ but error suggests -r was missing or command was different.
  3. Final Answer:

    You forgot the -r option to copy directories -> Option C
  4. Quick Check:

    Missing -r causes 'omitting directory' error [OK]
Hint: Always use -r to copy directories [OK]
Common Mistakes:
  • Assuming destination folder missing causes this error
  • Typing -r incorrectly but not noticing
  • Ignoring error message meaning
5. You want to copy all files and folders from project/ to backup/, but only if the files in backup/ are older than those in project/. Which command achieves this?
hard
A. cp -ru project/ backup/
B. cp -r project/ backup/
C. cp -rf project/ backup/
D. cp -ri project/ backup/

Solution

  1. Step 1: Understand the options

    -r copies directories recursively, -u copies only when source is newer than destination, -f forces overwrite, -i prompts before overwrite.
  2. 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.
  3. Final Answer:

    cp -ru project/ backup/ -> Option A
  4. Quick Check:

    Use -ru to copy recursively and update only newer files [OK]
Hint: Use -ru to copy folders and update only newer files [OK]
Common Mistakes:
  • Using -r alone copies all files regardless of age
  • Using -f overwrites without checking timestamps
  • Using -i prompts but doesn't check file age
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard