Labs ICT
โญ Pro Login

Copy & Move

Files don't stay where you put them forever. You'll need to copy them to new locations, move them between directories, or rename them. Two commands cover all of this: cp for copying and mv for moving.

Copying files โ€” cp

cp takes two arguments: the source file and the destination.


$ cp notes.txt notes-backup.txt
$ ls
notes.txt  notes-backup.txt
    

Now you have two identical files. If you copy to a directory, the file keeps its name:


$ cp notes.txt Documents/
$ ls Documents/
notes.txt
    
Try it Yourself โ†’

Copying directories โ€” cp -r

Files are easy, but directories need the -r flag (recursive) to copy everything inside them.


$ cp -r projects/ projects-backup/
$ ls
projects  projects-backup
    

Without -r, cp will refuse to copy a directory. The -r tells it to walk through every subdirectory and file inside.

Moving and renaming โ€” mv

mv moves a file from one place to another. It also renames files โ€” because renaming is really just moving a file to a new name in the same directory.


$ mv notes.txt old-notes.txt
$ ls
old-notes.txt
    

Moving to a different directory works the same way:


$ mv old-notes.txt Documents/
$ ls Documents/
old-notes.txt
    

You can also move and rename in one step:


$ mv Documents/old-notes.txt ./notes.txt
    

That moves the file back to the current directory (.) and renames it.

What's the difference?

cp leaves the original file where it is and makes a duplicate. mv picks the file up and drops it somewhere else โ€” the original is gone. Think of cp as "copy-paste" and mv as "cut-paste."

One more thing: mv also works on directories without a special flag. mv projects/ old-projects/ renames the directory just like it would a file.

๐Ÿงช Quick Quiz

Which command moves or renames a file?