Lesson 3 of 725 minutesHands-on

Create, move and delete files safely

This is the lesson where the terminal gets real power, and the one with a real risk attached: when Linux deletes a file, it’s gone. There’s no recycle bin to rescue you, and no “are you sure?” unless you ask for one. You’ll write and edit files in your practice folder, then learn the small habit that prevents nearly every accident.

In this lesson

  • Write to a file from the command line, and edit it in nano
  • Copy, move and rename files and folders
  • Delete safely, and use wildcards without getting hurt

You’ll need: the practice folder from Lesson 2. If you skipped it, run mkdir -p ~/practice/notes ~/practice/photos first.

Put some words in a file

Commands normally print to the screen. The > sign sends that output into a file instead. There are two versions, and the difference matters:

SignWhat it does
>Replaces the whole file with the output, with no warning
>>Adds the output to the end of the file

Try it yourself ยท part 1

Write, add and read

  1. Go to your practice folder and write a line to a file. cat shows what’s in it:
    cd ~/practice
    echo "buy milk" > notes/todo.txt
    cat notes/todo.txt
  2. Add a second line with two arrows, and read it again:
    echo "call the bank" >> notes/todo.txt
    cat notes/todo.txt
  3. You should see both lines:
    buy milk
    call the bank

Edit a file with nano

For anything longer than a line, use an editor. nano runs inside the terminal and lists its shortcuts along the bottom of the screen, where ^ means the Ctrl key.

Try it yourself ยท part 2

Edit your list

  1. Open your file:
    nano notes/todo.txt
  2. Move with the arrow keys (the mouse won’t move the cursor) and type a new line, such as walk the dog.
  3. Press Ctrl + O to save. It asks for the file name: press Enter to keep it.
  4. Press Ctrl + X to leave nano, then check your change with cat notes/todo.txt.

Reading longer files

CommandWhat it does
cat filePrints the whole file. Fine for short ones
less fileOpens it one screen at a time. Arrow keys move, /word searches, q quits
head fileShows the first 10 lines. head -n 3 file shows 3
tail fileShows the last 10 lines, which is where the newest log entries are

Try less /etc/services, a long list of network services by port number. Type /https and press Enter to jump to it, then press q to get out.

Try it yourself ยท part 3

Copy, rename and move

  1. Make a copy before you change something important. It’s a good habit:
    cp notes/todo.txt notes/todo-backup.txt
  2. mv does two jobs. Give it a new name and it renames:
    mv notes/ideas.txt notes/plans.txt
  3. Give it a folder and it moves:
    mv notes/plans.txt photos/
    ls notes photos
  4. To copy a whole folder, add -r (“recursive”, meaning everything inside it too):
    cp -r notes notes-copy
notes:
todo-backup.txt  todo.txt

photos:
plans.txt
Quiet overwriting. If the destination already exists, cp and mv replace it without asking, and the old file is gone. Add -i (“interactive”), as in mv -i old.txt new.txt, and they ask first.

Deleting: no recycle bin

rm removes a file straight away. It doesn’t move it anywhere you can get it back from. So there are two habits worth building from day one.

Try it yourself ยท part 4

Delete with a safety catch

  1. -i makes rm ask before each file. Type y and press Enter to confirm, or anything else to cancel:
    rm -i notes/todo-backup.txt
  2. A folder needs more. Try it, and read the error:
    rm photos
    rm: cannot remove 'photos': Is a directory
  3. rmdir is the careful way, because it only removes empty folders:
    rmdir photos
    rmdir: failed to remove 'photos': Directory not empty
  4. rm -r removes a folder and everything in it. Remove the copy you made:
    rm -r notes-copy

Wildcards, and the habit that saves you

A * matches any text in a file name, so *.txt means “every name ending in .txt”. That makes one command work on hundreds of files, which is exactly why it can do a lot of damage quickly. Here’s the habit: before you delete with a wildcard, run the same pattern with ls and read the list.

ls notes/*.txt

If that list is what you meant, then swap ls for rm. A single stray space matters here too: rm *.txt deletes the text files, while rm * .txt deletes everything in the folder, and then complains it can’t find .txt.

The one to never run. You’ll see rm -rf online: it deletes a folder and everything inside it, without asking anything. It has real uses, but never run it with a path you haven’t read character by character, and especially not with sudo in front, which you’ll meet in Lesson 5. For irreplaceable files, keep a real backup. Lesson 7 has you build one.

Key takeaways

  • > replaces a file and >> adds to it. Mixing them up loses data.
  • nano is a simple editor: Ctrl + O saves and Ctrl + X leaves.
  • mv both renames and moves. cp -r copies folders.
  • rm has no recycle bin. Use -i when in doubt.
  • Check a wildcard with ls before you use it with rm.

Check yourself

Three questions. Pick one answer each and you’ll see why.

1. You have a long notes file and you run echo "new idea" > notes.txt. What’s in the file now?

2. How do you rename draft.txt to final.txt?

3. You’re about to run rm old*. What should you do first?

Next you’ll combine small commands into something much more powerful, and use it to investigate a web server log.

This lesson is part of the free course Linux and the Command Line, licensed under CC BY-NC-SA 4.0: you may share and adapt it for non-commercial use, with credit to ExamineIP and a link to the original.

Scroll to Top