Shell and DoC Systems, Lab 1
The Linux Terminal and Scientia
Chapter 3: Copying, Moving, Removing
Copy files and directories
To copy files or directories, we can use cp.
First, make sure you’re in ~/modules/shell/notes. Let’s make a backup
of the (empty) file, commands.txt, called commands_backup.txt:
username@MACHINE:notes$ cp commands.txt commands_backup.txt
username@MACHINE:notes$ ls -l
total 0
-rw-r--r-- 1 username mai 0 Aug 21 11:32 commands_backup.txt
-rw-r--r-- 1 username mai 0 Aug 21 11:36 commands.txt
The basic syntax of cp is: cp <ORIGINAL FILE> <COPY OF ORIGINAL>.
In addition, the second argument can be an existing directory. In that case, the file would be copied to within the directory in question, with the same filename.
username@MACHINE:notes$ mkdir backups
username@MACHINE:notes$ cp commands.txt backups/
username@MACHINE:notes$ ls -l backups/
total 0
-rw-r--r-- 1 username mai 0 Aug 21 11:45 commands.txt
If you wish to copy an entire directory, with its contents, then
use the -R flag. This copies the directory recursively—its
subdirectories, subsubdirectories, and all files therein, etc.
username@MACHINE:notes$ cp -R backups backup_of_backups
username@MACHINE:notes$ ls -l
total 8
drwx------ 2 username mai 4096 Aug 21 13:23 backup_of_backups
drwx------ 2 username mai 4096 Aug 21 13:20 backups
-rw------- 1 username mai 0 Aug 21 11:32 commands_backup.txt
-rw------- 1 username mai 0 Aug 21 11:32 commands.txt
username@MACHINE:notes$ ls -l backup_of_backups
total 0
-rw------- 1 username mai 0 Aug 21 13:23 commands.txt
Exercise 7
- Make a new file,
meetings.txt, innotes. - Make a copy of this in
backups/.
Wildcards
Many shell commands take a list of arguments. For instance, we could do:
username@MACHINE:notes$ touch thing.txt todo.txt
username@MACHINE:notes$ ls -l
total 8
drwx------ 2 username mai 4096 Aug 21 13:23 backup_of_backups
drwx------ 2 username mai 4096 Aug 21 13:20 backups
-rw------- 1 username mai 0 Aug 21 11:32 commands_backup.txt
-rw------- 1 username mai 0 Aug 21 11:32 commands.txt
-rw------- 1 username mai 0 Aug 21 13:59 meetings.txt
-rw------- 1 username mai 0 Aug 22 13:01 thing.txt
-rw------- 1 username mai 0 Aug 22 13:01 todo.txt
It would be possible to write:
username@MACHINE:notes$ cp thing.txt todo.txt backups/
This would copy the first two files into backup. However, it’s shorter to
use a wildcard:
username@MACHINE:notes$ cp t* backups/
This will copy any file beginning with a t to the directory backups/.
Similarly, if we wanted to copy all files with a .txt extension, we could
write:
username@MACHINE:notes$ cp *.txt backups/
For much more about wildcards, see here.
Interactive copy
If there’s already a file in the place to which you’re copying, that
file will be overwritten—you’ll lose the data! Therefore you need to
be very careful with using cp. To make cp query you, interactively,
about whether to overwrite files, then use cp -i:
username@MACHINE:notes$ cp -i thing.txt todo.txt
cp: overwrite 'todo.txt?'
At this point, you can either answer y or n (with Enter).