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:
| Sign | What 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
- Go to your practice folder and write a line to a file.
catshows what’s in it:cd ~/practiceecho "buy milk" > notes/todo.txtcat notes/todo.txt - Add a second line with two arrows, and read it again:
echo "call the bank" >> notes/todo.txtcat notes/todo.txt - 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
- Open your file:
nano notes/todo.txt - Move with the arrow keys (the mouse won’t move the cursor) and type a new line, such as
walk the dog. - Press Ctrl + O to save. It asks for the file name: press Enter to keep it.
- Press Ctrl + X to leave nano, then check your change with
cat notes/todo.txt.
Reading longer files
| Command | What it does |
|---|---|
cat file | Prints the whole file. Fine for short ones |
less file | Opens it one screen at a time. Arrow keys move, /word searches, q quits |
head file | Shows the first 10 lines. head -n 3 file shows 3 |
tail file | Shows 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
- Make a copy before you change something important. It’s a good habit:
cp notes/todo.txt notes/todo-backup.txt mvdoes two jobs. Give it a new name and it renames:mv notes/ideas.txt notes/plans.txt- Give it a folder and it moves:
mv notes/plans.txt photos/ls notes photos - 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.txtcp 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
-imakesrmask before each file. Typeyand press Enter to confirm, or anything else to cancel:rm -i notes/todo-backup.txt- A folder needs more. Try it, and read the error:
rm photosrm: cannot remove 'photos': Is a directory rmdiris the careful way, because it only removes empty folders:rmdir photosrmdir: failed to remove 'photos': Directory not emptyrm -rremoves 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/*.txtIf 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.
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.nanois a simple editor: Ctrl + O saves and Ctrl + X leaves.mvboth renames and moves.cp -rcopies folders.rmhas no recycle bin. Use-iwhen in doubt.- Check a wildcard with
lsbefore you use it withrm.
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?
>>, would have added to the end. It’s one of the most common ways to lose work in the terminal.2. How do you rename draft.txt to final.txt?
mv does renames as well as moves. cp would work, but it would leave you with two files.3. You’re about to run rm old*. What should you do first?
ls shows exactly what would be deleted. Linux won’t ask unless you use -i, and there’s no recycle bin afterwards.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.