Chapter 3: Copying, Moving, Removing

Moving and Removing

face Robert Craven

The command mv works very similarly to cp, and has a similar range of options. The main difference is that, unlike cp, it removes the original file—the data is moved to a new location and nothing remains of the original.

For instance:

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
username@MACHINE:notes$ mv meetings.txt old_meetings.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 old_meetings.txt
-rw------- 1 username mai    0 Aug 22 13:01 thing.txt
-rw------- 1 username mai    0 Aug 22 13:01 todo.txt

Removing files

To remove files or directories, use rm.

There’s a lot of clutter in /homes/username/modules/shell/notes—let’s clean up.

username@MACHINE:notes$ rm t* commands_backup.txt
rm: remove regular empty file 'thing.txt'? y
rm: remove regular empty file 'todo.txt'? y
rm: remove regular empty file 'commands_backup.txt'? y
username@MACHINE:notes$ rm -Rf backup_of_backups/
username@MACHINE:notes$ ls -l
total 4
drwx------ 2 username mai 4096 Aug 21 13:20 backups
-rw------- 1 username mai    0 Aug 21 11:32 commands.txt
-rw------- 1 username mai    0 Aug 21 13:59 old_meetings.txt
username@MACHINE:notes$

Notice how we used the -R option to remove the backup_of_backups directory, and all its contents, recursively.

CAUTION!

The -f flag, with rm, will remove files and directories without checking first if you wish to proceed. This makes rm -f—and especially, rm -Rf—a dangerous beast to let loose.

If you’re not careful, you will delete entire directories and subdirectories of important files! (And Linux will not put files deleted using rm in the recycle bin—once they are removed, they vanish!)

Exercise 8

  1. Make a copy of old_meetings.txt in backups.
  2. Move to the parent directory (~/modules/shell/), and rename notes as misc using mv.
  3. Now, without changing directories, remove the backups directory and all its contents.