fbpx

how to delete all files in a directory linux

Avatar for Noman Mohammad

By Noman Mohammad

Published on:

how to delete all files in a directory linux
Your rating ?

Deleting files in Linux is somewhat tricky but is actually quite simple! Whether you are clearing out space, tidying up your system, or starting anew, learning to delete files in a directory is a must for every Linux user.

This guide is going to show you how to delete all files within a directory in Linux easily. Just follow these instructions, and you’ll acquire these skills in no time!

Why Would You Want to Delete All Files?

Sometimes directories can become cluttered with unnecessary files. For example:

  • Log or temporary files on an extraordinary scale; they get mounding since olden times.
  • Test files: while playing around, you may create unwanted files.
  • Empty the download folder: it’s good hygiene for the system to delete the files.

Whatever those reasons may be, Linux has some great commands that can get this task done quickly and with relative ease.

Use the rm Command to Delete Files

The rm command is your go-to tool for deleting files. Here’s the simplest way to delete all files in a directory:

rm /path/to/directory/*  

Explanation:

  • rm: Stands for remove.
  • /path/to/directory/: Replace this with the location of your directory.
  • *: This is a wildcard. It means “all files in this directory.”

For example, if your directory is /home/noman/test, you would run:

rm /home/noman/test/*

Delete Files and Subdirectories

What if your directory has subdirectories too? Use the -r (recursive) flag to delete everything:

rm -r /path/to/directory/*  

This command will remove:

  • All files in the directory.
  • All subdirectories and their files.

Warning: Be careful! This will delete everything permanently.


Use find for Advanced Deletion

Want more control? The find command helps you delete specific files or filter by criteria like file type or size.

Example 1: Delete All Files

find /path/to/directory -type f -delete  
  • -type f: Targets files only (not directories).
  • -delete: Deletes the files.

Example 2: Delete Files by Extension

To delete only .txt files:

find /path/to/directory -type f -name "*.txt" -delete  

Be Careful with These Commands!

Linux commands can be very powerful, but they simply do not have a rewind button. Here are a few safety tips:

  • Always double-check the command you are about to run, particularly one that will execute an operation on a directory path.
  • Do a test run like this lab experiment of sorts – for what files will get affected by the deletion by running the below command:
ls /path/to/directory/*  
  • Backing up their files: Always keep important files backed up somehow.

Conclusion

Deleting everything in a folder is very simple; with the right command, deletion can be done through rm or find; so, Linux will allow you many alternative ways to quickly delete everything.

But always remember this! With these tips, you will clean up your directories like a true professional.

If this guide proved helpful to you, then do share it with others, or let us know your thought in the comments below!

FAQs

Can I recover files deleted using rm?

No, files deleted with rm are permanently removed. Use backups to recover important data.

How can I delete hidden files in a directory?

Use the command:

rm /path/to/directory/.*

Be cautious, as this may also target critical hidden system files.

What happens if I run rm -rf /?

This command deletes all files and directories on your system, including system files, rendering your Linux unusable. Avoid running it at all costs!

Leave a Comment