Logo Background

Unix Find Files And Delete

  • By on June 19, 2010 | No Comments

    Looking for how to find and delete or remove files in a single command under UNIX / Linux operating system? It is necessary to find out files and remove them especially in the log folder.

    The find command is capable of much more than simply locating files where it can automatically execute sequences of other UNIX commands by using the filenames found as input.

    You plan to housekeep the log folder to keep the log files not older than 30 days.

    Under 30 days log retention period, you need to have a single Unix command to find files and delete in order to achieve the housekeeping criteria.

    The first command as below will list down all the files which is older than 30 days while the second command will remove the files.

    Remember to create a backup for the files that you going to remove before you execute any Unix find files and delete command.

    $ find . -mtime +30 | xargs ls -lrt
     
    $ find . -mtime +30 -exec rm -f {} \;

    Caution: Before removing file makes sure, you have backup of all-important files. Do not use rm command as root user it can do critical damage to Unix / Linux operating system.

    Previous
    Next
    » View Files In TAR Archive
Leave a Comment