Skip to main content

Welcome to a Grasp lesson

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

Lesson 21

Command Line File Management

Recommended resources

Hello! Let's get started with your next lesson.

In our last session, we mapped out the Linux filesystem, understanding the purpose of critical directories like /etc, /var, and /home. You now have the "blueprint" of your server. Today, we'll pick up the "tools" you need to build, rearrange, and clear out structures within that blueprint. This is where your practical skills will start to take shape.

The learning outcome for this lesson is to perform essential file operations from the command line: create, copy, move, rename, and delete files and directories. These commands are the absolute bedrock of Linux system administration. You will use them every single day to manage configuration files, organize data, deploy applications, and troubleshoot issues.

1. A Practical Video Guide

We'll begin with a practice-first approach. The best way to learn these commands is to see them in action and try them yourself. Fire up your Ubuntu Server VM and log in. As you watch the following video, I encourage you to type the commands into your own terminal and observe the results.

Basic Linux Navigation

This video, 'Basic Linux Navigation' by Nathan Backman, provides an excellent, hands-on demonstration of the core commands for file and directory manipulation. It follows a simple, practical scenario of organizing course folders.

Watch the video from 01:37 to 02:30 (creating directories), 05:50 to 08:13 (deleting, copying, and navigating), and 08:25 to 11:54 (moving, renaming, and deleting directories). The video also mentions the nano text editor, which you can ignore for now—we will cover it in detail in our next lesson.


2. Reinforcing the Concepts

The video gave you a great whirlwind tour. Now, let's slow down and solidify your understanding of each command and its most important options. We'll use a text-based resource for a more structured breakdown. This is the kind of material you might review before a job interview to ensure you can explain not just how to use a command, but why you'd choose a specific option.

Creating, Moving and Deleting Files - LPI

The Linux Professional Institute (LPI) provides concise, professional-grade learning materials. We'll use their guide to review the commands and explore some crucial options the video touched upon.

Read through the sections on 'Creating Directories', 'Creating Files', 'Renaming Files', 'Moving Files', 'Deleting Files and Directories', and 'Copying Files and Directories'. Pay close attention to the command options like mkdir -p, mv -i, and rm -r.

Key Command Summary

Let's pull together the key commands from the video and the reading.

Creating

  • mkdir [directory_name]: Creates a new directory.
  • mkdir -p [parent_dir]/[child_dir]: The -p (parents) option is extremely useful. It creates the entire directory path if it doesn't already exist.
  • touch [file_name]: Creates a new, empty file. If the file already exists, it updates the file's modification time. This is often used to create placeholder log files or to simply create a file that another program will write to.
Using the 'touch' Command to Create Files in Linux
This terminal output shows the `touch` command being used to create several files within a directory structure, and the `tree` command is then used to display the resulting hierarchy, confirming the files' creation.

Copying

  • cp [source_file] [destination_file]: Copies a file. A common use is creating a backup before editing: cp config.txt config.txt.bak.
  • cp [source_file] [destination_directory]: Copies a file into a directory.
  • cp -r [source_directory] [destination_directory]: The -r (recursive) option is required to copy a directory and all of its contents.

Moving and Renaming

The mv command does double duty.

  • To rename: mv [old_name] [new_name]. This "moves" the file to the same location but with a new name.
  • To move: mv [source_file] [destination_directory]. This moves a file or directory to a new location.
  • You can also move and rename at the same time: mv source.txt /path/to/destination/dest.txt.

Deleting

Warning: There is no "Recycle Bin" on the Linux command line. Once a file is deleted with rm, it is gone. Always double-check your rm commands.

  • rmdir [directory_name]: Removes an empty directory. It will return an error if the directory contains any files.
  • rm [file_name]: Removes (deletes) a file.
  • rm -r [directory_name]: Use with caution. The -r (recursive) option deletes a directory and everything inside it.
  • rm -i [file_or_directory]: The -i (interactive) option prompts you for confirmation before every deletion. This is a great safety feature to use until you are very confident.
