AI Hints
Home›Linux Tutorial›Copy, Move, Remove
Linux · Lesson 25 of 120

cp: Copying

Files and folders.

⏱ 2 min read▶ Video at 30:19🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Copying files is one of the most common jobs in any operating system. In the terminal, it's the CP command. You tell it what to copy, and where to put the copy.

cp: Copying
From the video: cp: Copying
  • A very common job
  • cp copies
  • What, then where

Copy a file

CP takes two arguments. First the file you're copying, then the name of the new copy. CP notes dot TXT, notes backup dot TXT, makes an exact copy. LS shows both, and cat shows the copy has the same contents.

Terminal
cp notes.txt notes-backup.txt
ls
Documents  Downloads  Music  Pictures  notes-backup.txt  notes.txt  todo.txt
cat notes-backup.txt
Buy milk
Call Mum
Learn Linux

Copy into a folder

If the second argument is a folder, CP puts the copy inside it, with the same name. Here we copy to do dot TXT into Documents. You can also copy several files at once into a folder, by listing them all before the folder name.

Terminal
cp todo.txt Documents
ls Documents
cv.txt  report.txt  todo.txt
cp notes.txt todo.txt Downloads
ls Downloads
notes.txt  todo.txt

Copying a whole folder

Copying a folder needs one extra option. Without it, CP refuses, and says it's omitting the directory. Dash R means recursive. It copies the folder, and everything inside it, all the way down.

Terminal
cp Documents Documents-copy
cp: -r not specified; omitting directory 'Documents'
cp -r Documents Documents-copy
ls Documents-copy
cv.txt  report.txt

Summary

CP copies a file to a new name, or into a folder, and dash R copies whole folders. Next, moving and renaming.

Key takeaways
  • cp <from> <to>
  • cp files... <folder>
  • cp -r for folders
Next up: mv: Moving and Renaming — one command, two jobs