Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 13

File and Directory Management

Recommended resources

Hello! Welcome to your fifth lesson in the Linux Fundamentals module.

In our last session, we mastered the "GPS" of the Linux command line: using absolute and relative paths to reference any file or directory. You learned how to construct paths with special characters like /, ~, ., and .. and why it's efficient to use paths directly with commands.

Today, we'll put that knowledge into action. Now that you can point to any location, we'll learn the commands to manipulate the filesystem itself. This lesson directly addresses the learning outcome: Create, copy, move, and delete files and directories. These commands are the fundamental tools you'll use daily to organize your work.

We will cover five core commands:

  • mkdir - make directory
  • touch - create an empty file
  • cp - copy a file or directory
  • mv - move or rename a file or directory
  • rm - remove a file or directory

1. Creating Files and Directories

First, let's learn how to create the basic building blocks of your filesystem: directories to hold things and empty files to work with.

  • mkdir (make directory): This command creates a new directory. Its simplest form is mkdir directory_name.
  • touch: This command's primary purpose is to update a file's timestamp, but if the file doesn't exist, touch conveniently creates a new, empty file for you.

To see these commands in action and understand their basic usage, the following video provides a clear, step-by-step demonstration. The presenter shows the commands in the terminal and the result in a graphical file manager simultaneously, which is very helpful for visualizing what's happening.

Linux/Mac Terminal Tutorial: Create, Copy, Move, Rename and Delete Files and Directories

Watch this segment from a tutorial by Corey Schafer. It will walk you through creating a directory with mkdir and an empty file with touch.

Watch from 00:51 to 02:00. Pay attention to how mkdir creates a new folder and touch creates a new, empty file within it.

Creating Nested Directories with mkdir -p

What if you want to create a directory inside another directory that doesn't exist yet? For example, creating project/assets when project doesn't exist. Running mkdir project/assets would fail.

The -p (for parent) option tells mkdir to create any necessary parent directories along the way.

# This command creates 'project', then 'assets' inside it, then 'images' inside that.
mkdir -p project/assets/images 
Using `mkdir` Command in Linux Terminal
This terminal capture shows several examples of the `mkdir` command. Notice the use of `-p` to create nested directories and `-v` (verbose) to print a message for each directory created.

2. Copying, Moving, and Renaming

Once you have files and directories, you'll need to organize them. This is done with the cp (copy) and mv (move) commands.

  • cp (copy): Creates a duplicate of a file or directory.
    • cp source_file destination_file
  • mv (move): This command has a dual purpose:
    1. It moves a file or directory to a different location.
    2. It renames a file or directory if the destination is in the same location but has a new name.

The syntax for both is similar: command source destination. The video below demonstrates this beautifully. It shows how mv is used for both renaming and moving, and how cp creates a duplicate.

Linux/Mac Terminal Tutorial: Create, Copy, Move, Rename and Delete Files and Directories

Let's continue with Corey Schafer's video to see how cp and mv work.

Watch from 02:00 to 04:48. Notice the key difference: after cp, the original file still exists; after mv, it does not.

Working with Directories: The -r Flag

When you want to copy a directory, you must tell cp to operate recursively—that is, to copy the directory and everything inside it. This is done with the -r flag.

  • cp -r source_directory destination_directory

Interestingly, the mv command does not require a -r flag to move or rename a directory. It automatically moves the directory and all its contents.

This next segment explains this crucial distinction.

Linux/Mac Terminal Tutorial: Create, Copy, Move, Rename and Delete Files and Directories

This final segment from the same video explains how to copy and move entire directories, highlighting the required -r flag for cp.

Watch from 05:41 to 09:12. Pay close attention to the error you get when you try to cp a directory without -r, and how mv handles directories without needing a special flag.

Test your understanding!

You are in your home directory (~). You have a directory named project_alpha containing a file named report.txt.

Your goal is to have a new directory structure where you have a backup of the original project and a new working version. The final state should look like this:

~/
├── project_alpha_backup/
│ └── report.txt
└── project_beta/
└── draft_report.txt

What sequence of commands would you use to get from the initial state to the final state, without using cd?

Show answer

Here is one possible solution:

  1. cp -r project_alpha project_alpha_backup
    • This recursively copies the project_alpha directory and its contents to a new directory named project_alpha_backup.
  2. mv project_alpha project_beta
    • This renames the original project_alpha directory to project_beta.
  3. mv project_beta/report.txt project_beta/draft_report.txt
    • This renames the file inside the project_beta directory.

3. Deleting Files and Directories

The final fundamental operation is deletion, which is handled by the rm command.

A very important warning: In the Linux command line, there is no "Trash Can" or "Recycle Bin". When you use rm, the file is gone permanently. Always double-check your rm commands before pressing Enter.

  • rm (remove): Deletes a file.
    • rm file_name
  • rmdir (remove directory): Deletes an empty directory. It will fail if the directory contains any files or other directories.
  • rm -r: To delete a directory and all of its contents (recursively), you use the same -r flag we saw with cp.

The last video clip demonstrates these deletion commands and reiterates the warning about their permanence.

Linux/Mac Terminal Tutorial: Create, Copy, Move, Rename and Delete Files and Directories

This clip covers file deletion with rm, empty directory deletion with rmdir, and non-empty directory deletion with rm -r.

Watch from 04:48 to 05:41 and from 09:12 to 11:03. Pay very close attention to the warnings about the rm and rm -rf commands.

The -f (force) option can be combined with -r (rm -rf) to remove files and directories without any prompts, even if they are write-protected. This is one of the most powerful and potentially destructive commands in Linux. Use it with extreme caution.

Linux 'rm' Command Demonstration
This terminal capture shows the `rm` command in action. The `rm -vir` command is a safer way to delete, as it is verbose (`-v`), interactive (`-i`, prompting for confirmation), and recursive (`-r`). This contrasts with `rm -rf`, which deletes everything forcefully.

4. Summary and Reference

You've just learned the essential commands for file and directory management. To help you review, this webpage provides a concise summary table and more examples for each command.

Mkdir, touch, and rm

This webpage from CIS-106 Linux Fundamentals serves as an excellent quick reference. The table at the top is particularly useful for a quick review.

Review the 'Commands reference table' at the top of the page. It summarizes the syntax and purpose of all the commands we've discussed today. You can also skim the rest of the page for more detailed examples.


Conclusion

You now have the core skills to manage the filesystem from the command line. By combining these five commands (mkdir, touch, cp, mv, rm) with your knowledge of paths from the last lesson, you can create, organize, and clean up files and directories anywhere on the system.

Key Takeaways:

  • Create: mkdir for directories (use -p for parents) and touch for empty files.
  • Copy: cp for files. Use cp -r for directories.
  • Move/Rename: mv handles both moving and renaming for files and directories.
  • Delete: rm for files, rmdir for empty directories. Use rm -r to delete a directory and its contents.
  • Be Careful: The rm command is permanent. rm -rf is extremely powerful and should be used with great care.

In our next lesson, we will build directly on this foundation. Instead of manipulating one file at a time, you'll learn how to use file globbing patterns and wildcards to perform batch operations on many files at once—a huge time-saver