0
0
Linux CLIscripting~10 mins

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

Choose your learning style9 modes available
Concept Flow - cp (copy files and directories)
Start: cp command
Check source exists?
NoError: 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.