UNIX / Linux: Deleting Multiple Files In Bulk

Posted on in Categories , , , , , , last updated March 8, 2017

How do delete multiple files in bulk (say all *.bak file stored in /netapp/ and its sub-directory) under Linux / UNIX / *BSD MacOS X operating systems?

You can use the find command as follows to find and delete file in bulk.

Find command

The syntax is:

find /path/to/delete -type f -iname "fileType" -delete


find /path/to/delete -type f -iname "fileType" -exec rm -f {} \;

To delete all *.bak files in bulk from /netapp/ and its sub-directory, enter:
# find /netapp/ -type f -iname "*.bak" -delete
# find /netapp/ -type f -iname "*.bak" -exec rm -f {} \;

Shell scripting example to delete files in bulk

In this example, I have a file called delete.txt (with 5000+ entries, each per line) and you need to delete all those files:


Create a shell script as follows to read a text file using while loop one line at a time:

# Author: Vivek Gite
# Purpose: Delete a file using shell script in bulk
# -------------------------------------------------- 
## No editing below ##
[ ! -f "$_input" ] && { echo "File ${_input} not found."; exit 1; }
while IFS= read -r line
	[ -f "$line" ] && rm -f "$line"
done < "${_input}"

Run it as follows:
$ chmod +x script.sh
$ ./script.sh

The while statement is used to executes rm commands repeatedly on each file.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Leave a Comment