Creating Files and Directories

LinuxBeginner
Practice Now

Introduction

In previous labs, you explored files that already existed. Now it is time to become a creator.

This is an important shift. A filesystem is not just something you look at; it is also something you shape. Creating files and directories means you are starting to organize information deliberately instead of only observing it.

In this lab, you will learn how to:

  1. Create new empty files.
  2. Build folders (directories) to organize your work.
  3. Construct entire directory paths in a single command.

You will become the architect of your own file system.

Create New Files

How do you create a file? In Linux, the easiest way is with a command called touch.

Originally designed to simply update a file's timestamp ("touching" it), touch has a very useful feature: If the file doesn't exist, it creates it. It creates an empty file—a blank page waiting for content.

That historical detail is useful because it explains the command's name. touch was not originally a "create file" command in the human sense; it was a way to change file times. Creating an empty file as a side effect became one of its most practical everyday uses.

Let's create a file named file1.txt.

touch file1.txt

If the command succeeds, it will print nothing. Remember the Golden Rule of Linux: No news is good news.

Let's verify our work by listing the files:

ls -l

You should see file1.txt. Look at the size column (usually the 5th column). It should say 0. This confirms you have created a perfectly empty file.

Create Directories

Files need a place to live. To keep things organized, we use folders, which in Linux are called Directories.

Directory structure matters more than many beginners expect. As systems grow, a clear directory layout helps humans find things quickly and helps scripts work predictably. Good organization is part of system administration, not just personal neatness.

We create them using the mkdir command (short for Make Directory).

Let's build a new directory called project_alpha:

mkdir project_alpha

Again, silence means success. Let's verify it exists using ls -F. The -F option adds a slash / to directory names, making them easy to spot.

ls -F

You should now see project_alpha/. You have successfully built a new room in your file system.

Create Nested Directories

What if you need to build a folder inside a folder inside a folder? For example: logs/labexio/jan (A January folder, inside a LabexIO folder, inside a Logs folder).

If you try mkdir logs/labexio/jan, Linux will complain because the intermediate folders don't exist yet.

You could make them one by one, but that is tedious.

Instead, we use the -p (parents) flag. This tells mkdir: "Please create any parent directories that are missing."

This is a good example of Linux tools being efficient once you know the right option. The command is not just creating the final directory; it is building the necessary path for you.

mkdir -p logs/labexio/jan

Let's check our work using the recursive list command:

ls -R logs

You should see the entire nested structure created instantly.

Summary

You are now a creator!

  • touch filename: Creates an empty file (or updates the timestamp of an existing one).
  • mkdir directoryname: Creates a new directory.
  • mkdir -p path/to/directory: Creates a full path of directories, building any missing parents automatically.

Now that you can create files and folders, the next logical step is learning how to duplicate them and move them around.