<p>Is there a simple way, in a pretty standard UNIX environment with bash, to run a command to delete all but the most recent X files from a directory?</p>

<p>To give a bit more of a concrete example, imagine some cron job writing out a file (say, a log file or a tar-ed up backup) to a directory every hour. I'd like a way to have another cron job running which would remove the oldest files in that directory until there are less than, say, 5.</p>

<p>And just to be clear, there's only one file present, it should never be deleted.</p>

Remove all but 5 (or whatever number) of the most recent files in a directory.
```
```
rm `ls -t | awk 'NR>5'`
```
```