Linux Command Line File Operations: rm, touch, and ls
This terminal output demonstrates using `touch` with a wildcard to create several files, listing them with `ls`, and then using `rm` with a wildcard to delete them.

3. Practical Application: Your First Sysadmin Task

Now it's time to put your "katana" to work. Imagine you've been given a ticket to set up the directory structure for a new web service.

In your home directory (~), follow these steps using the commands we've just learned.

  1. Create the directory structure: The new service, webapp, needs a home. Create the following directory path in one command: services/webapp/config, services/webapp/logs, and services/webapp/data.

    Hint `mkdir -p services/webapp/config services/webapp/logs services/webapp/data`

  2. Stage initial files: Create an empty log file named webapp.log inside the logs directory.

    Hint `touch services/webapp/logs/webapp.log`

  3. Deploy the configuration: A default configuration file has been placed for you. First, create this dummy file: echo "DEFAULT_CONFIG_VERSION=1.0" > /tmp/default.conf. Now, copy this file into your config directory and name it webapp.conf.

    Hint `cp /tmp/default.conf services/webapp/config/webapp.conf`

  4. Correct a mistake: You've just been told the default config is outdated.

    • First, rename the existing webapp.conf to webapp.conf.old as a backup.
    • Next, create the "new" config file: echo "FINAL_CONFIG_VERSION=1.2" > /tmp/final.conf.
    • Finally, move the new final.conf into your config directory, naming it webapp.conf.
    Hint `mv services/webapp/config/webapp.conf services/webapp/config/webapp.conf.old` `mv /tmp/final.conf services/webapp/config/webapp.conf`

  5. Verify your work: Use the tree services command to view the final structure. It should look like this:

    services/
    └── webapp
        ├── config
        │   ├── webapp.conf
        │   └── webapp.conf.old
        ├── data
        └── logs
            └── webapp.log
    

  6. Decommission the service: The test is over. Clean up everything by deleting the entire services directory and all its contents in a single command. Be careful!

    Hint `rm -r services`

4. Working Smarter: Wildcards

Typing every single filename is tedious and error-prone. To work efficiently, you can use special characters called wildcards to create patterns that match multiple files.

Simple exercises for learning basic Unix/Linux commands

This document introduces the most common wildcards. Understanding these is essential for performing operations on groups of files.

Read the section titled 'Wildcards' (page 2). Focus on the definitions of * (asterisk) and ? (question mark).

Here's a quick summary:

  • * (asterisk): Matches zero or more characters.
  • ? (question mark): Matches exactly one character.

Let's try it. In your VM, run these commands:

mkdir wildcard_test
cd wildcard_test
touch report_jan.txt report_feb.log app.conf app.log
ls

Now, try to predict what each of the following commands will list:

  • ls *.log (Lists all files ending in .log)
  • ls app.* (Lists all files starting with app.)
  • ls report_???.log (This will fail, as ? is a single character)
  • ls report_???.txt (This will match report_jan.txt)
  • ls report_* (This will match both the .txt and .log report files)

Wildcards can be used with cp, mv, and rm to manage large numbers of files at once. For example, rm *.tmp is a common command to delete all temporary files in a directory.


Conclusion

Excellent work. You have now mastered the fundamental tools for file and directory management on the Linux command line. These commands are verbs—the actions you will take to build and maintain systems.

Key Takeaways:

  • You can create files and directories with touch and mkdir (using the -p flag for convenience).
  • You can duplicate files and directories using cp (with the -r flag for directories).
  • You can move and rename files and directories with the versatile mv command.
  • You can delete files with rm and empty directories with rmdir. The rm -r command is powerful for deleting directories and their contents, and must be used with caution.
  • Wildcards (*, ?) are powerful tools for performing operations on multiple files at once.

In our next lesson, we'll address a crucial missing piece. We can create, copy, move, and delete files, but how do we actually edit their contents? We will dive into a command-line text editor, which is essential for changing the configuration files that control how your server and its applications run.