How to delete files from a folder having large number of files

Find file patern

Change to the folder with the files. and use the below command the list 10 files to know the file name pattern.

 ls -U | head -10

Let’s assume the results shown were as below. you can see that the files are in different file paterns.

Delete the files

let’s choose one of the patterns 1481437763… and execute the following commands to try to delete all files of that pattern. You can see that the last command in the below list has one less numeral. You may have to adjust your patterns depending on the number of files present int he pattern.

rm -rf 148101*
rm -rf 148102*
rm -rf 148103*
rm -rf 148104*
rm -rf 148105*
rm -rf 148106*
rm -rf 14810*

once sufficient files are deleted you can execute the below command to delete remaining files in the folder.

rm -rf *

You can use the above solution to also resolve below issues

1) ls command not responding. too many files.

2) cannot delete. rm command returns Argument list too long”

3) too many files in Maildir folder

Leave a Comment