Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»How to use the ‘find’ command to delete files modified older than X days in Linux

    How to use the ‘find’ command to delete files modified older than X days in Linux

    By RahulFebruary 10, 20233 Mins Read

    The “find” command in Linux is a powerful tool that can be used to search for files based on various criteria, such as name, type, size, and timestamp. One common use case for the “find” command is to delete files that are older than a certain number of days. This can be useful for cleaning up old backups, log files, or other types of files that are no longer needed.

    In this article, we will discuss how to use the “find” command to delete files older than X days in Linux.

    Step 1: Navigate to the directory

    Before using the “find” command, you need to navigate to the directory that contains the files you want to search and delete. You can do this using the cd command:

    cd /path/to/directory 
    

    Replace “/path/to/directory” with the path to the directory you want to search.

    Step 2: Preview the files to be deleted

    Before actually deleting the files, it’s always a good idea to preview the files that will be deleted, to ensure you’re not deleting any files by mistake. To do this, you can use the following “find” command, replacing “X” with the number of days:

    find . -type f -mtime +X -print 
    

    The “.” argument specifies the current directory, the “-type f” argument specifies that we want to search for files (not directories), the “-mtime +X” argument specifies that we want to search for files that are older than X days, and the “-print” argument displays the names of the matching files.

    For example, to find file modified older than 365 days, type:

    find . -type f -mtime +365 -print 
    

    Step 3: Delete the files

    Warning: The “find” command can be used to delete large numbers of files, so be careful when using it. It’s always a good idea to make a backup of the files you’re about to delete, in case you need to recover them later.

    Once you’re confident that the “find” command is correctly identifying the files you want to delete, you can use the following command to actually delete the files:

    find . -type f -mtime +365 -delete 
    

    This is the same command as in step 2, but with the addition of the “-delete” argument, which deletes the matching files.

    Conclusion

    In conclusion, the “find” command is a powerful tool that can be used to search for and delete files based on various criteria, including timestamps. By using the “find” command to delete files older than X days, you can automate the process of cleaning up old files, freeing up disk space and keeping your file system organized.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Ignore SSL Certificate Check with Wget

    How to Ignore SSL Certificate Check with Curl

    echo Command in Linux with Practical Examples

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Create and Use Custom Python Module
    • How to Install and Use Podman on Ubuntu 22.04 & 20.04
    • Setting Up Laravel with Docker and Docker-compose
    • Setting Up Development Environments with PHP and Docker
    • Using Composer with Different PHP Versions in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.