← Back to course

Files and Folders

Files and Folders

Welcome back.

In the previous lesson, you learned how to move around the file system.

Now it is time to start touching things.

Carefully.

Because the terminal can create files like a wizard, move them like a professional, and delete them like a tiny digital bulldozer with no emotional attachment.

What You’ll Learn

In this lesson, you’ll learn how to:

The Mission

Your mission is simple:

Create a small practice folder, add files inside it, copy them, rename them, move them, and delete them without causing a dramatic Linux opera.

Create a Practice Folder

First, go to your home directory:

cd

Now create a folder called terminal-practice:

mkdir terminal-practice

Enter it:

cd terminal-practice

Check where you are:

pwd

You should see something like:

/home/viktor/terminal-practice

Good. This is your safe playground.

If something goes wrong here, at least we are not destroying your real documents.

Very professional. Almost responsible.

Create an Empty File

To create an empty file, use touch.

touch notes.txt

Now list the folder:

ls

You should see:

notes.txt

The file exists.

It is empty, but it exists.

A bit like some meetings.

Create More Files

Create a few more files:

touch todo.txt ideas.txt commands.txt

Now list them:

ls

You should see something like:

commands.txt  ideas.txt  notes.txt  todo.txt

You have created files from the terminal.

No mouse. No file manager. No unnecessary clicking ceremony.

Create a Folder Inside a Folder

Now create a folder called backup:

mkdir backup

Check:

ls

You should see:

backup  commands.txt  ideas.txt  notes.txt  todo.txt

Folders help you organize files.

Without folders, your computer becomes a digital drawer full of cables, old receipts, and one mysterious USB stick.

Copy a File

To copy a file, use cp.

cp notes.txt backup/

This copies notes.txt into the backup folder.

Check inside backup:

ls backup

You should see:

notes.txt

The original file is still in the main folder, and the copy is inside backup.

Copy and Rename at the Same Time

You can copy a file and give the copy a new name:

cp todo.txt backup/todo-backup.txt

Check:

ls backup

Now you should see:

notes.txt  todo-backup.txt

This is useful when you want to keep an older version of a file.

Because sometimes “final.txt” becomes “final-final-real-final-v7.txt”.

We have all been there.

Rename a File

To rename a file, use mv.

mv ideas.txt project-ideas.txt

Now list files:

ls

You should see project-ideas.txt instead of ideas.txt.

In Linux, renaming is basically moving a file to a new name.

Simple. Slightly weird. Very Linux.

Move a File

Move commands.txt into the backup folder:

mv commands.txt backup/

Check the current folder:

ls

Then check the backup folder:

ls backup

The file moved.

It is no longer in the original place.

Unlike copying, moving does not leave the original behind.

Delete a File

To delete a file, use rm.

Create a file we can safely delete:

touch delete-me.txt

Now delete it:

rm delete-me.txt

Check:

ls

The file is gone.

Important: rm usually does not move files to a trash bin.

It deletes them.

Linux does not say: “Are you sure, sweetie?”

Linux says: “Done.”

This is why we respect rm.

Delete an Empty Folder

Create an empty folder:

mkdir empty-folder

Delete it with:

rmdir empty-folder

rmdir only works on empty folders.

If the folder contains files, Linux refuses.

For once, Linux protects you from yourself.

Enjoy this rare moment.

Delete a Folder with Files Inside

To delete a folder and everything inside it, you can use:

rm -r folder-name

But be careful.

rm -r means recursive delete.

It deletes the folder and its contents.

This is powerful.

This is also how people accidentally create sad stories.

For now, do not use it unless you are inside your practice folder and you know exactly what you are deleting.

Common Mistakes

Forgetting where you are

Before deleting anything, run:

pwd

This shows where you are.

If you are not inside your practice folder, stop and think.

The terminal rewards thinking. It punishes sleepy fingers.

Deleting the wrong file

This deletes one file:

rm notes.txt

This deletes all .txt files in the current folder:

rm *.txt

That star * is powerful.

Do not use it casually.

It is not decoration. It is a tiny command grenade.

Spaces in names

This may fail:

rm my file.txt

Use quotes:

rm "my file.txt"

Or better: while learning, avoid spaces in file names.

Use names like:

my-file.txt
my_file.txt
notes.txt

Your future self will thank you.

Practice

Inside terminal-practice, try this:

mkdir lesson3
cd lesson3
touch file1.txt file2.txt file3.txt
ls
mkdir backup
cp file1.txt backup/
mv file2.txt renamed-file2.txt
mv file3.txt backup/
ls
ls backup

Then answer:

  1. Which command creates a folder?
  2. Which command creates an empty file?
  3. Which command copies a file?
  4. Which command moves or renames a file?
  5. Which command deletes a file?

Mini Challenge

Create this structure using only the terminal:

terminal-practice/
└── project/
    ├── notes.txt
    ├── todo.txt
    └── backup/
        └── notes-backup.txt

Steps:

  1. Create a folder called project.
  2. Enter it.
  3. Create notes.txt and todo.txt.
  4. Create a folder called backup.
  5. Copy notes.txt into backup as notes-backup.txt.
  6. List everything and check your work.

No mouse.

The mouse is still on vacation from Lesson 2.

Summary

Today you learned:

You now know how to create and organize files from the terminal.

This is where the terminal starts feeling less like a scary black window and more like a tool.

A sharp tool.

Do not wave it around.

Next Lesson

In the next lesson, we’ll learn how to read files and search inside them using commands like cat, less, head, tail, and grep.

Because creating files is nice.

But reading them is where the detective work begins.