How to Delete Multiple Files in Linux?

We have shared various articles that illustrate SSH commands and their functions. Today, in this tutorial we will explain the procedure to delete multiple files in Linux.

Delete Multiple Files in Linux

In this article, we will go through the following.

  1. Command to delete multiple files
  2. Command to delete multiple files accessed before
  3. Command to delete multiple files with same keyword
  4. Command to delete multiple files of same extension
  5. Command to delete files of certain size

Now, let us go through each command one by one.

#1. Command to delete multiple files

Execute the below given command to delete multiple files in Linux.

rm -v filename1 filename2 filename3

Replace filename1, filename2 & filename3 with the files which you want to delete or remove.

#2. Command to delete multiple files accessed before

  • Delete files in a directory accessed before particular date

Execute the below command to delete all the files modified or accessed before particular date and time in the current working directory.

find . -type f ! -newerat 'dd/mm/yyyy 17:24:00' -exec rm -f {} \;

You can modify the date & time in the above given command.

  • Delete files in a directory accessed before particular day

The below given command will help you to delete or remove files modified or accessed before particular days.

find path/of/the/file -type f -mtime +30 -delete 

+30 in the above command represents the days. You can change as per your requirement, i.e, if you want to delete multiple files accessed before 10 days then +30 would be replaced with +10 in the above command.

#3. Command to delete multiple files with same keyword

With the command below you can delete all the files in a directory having same filename keyword. This means if you have multiple files having keyword  new then you can use the below given command to delete them all at once.

rm -v keyword*
find -type f -name '*keyword*' -delete

Replace the keyword in above command with the one you want to delete.  That means if you want to delete all the files having new in their filename, then you need to new in place of keyword in above command.

#4. Command to delete multiple files of same extension

The below command will delete multiple files with same extension. This can be done in two ways.

  • Delete files of same extension in a directory by using the below command.
find path/of/the/file -type f -name "*.php" -exec rm -f {} \;
  • Delete files of same extension modified or accessed before particular days.
find path/of/the/file -name "*.php" -type f -mtime +30 -delete 

#5. Command to delete files of certain size

With the below command you can delete files of certain size.

  • Delete files larger than 70MB in the current working directory
find path/of/the/file -type f -size +70M -exec rm -f {} \;
  • Delete files smaller than 5MB in the current directory
find path/of/the/file -type f -size -5M -exec rm -f {} \;

You can modify the size of the file as per your choice.

That’s It !

Hope the article better describes the procedure to delete multiple files in Linux. Do share it with your colleagues if you find it working. Also share your suggestions and drop your queries to start the discussion on the related topic. Lets connect on social media with the below links.

Facebook Page: https://facebook.com/redserverhost                                                                                       Twitter Page: https://twitter.com/redserverhost.com