You may want to move the readdir DIR step to a point just prior to the sort so that you can properly filter the directory there... perhaps using a glob or File::Find. This way we can explicitly include only the file types we want, rather than excluding the . and .. (what about .bash_history or such files?) inside the for loop.