I want to remove all files from a directory. Can you tell me Linux command to delete all files in a directory?

Introduction: You can remove all files in a directory using unlink command. Another option is to use the rm command to delete all files in a directory. This page explains how to delete all files in a directory using the command line options.

Linux Delete All Files In Directory

The procedure to remove all files from a directory:

  1. Open the terminal application
  2. To delete everything in a directory run: rm /path/to/dir/*
  3. To remove everything to remove all sub-directories and files: rm -r /path/to/dir/*

Let us see some examples of rm command to delete all files in a directory when using Linux operating systems.

How to remove all the files in a directory?

Suppose you have a directory called /home/vivek/data/. To list files type the ls command:
$ ls ~/data/
List all files in Linux using ls command
To delete all files in a directory named /home/vivek/data/, run:
$ rm /home/vivek/data/*
You can see what is being done when deleting all files in directory pass the -v option to the rm command:
$ rm -v /home/vivek/data/*
Verify it:
$ ls - /home/vivek/data/
Linux Delete All Files In Directory Using rm
As you can see rm command failed to remove subdirectories /home/vivek/data/images and /home/vivek/data/scripts. To delete all files folders from a directory, run:
$ rm -rfv /home/vivek/data/
How to remove all files from a directory using rm

Understanding rm command option that deleted all files in a directory

  • -r : Remove directories and their contents recursively
  • -f : Force option i.e. ignore nonexistent files and arguments, never prompt
  • -v : Verbose option

Linux Remove All Files In Directory

As I said earlier one can use the unlink command too. The syntax is:
unlink filename
It can only delete a single file at a time. You can not pass multiple files or use wildcards such as *. Therefore, I strongly recommend you use the rm command as discussed above.

Conclusion

In this quick tutorial, you learned how to remove or delete all the files in a directory using the rm command. Linux offers a few more options to find and delete files. Please see the following tutorials:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Similar Posts