Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Delete Files and Folders Using Linux Command Line

Learn how to delete files and folders using Linux command line with clear steps, practical tips, and troubleshooting advice for common issues.

Published: · 4 minutes read
Table of Contents

Quick Overview

Linux : Follow this practical guide to delete Files and Folders Using Linux Command Line, including Delete a file on Linux and Delete multiple files on Linux.

In this tutorial, the editorial team. com. com will show you how to delete files and folders using the command line on Linux. This tutorial is compatible with all Linux distributions, so it works the same way on Ubuntu, Debian, CentOS, AlmaLinux, Rocky Linux, etc.

Delete a file on Linux

In Linux, the rm command is used to delete files and directories. Navigate to the specific directory that contains the file you want to delete. Specify a location otherwise the command will start searching in the current working directory. For example, the author of this article has a file in the /tmp/ directory that he wants to delete. To delete the desired file, open Terminal and enter the following command:

# rm file.txt

Delete a file on Linux

Be careful while deleting files and folders from Linux because once deleted they cannot be recovered. So it will ask you to confirm before deleting the file:

# rm –i file.txt

Delete a file on Linux image 2

If you don't want to see a confirmation message for deletion, use the following command:

# rm –f file.txt

Delete a file on Linux image 3

The confirmation message will not appear.

Delete multiple files on Linux

To delete multiple files on Linux, the same rm command can be used.

# rm file.txt file1.txt file2.txt

Delete multiple files on Linux

Delete a directory on Linux

To delete a directory on Linux, the same command is used. But you need to add the -r and -f options to delete a directory.

# rm –rf /data

Delete a directory on Linux

But be careful, this will delete the entire directory, including all the files and directories inside. You can use the above command without -f, as it will not prompt for confirmation. The -r option is used to delete a directory.

If you just want to delete an empty directory, use this command instead:

rmdir /data

The command will show an error in case the directory is not empty.

Summary

In all Linux distributions, the rm command is used to delete files and directories.

  1. If -i is used with rm, you will be prompted for confirmation before deleting.
  2. If -r is used with rm, the directory will be deleted without confirmation.

See also:

FAQ About Linux

What should you prepare before you delete Files and Folders Using Linux Command Line?

Delete a file on Linux In Linux, the rm command is used to delete files and directories

What should you do if delete Files and Folders Using Linux Command Line does not work?

Understanding Linux helps you evaluate its benefits, limitations, and potential risks before making a decision.

How can you get better results when working with Linux?

To delete the desired file, open Terminal and enter the following command: # rm file. txt Be careful while deleting files and folders from Linux because once deleted they cannot be recovered

David Pac - IT Expert and Technology Writer

Written by David Pac

IT Expert & Technology Writer

David Pac has 20+ years of experience in software development, Windows troubleshooting, computer hardware diagnostics, and system administration.

Expertise: Windows | Software | Hardware | System Troubleshooting

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.