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:
- Open the terminal application
- To delete everything in a directory run: rm /path/to/dir/*
- 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/
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/*
$ ls - /home/vivek/data/
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/
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:
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.
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: