Shell Copying Files
Copying a file makes a second file with the same words in it. The first file stays where it was, unchanged.
Copy a file
cp copies notes.txt to a new file called notes-copy.txt.
cp notes.txt notes-copy.txt
cat notes-copy.txtThe first file stays put
The copy is new, and the first file is still there too.
cp notes.txt notes-copy.txt
ls notes.txt
ls notes-copy.txtCopy a folder
cp -r copies the whole docs folder to docs-copy.
cp -r docs docs-copy
ls docs-copyThe copy is separate
New words go in the copy only, and notes.txt stays the same.
cp notes.txt notes-copy.txt
echo "New line" > notes-copy.txt
cat notes.txt
cat notes-copy.txtExample
cp report.txt report-copy.txt
cp -r photos photos-copy
ls report-copy.txt
ls photos-copyKey points
- cp: a command that copies a file.
- Use cp to copy a file, then give the copy a new name.
- Use cp -r to copy a whole folder and all the files in it.
- The copy is a separate file, so changing one does not change the other.
In this example, cp copies report.txt to report-copy.txt. Then cp -r copies the whole photos folder to photos-copy. The two ls commands show the copied file and the files in the copied folder.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which command copies a folder and all its files?