sort, this gets you the 50 latest files and their timestamp from the current directory:
find . -mindepth 1 -maxdepth 1 -printf '%T@ %p\n' | sort -n | tail -n 50
Pipe these into
while read to get each filename:
find . -mindepth 1 -maxdepth 1 -printf '%T@ %p\n' |
sort -n | tail -n 50 |
while read -r ts file; do echo rm -rf -- "$file"; done
printf outputs a sortable timestamp, which the numeric
sort will use. We get the oldest 50 files or directories and remove each one individually. Remove the
echo in the script to actually execute the commands.
Note that this does not work if your files/directories contain a newline in their name. Doesn't happen that often, but be careful to check. Due to the nature of
read, it will also not work if there are leading or trailing spaces.
For some more ideas, see: BashFAQ/003 – How can I find the latest (newest, earliest, oldest) file in a directory?