Copying, Moving, and Deleting#
Copying files#
Let’s say we want to make a copy (backup) of our thesis. We use the cp command for this.
This has the syntax cp origin destination.
Let’s start by just making a copy in our own directory. We can do this as:
cp thesis.txt thesis-backup.txt
If we do ls -l we should see both the original and copy present.
We could put the copy in a different location, like our home directory, by including the path in the destination, like:
cp thesis.txt ~/thesis-backup.txt
Tip
The source can be anywhere on the filesystem and the destination can be the
directory you are in by using . as the destination. E.g., to copy your
~/.bashrc file into your current directory, you can do:
cp ~/.bashrc .
Moving files#
Maybe we want to put the backup in a separate directory.
We could copy it there, but we already made a copy. Instead let’s move
our copy to a new directory called backup/ under our thesis/ directory.
Starting in ~/shell-lesson-data/exercise-data/writing/thesis/, we can do:
mkdir backup
mv thesis-backup.txt backup
Here we are using a relative path for the destination.
Note
We didn’t need to specify the filename in the destination part of
the mv—in this case, it will keep the filename the same, but
just move it into the new directory.
We could equivalently do:
mv thesis-backup.txt backup/thesis-backup.txt
We could also be more explicit and do:
mv thesis-backup.txt ./backup/thesis-backup.txt
Tip
Both cp and mv can copy/move multiple files to a
destination in a single command. In this case, the last argument
is used as the destination.
So to move both thesis-backup.txt and topics.txt, we could
do:
mv thesis-backup.txt topics.txt backup
Deleting files#
To delete files, we use the rm command (short for remove).
Let’s remove the empty file topics.txt:
rm topics.txt
Try it…
We also put a copy of thesis-backup.txt in our home directory. Remove that file.
To remove a directory, we need to first remove all the files in the directory, and then we can remove the directory using rmdir.
Let’s remove the backup/ directory. First go back to your thesis/ sub-directory. Then:
rm backup/thesis-backup.txt
rmdir backup
Tip
We can do a recursive rm to remove all the contents of a directory and the directory itself, by doing:
rm -r backup
Caution
Linux does not have an “undelete” function. When you remove a file
on the command line it is gone for good. Therefore you should
always double check the rm command you typed before hitting Enter.
Another good practice is to use rm in interactive mode, where
it will prompt you before removing a file (and cp and mv
can be used this way too).
To make this the default, add the following lines to your .bashrc:
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
Summary#
We learned the following commands:
mv: move a file or directorycp: copy a file or directoryrm: remove a file (and/or directory if done recursively)