Back to Blog
Lesson 6 of the Linux: Linux Command Line for Developers course
LinuxJuly 17, 20264 min read

Creating and Removing Files: A Guide to Linux File Manipulation

Master essential file manipulation in Linux. Learn how to create files with touch, safely remove files with rm, and manage directories with rmdir.

linuxcommand linedevopsterminalfile management
Neatly arranged blue office binders labeled with dates and names for organized storage.

Previously in this course, we used Project Kickoff: Provisioning Web Server Directories to scaffold the structure for our hardened web server. Now that we have our directories in place, we need the ability to create and remove files within them to manage our configuration assets and logs.

Effective file manipulation is a core skill for any DevOps engineer. Whether you are creating a placeholder index file or deleting a temporary log file that is consuming disk space, these commands are your primary tools.

Creating Files with the touch Command

In Linux, the touch command is the standard way to create an empty file. While it was originally designed to update the access and modification timestamps of existing files, its most common use in automation and project management is simply to create a new, zero-byte file if it doesn't already exist.

If you are just getting started with your project, you might want to create a placeholder configuration file.

Bash
# Navigate to your project directory
cd ~/web-server/config

# Create an empty configuration file
touch server.conf

If you run ls -l (as covered in The ls Command: Listing and Inspecting Files in Linux), you will see the file server.conf listed with a size of 0 bytes. touch is non-destructive; if you run it on an existing file, it simply updates the timestamp rather than overwriting your content.

Removing Files with rm

Close-up of a person organizing files in cardboard boxes, ideal for business and office themes.

When you need to clean up temporary files or remove deprecated configurations, use the rm (remove) command. This command is powerful and, by default, permanent—there is no "trash bin" in the CLI.

To remove a single file:

Bash
rm temp_log.txt

To remove multiple files at once, you can pass multiple arguments:

Bash
rm file1.txt file2.txt

The Power of Flags

The rm command is often used with flags to make operations safer or more aggressive:

  • -i (interactive): Prompts you before removing every file. Use this if you are hesitant about what you are deleting.
  • -f (force): Ignores non-existent files and never prompts for confirmation. Use this carefully, especially when scripting.

Removing Directories with rmdir

The rmdir command is designed specifically for removing empty directories. It is a safety-first command: if the directory contains even a single file, rmdir will refuse to delete it.

Bash
# This will only work if 'logs' is completely empty
rmdir logs

If you try to remove a directory that contains files, you will receive an error: rmdir: failed to remove 'logs': Directory not empty. This serves as a built-in safety net to prevent you from accidentally deleting project data.

Comparison: Deleting Objects

CommandTargetBehavior
touchFilesCreates a new file or updates timestamps
rmFiles/DirectoriesDeletes files; can delete directories with -r
rmdirDirectoriesOnly removes empty directories

Hands-on Exercise

Let’s apply this to our web server project:

  1. Navigate to your ~/web-server/logs directory.
  2. Use touch to create a file named access.log.
  3. Use ls to verify it exists.
  4. Use rm to delete access.log.
  5. Navigate back to ~/web-server and use rmdir to remove your logs directory (ensure it is empty first!).

Common Pitfalls

  • The "Recursive" Danger: You might be tempted to use rm -rf to delete directories. Be extremely careful; the -r (recursive) flag tells rm to delete the directory and everything inside it, including subdirectories. Always double-check your path before hitting enter.
  • Permissions: You may encounter a "Permission denied" error if you try to delete files created by another user or the root account. We will cover how to manage these permissions in upcoming lessons.
  • Wildcard Misuse: Using rm * is a common way to empty a directory, but if you are in the wrong folder (like /), you can accidentally delete system files. Always verify your location with pwd before using wildcards.

Recap

Wooden Scrabble tiles spelling 'RECAP' on a brown textured background. Text concept image.

You now know how to create empty files with touch, remove unwanted files with rm, and safely remove empty directory structures with rmdir. These commands allow you to maintain a clean and organized project environment as you build out your server.

Up next: We will learn how to duplicate and relocate your data using the cp and mv commands.

Similar Posts