How to Remove Multiple Files at once on Linux

Deleting (or removing) a file in Linux from the command line can be done using the rm command. It allows you to delete more than one files at once.

Also, you can match multiple files using the wildcard (*) and regular expansions and easily delete them as needed.

In this tutorial, we’ll show you how to use the rm command, and a combination of other commands to remove files and directories in Linux.

To demonstrate this, we have created the below files.

ls -lh /home/linuxgeek/LAB
Removing Multiple Files at once on Linux

How to remove multiple files in Linux

In this section, we will show you multiple ways to delete more than one files at once.

1) To delete multiple files at once, use the rm command followed by the file names separated by space. This is the traditional method used by newbies.

rm -v filename1 filename2 filename3

2) To delete multiple files at once, use the brace expansion as shown below: This example deletes the given files for the month of 'September (09)'.

rm -v xyz-log-2021-09-{01,03,05}.txt

removed 'xyz-log-2021-09-01.txt'
removed 'xyz-log-2021-09-03.txt'
removed 'xyz-log-2021-09-05.txt'

3) Alternatively, you can also use the following command to perform the same action as above.

rm -v xyz-log-2021-09-0[135].txt

removed 'xyz-log-2021-09-01.txt'
removed 'xyz-log-2021-09-03.txt'
removed 'xyz-log-2021-09-05.txt'

4) To remove all logs for the month of 'October (10)', run:

rm -v xyz-log-2021-10-*

removed 'xyz-log-2021-10-0.txt'
removed 'xyz-log-2021-10-1.txt'
removed 'xyz-log-2021-10-3.txt'
removed 'xyz-log-2021-10-7.txt'

5) To remove all logs for the year of '2021', run:

rm -v xyz-log-2021-*

removed 'xyz-log-2021-09-02.txt'
removed 'xyz-log-2021-09-04.txt'
removed 'xyz-log-2021-11-21.txt'
removed 'xyz-log-2021-11-26.txt'
removed 'xyz-log-2021-11-28.txt'
removed 'xyz-log-2021-11-29.txt'

Make a Note: To remove write-protected files, you must pass the ‘-f (force)’ option to the rm command.

rm -f xyz-log-2021-*

6) You can use a wildcard (*) to match multiple files. For example, to remove all '.txt' files in the current directory, use the following command: The wild card works well for this, however to be safe it is best to use the wild card as minimal as possible.

rm *.txt

7) To delete all '*.tar.gz' files in the given directory in Linux, use the following command:

find /home/linuxgeek/LAB -type f -name "*.tar.gz" -exec rm -f {} \;
or
find /home/linuxgeek/LAB -type f -name "*.tar.gz" | xargs rm -f

8) To delete files older than '10 days' in Linux, use the following command:

find /home/linuxgeek/LAB -type f -mtime +10 -exec rm -f {} \;
or
find /home/linuxgeek/LAB -type f -mtime +10 | xargs rm -f

9) To delete all files larger than a certain size, run: This example find and delete files greater than '50 MB'.

find /home/linuxgeek/LAB -type f -size +50M -exec rm -f {} \;
or
find /home/linuxgeek/LAB -type f -size +50M | xargs rm -f

10) To quickly remove all the 'zero (0)' bytes files, run:

find /home/linuxgeek/LAB -type f -size 0 -exec rm -f {} \;
or
find /home/linuxgeek/LAB -type f -size 0 | xargs rm -f

11) To delete all files smaller than a certain size, run: The following command will find and delete all files less than '10M' size.

find /home/linuxgeek/LAB -type f -size -10M -exec rm -f {} \;
or
find /home/linuxgeek/LAB -type f -size -10M | xargs rm -f

12) To remove files between ‘1st Jan 2021’ to ’31st Oct 2021′, run:

find /home/linuxgeek/LAB -type f -newermt '2021-01-01' ! -newermt '2021-10-31' -exec rm -f {} \;
or
find /home/linuxgeek/LAB -type f -newermt '2021-01-01' ! -newermt '2021-10-31' | xargs rm -f

Wrapping Up

In this tutorial, we’ve shown you the different ways to remove or delete multiple files at once in Linux. I hope this guide enhances your ability to perform file removal operations on the terminal.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *