Copying, moving and deleting safely

Video 105 of 140 in Virtualization Foundations, in the unit “The shell and the filesystem tree”. 2:49, with English and Hindi audio.

Copying, moving and deleting are the jobs you will do most often on a server, and each one changes the disk. Some of those changes cannot be undone from the shell itself. A wildcard can also reach more files than you meant. This article explains the risk in cp, mv and rm, shows how to preview a delete before it runs, and lists the habits that prevent most damage.

What you will learn

Three everyday jobs and the risk in each

Moving a file to another folder on the same disk is really a rename, so the old name disappears at once. Read the target path twice before you press Enter, because one wrong letter can point at the wrong folder.

Preview a delete before you run it

Practise in a scratch folder. Start by making two empty log files.

Make two empty log files to practise on
touch a.log b.log

Next, preview the delete. The echo command prints the command the shell would run, with the star pattern already expanded. Nothing is deleted yet.

Print the delete command without running it
echo rm *.log

The interactive option of rm asks before each deletion, so you can answer for each file.

Ask before removing one file
rm -i a.log

Note: Answer n to keep the file, or y to delete it for good.

Habits that prevent most damage

The copy and move commands also have an interactive option. It asks before overwriting an existing file.

One wildcard can reach the wrong files

A delete command with a wildcard removes whatever the pattern catches, and it runs at once. If the pattern is wider than you meant, the files it reaches are gone. Imagine old logs stored beside a folder of customer data. A single wrong name can reach the wrong place, and the shell does not ask.

Note: The checks cost only seconds: preview the list, ask before each change, and check the path.

Try it yourself

Use a scratch folder that holds no real data. You will make two empty log files, preview a delete, and then remove one file only after the shell asks you.

  1. Make two empty log files to practise on.

    touch a.log b.log
  2. Print the delete command without running it. The pattern is expanded, and nothing is deleted.

    echo rm *.log

    What you should see: The shell prints: rm a.log b.log

  3. Remove one file, but ask first, so the shell waits for your answer.

    rm -i a.log

    What you should see: The shell asks: rm: remove regular file 'a.log'? Answer n to keep the file, or y to delete it for good.

Terms

Wildcard
A pattern such as *.log that the shell expands into the names of the matching files.
Interactive option
A switch, such as the one for rm, that makes the command ask before each change.

Check yourself

Why does moving a file to another folder on the same disk remove the old name at once?

On the same disk the move is really a rename, so the old name disappears at once, and nothing is left behind under it.

What does echo rm *.log do?

It prints the command the shell would run, with the pattern expanded to the matching names. It deletes nothing.

Which option stops cp from silently overwriting a file?

The interactive option. It asks before it overwrites an existing file, so you can answer no.

Previous: Files, hard links and symbolic links. Next: Finding files and programs.