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.

1481437763.H954P5447.server.hostname.com
1468783501.H490580P22718.server.hostname.com
1475010772.H687029P17227.server.hostname.com
1493274854.H345961P8138.server.hostname.com
1482192789.H301559P16572.server.hostname.com
1489596894.H418001P14490.server.hostname.com
1477925020.H369024P27934.server.hostname.com
1492679074.H264189P23124.server.hostname.com
1472341025.H302369P21273.server.hostname.com
1478050624.H708736P8736.server.hostname.com

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