kcott already provided a nice clean Perl solution.
If his solution is running too slowly, please post a reply with your new code, along with system profiling output,
just in case we can spot any booboos you might have made in implementing his suggestions.
Given you have "2064 files (up to a couple hundred MB)", it would not surprise if you've hit a chronic
thrashing problem. If so, I can see three options you might try:
- The approach taken by tybalt89 using an external sort command.
- Replace Perl's internal sort function with an external sort such as Sort::External - see also the "External Sorting" section at Sorting References.
- Depending on your hardware/budget, a simple solution requiring no coding changes at all may be to just buy more memory! :) You can buy 32 GB of PC memory for around $200 nowadays. Update: Note that a DDR4 DIMM can hold up to 64 GB, while DDR5 octuples that to 512 GB ... which makes worrying about a program's memory usage seem much less important than in the good old days. :)
Alternative suggestions welcome.