Shell script to count files, then remove oldest files
        Posted  
        
            by Nic Hubbard
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Nic Hubbard
        
        
        
        Published on 2010-06-02T17:27:48Z
        Indexed on 
            2010/06/02
            17:43 UTC
        
        
        Read the original article
        Hit count: 259
        
I am new to shell scripting, so I need some help here. I have a directory that fills up with backups. If I have more than 10 backup files, I would like to remove the oldest files, so that the 10 newest backup files are the only ones that are left.
So far, I know how to count the files, which seems easy enough, but how do I then remove the oldest files, if the count is over 10?
if [ls /backups | wc -l > 10]
    then
        echo "More than 10"
fi
© Stack Overflow or respective owner