How to Remove Files and Directories in Linux

How to Remove Files and Directories in Linux

Navigating the file system is an integral part of Linux administration, and at times, removing files and directories becomes essential. The rm command in Linux offers a powerful and precise tool for this task. In this article, we’ll guide you through the intricacies of using rm to ensure safe and effective deletion of files and directories, keeping your Linux environment organized and clutter-free.

Importance of File and Directory Removal In Ubuntu

Removing files and directories in Ubuntu, as in other operating systems, is essential for various reasons. Here’s why and when you might need to do it:

  1. Freeing Up Disk Space. Over time, systems accumulate files—temporary data, cached content, old logs, unused software, and more. Regularly removing unnecessary files ensures you don’t run out of disk space, which can impact system performance and prevent you from storing essential data.
  2. System Performance. Some applications generate logs or temporary files. If not managed, these can slow down the system or cause applications to malfunction.
  3. Software Management. When installing or updating software, occasionally, older versions or residual configuration files remain. Removing them ensures that the software runs efficiently and minimizes potential conflicts.
  4. System Migration or Backup. If you’re migrating to a new system or backing up your data, removing unnecessary files can speed up the process and require less storage.

Removing Files and Directories in Ubuntu Using the rm Command

Here’s a step-by-step guide to removing files and directories in Ubuntu, complete with commands, descriptions, and expected outputs.

#1 Remove a Single File

To delete a specific file. Run this command below. Please note, typically, the rm command will not output anything if successful.

rm filename.txt

#2 Prompt Before Removing

If you want to ensure safety by being prompted before deletion. Run this command:

rm -i filename.txt

Sample Output:

rm: remove regular file 'filename.txt'?

You can then type y for yes or n for no.

#3 Remove Multiple Files

Remove Multiple Files by using this command.

rm file1.txt file2.txt file3.txt

#4 Remove a Directory

To delete an empty directory by running this command.

rmdir directoryname

#5 Remove a Directory and Its Contents

To delete a directory and all its contents, use this command with caution. It will delete everything without prompting! Exercise care when utilizing this command.

rm -r directoryname

#6 Prompt Before Removing Directories and Files Recursively

For a safer recursive delete, where you’re prompted before each deletion.

rm -ri directoryname

Sample Output:

rm: descend into directory 'directoryname'?
rm: remove regular file 'directoryname/file1.txt'?

You’ll be prompted for each item.

#7 Force Remove (with Caution)

To force the deletion of files without being prompted, even if the files are write-protected.

rm -f filename.txt

#8 Force Remove Directories Recursively (with Extreme Caution)

This combines force deletion with recursive deletion. It’s one of the most powerful and dangerous commands. Make sure you truly want to delete the directory and everything inside it without any prompts.

rm -rf directoryname

Congratulations! You’ve now learned how to remove files and directories in Ubuntu. Always exercise caution when using the rm command, particularly with the -f and -r flags, as it’s easy to accidentally delete vital data. Make sure you have backups or utilize the -i flag for interactive prompts to confirm each deletion.


Looking to deploy Linux in the cloud? With Gcore Cloud, you can choose from Basic VM, Virtual Instances, or VPS/VDS suitable for Linux:

Choose an instance

Subscribe to our newsletter

Stay informed about the latest updates, news, and insights.