A few shell commands to list, sort or move files in a large folder.
I have a component that writes a log file every time a task is executed, and they all go to the same folder. ls or Midnight Commander take a long time on that folder. I’d like to move the old files out of the way, e.g. to a folder called, well, old. Or I want to delete them. Also, I’d like to list the most recent files, maybe the largest of today, or just the last file that was written. Here a few commands that work on my Ubuntu bash:
find /data/logs/ -maxdepth 1 -mtime +30 -type f -exec mv "{}" old/ \;
Finds the files older than 30 days and moves them to a folder old/
find /data/logs/ -maxdepth 1 -type f -mtime -1
Lists the files no more than 1 day old.
find /data/logs/ -maxdepth 1 -type f -mtime -1 -exec du -ah "{}" \; | sort -n -r | head -n 10
Finds the files no older than 1 day, sorts them by size, and shows the largest 10.