Removing files older than 7 days












57















I write below command to delete all files that are older than 7 days, but it doesn't work:



find /media/bkfolder/ -mtime +7 -name'*.gz' -exec rm {} ;


How can I remove these files?










share|improve this question




















  • 3





    There should be a space between name and '*.gz'.

    – Jos
    Feb 24 '15 at 9:25
















57















I write below command to delete all files that are older than 7 days, but it doesn't work:



find /media/bkfolder/ -mtime +7 -name'*.gz' -exec rm {} ;


How can I remove these files?










share|improve this question




















  • 3





    There should be a space between name and '*.gz'.

    – Jos
    Feb 24 '15 at 9:25














57












57








57


18






I write below command to delete all files that are older than 7 days, but it doesn't work:



find /media/bkfolder/ -mtime +7 -name'*.gz' -exec rm {} ;


How can I remove these files?










share|improve this question
















I write below command to delete all files that are older than 7 days, but it doesn't work:



find /media/bkfolder/ -mtime +7 -name'*.gz' -exec rm {} ;


How can I remove these files?







command-line delete find rm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 9 '17 at 3:40









αғsнιη

24.3k2295156




24.3k2295156










asked Feb 24 '15 at 9:22









Malihe PakyariMalihe Pakyari

286134




286134








  • 3





    There should be a space between name and '*.gz'.

    – Jos
    Feb 24 '15 at 9:25














  • 3





    There should be a space between name and '*.gz'.

    – Jos
    Feb 24 '15 at 9:25








3




3





There should be a space between name and '*.gz'.

– Jos
Feb 24 '15 at 9:25





There should be a space between name and '*.gz'.

– Jos
Feb 24 '15 at 9:25










2 Answers
2






active

oldest

votes


















106














As @Jos pointed out you missed a space between name and '*.gz'; also for speeding up the command use -type f option to running the command on files only.



So the fixed command would be:



find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' ;


Explanation:





  • find: the unix command for finding files/directories/links and etc.


  • /path/to/: the directory to start your search in.


  • -type f: only find files.


  • -name '*.gz': list files that ends with .gz.


  • -mtime +7: only consider the ones with modification time older than 7 days.


  • -execdir ... ;: for each such result found, do the following command in ....


  • rm -- '{}': remove the file; the {} part is where the find result gets substituted into from the previous part. -- means end of command parameters avoid prompting error for those files starting with hyphen.


Alternatively, use:



find /path/to/ -type f -mtime +7 -name '*.gz' -print0 | xargs -r0 rm --


From man find:



-print0 
True; print the full file name on the standard output, followed by a null character
(instead of the newline character that -print uses). This allows file names that contain
newlines or other types of white space to be correctly interpreted by programs that process
the find output. This option corresponds to the -0 option of xargs.


Which is a bit more efficient, because it amounts to:



rm file1 file2 file3 ...


as opposed to:



rm file1; rm file2; rm file3; ...


as in the -exec method.





An alternative and also faster command is using exec's + terminator instead of ;:



find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' +


This command will run rm only once at the end instead of each time a file is found and this command is almost as fast as using -delete option as following in modern find:



find /path/to/ -type f -mtime +7 -name '*.gz' -delete





share|improve this answer





















  • 1





    Why wouldn't I go straight with -delete at the end? Why mess with the + or ;?

    – rain01
    Jan 5 at 2:46











  • @rain01 plz read unix.stackexchange.com/q/167823/72456

    – αғsнιη
    Jan 14 at 17:49



















1














Be careful removing files with find. Run the command with -ls to check what you are removing



find /media/bkfolder/ -mtime +7 -name '*.gz' -ls
. Then pull up the command from history and append -exec rm {} ;



Limit the damage a find command can do. If you want to remove files from just one directory, -maxdepth 1 prevents find from walking through subdirectories or from searching the full system if you typo /media/bkfolder /.



Other limits I add are more specific name arguments like -name 'wncw*.gz', adding a newer-than time -mtime -31, and quoting the directories searched. These are particularly important if you are automating cleanups.



find "/media/bkfolder/" -maxdepth 1 -type f -mtime +7 -mtime -31 -name 'wncw*.gz' -ls -exec rm {} ;






share|improve this answer








New contributor




zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















    protected by Sergiy Kolodyazhnyy 25 mins ago



    Thank you for your interest in this question.
    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



    Would you like to answer one of these unanswered questions instead?














    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    106














    As @Jos pointed out you missed a space between name and '*.gz'; also for speeding up the command use -type f option to running the command on files only.



    So the fixed command would be:



    find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' ;


    Explanation:





    • find: the unix command for finding files/directories/links and etc.


    • /path/to/: the directory to start your search in.


    • -type f: only find files.


    • -name '*.gz': list files that ends with .gz.


    • -mtime +7: only consider the ones with modification time older than 7 days.


    • -execdir ... ;: for each such result found, do the following command in ....


    • rm -- '{}': remove the file; the {} part is where the find result gets substituted into from the previous part. -- means end of command parameters avoid prompting error for those files starting with hyphen.


    Alternatively, use:



    find /path/to/ -type f -mtime +7 -name '*.gz' -print0 | xargs -r0 rm --


    From man find:



    -print0 
    True; print the full file name on the standard output, followed by a null character
    (instead of the newline character that -print uses). This allows file names that contain
    newlines or other types of white space to be correctly interpreted by programs that process
    the find output. This option corresponds to the -0 option of xargs.


    Which is a bit more efficient, because it amounts to:



    rm file1 file2 file3 ...


    as opposed to:



    rm file1; rm file2; rm file3; ...


    as in the -exec method.





    An alternative and also faster command is using exec's + terminator instead of ;:



    find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' +


    This command will run rm only once at the end instead of each time a file is found and this command is almost as fast as using -delete option as following in modern find:



    find /path/to/ -type f -mtime +7 -name '*.gz' -delete





    share|improve this answer





















    • 1





      Why wouldn't I go straight with -delete at the end? Why mess with the + or ;?

      – rain01
      Jan 5 at 2:46











    • @rain01 plz read unix.stackexchange.com/q/167823/72456

      – αғsнιη
      Jan 14 at 17:49
















    106














    As @Jos pointed out you missed a space between name and '*.gz'; also for speeding up the command use -type f option to running the command on files only.



    So the fixed command would be:



    find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' ;


    Explanation:





    • find: the unix command for finding files/directories/links and etc.


    • /path/to/: the directory to start your search in.


    • -type f: only find files.


    • -name '*.gz': list files that ends with .gz.


    • -mtime +7: only consider the ones with modification time older than 7 days.


    • -execdir ... ;: for each such result found, do the following command in ....


    • rm -- '{}': remove the file; the {} part is where the find result gets substituted into from the previous part. -- means end of command parameters avoid prompting error for those files starting with hyphen.


    Alternatively, use:



    find /path/to/ -type f -mtime +7 -name '*.gz' -print0 | xargs -r0 rm --


    From man find:



    -print0 
    True; print the full file name on the standard output, followed by a null character
    (instead of the newline character that -print uses). This allows file names that contain
    newlines or other types of white space to be correctly interpreted by programs that process
    the find output. This option corresponds to the -0 option of xargs.


    Which is a bit more efficient, because it amounts to:



    rm file1 file2 file3 ...


    as opposed to:



    rm file1; rm file2; rm file3; ...


    as in the -exec method.





    An alternative and also faster command is using exec's + terminator instead of ;:



    find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' +


    This command will run rm only once at the end instead of each time a file is found and this command is almost as fast as using -delete option as following in modern find:



    find /path/to/ -type f -mtime +7 -name '*.gz' -delete





    share|improve this answer





















    • 1





      Why wouldn't I go straight with -delete at the end? Why mess with the + or ;?

      – rain01
      Jan 5 at 2:46











    • @rain01 plz read unix.stackexchange.com/q/167823/72456

      – αғsнιη
      Jan 14 at 17:49














    106












    106








    106







    As @Jos pointed out you missed a space between name and '*.gz'; also for speeding up the command use -type f option to running the command on files only.



    So the fixed command would be:



    find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' ;


    Explanation:





    • find: the unix command for finding files/directories/links and etc.


    • /path/to/: the directory to start your search in.


    • -type f: only find files.


    • -name '*.gz': list files that ends with .gz.


    • -mtime +7: only consider the ones with modification time older than 7 days.


    • -execdir ... ;: for each such result found, do the following command in ....


    • rm -- '{}': remove the file; the {} part is where the find result gets substituted into from the previous part. -- means end of command parameters avoid prompting error for those files starting with hyphen.


    Alternatively, use:



    find /path/to/ -type f -mtime +7 -name '*.gz' -print0 | xargs -r0 rm --


    From man find:



    -print0 
    True; print the full file name on the standard output, followed by a null character
    (instead of the newline character that -print uses). This allows file names that contain
    newlines or other types of white space to be correctly interpreted by programs that process
    the find output. This option corresponds to the -0 option of xargs.


    Which is a bit more efficient, because it amounts to:



    rm file1 file2 file3 ...


    as opposed to:



    rm file1; rm file2; rm file3; ...


    as in the -exec method.





    An alternative and also faster command is using exec's + terminator instead of ;:



    find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' +


    This command will run rm only once at the end instead of each time a file is found and this command is almost as fast as using -delete option as following in modern find:



    find /path/to/ -type f -mtime +7 -name '*.gz' -delete





    share|improve this answer















    As @Jos pointed out you missed a space between name and '*.gz'; also for speeding up the command use -type f option to running the command on files only.



    So the fixed command would be:



    find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' ;


    Explanation:





    • find: the unix command for finding files/directories/links and etc.


    • /path/to/: the directory to start your search in.


    • -type f: only find files.


    • -name '*.gz': list files that ends with .gz.


    • -mtime +7: only consider the ones with modification time older than 7 days.


    • -execdir ... ;: for each such result found, do the following command in ....


    • rm -- '{}': remove the file; the {} part is where the find result gets substituted into from the previous part. -- means end of command parameters avoid prompting error for those files starting with hyphen.


    Alternatively, use:



    find /path/to/ -type f -mtime +7 -name '*.gz' -print0 | xargs -r0 rm --


    From man find:



    -print0 
    True; print the full file name on the standard output, followed by a null character
    (instead of the newline character that -print uses). This allows file names that contain
    newlines or other types of white space to be correctly interpreted by programs that process
    the find output. This option corresponds to the -0 option of xargs.


    Which is a bit more efficient, because it amounts to:



    rm file1 file2 file3 ...


    as opposed to:



    rm file1; rm file2; rm file3; ...


    as in the -exec method.





    An alternative and also faster command is using exec's + terminator instead of ;:



    find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '{}' +


    This command will run rm only once at the end instead of each time a file is found and this command is almost as fast as using -delete option as following in modern find:



    find /path/to/ -type f -mtime +7 -name '*.gz' -delete






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 9 '17 at 3:43

























    answered Feb 24 '15 at 10:14









    αғsнιηαғsнιη

    24.3k2295156




    24.3k2295156








    • 1





      Why wouldn't I go straight with -delete at the end? Why mess with the + or ;?

      – rain01
      Jan 5 at 2:46











    • @rain01 plz read unix.stackexchange.com/q/167823/72456

      – αғsнιη
      Jan 14 at 17:49














    • 1





      Why wouldn't I go straight with -delete at the end? Why mess with the + or ;?

      – rain01
      Jan 5 at 2:46











    • @rain01 plz read unix.stackexchange.com/q/167823/72456

      – αғsнιη
      Jan 14 at 17:49








    1




    1





    Why wouldn't I go straight with -delete at the end? Why mess with the + or ;?

    – rain01
    Jan 5 at 2:46





    Why wouldn't I go straight with -delete at the end? Why mess with the + or ;?

    – rain01
    Jan 5 at 2:46













    @rain01 plz read unix.stackexchange.com/q/167823/72456

    – αғsнιη
    Jan 14 at 17:49





    @rain01 plz read unix.stackexchange.com/q/167823/72456

    – αғsнιη
    Jan 14 at 17:49













    1














    Be careful removing files with find. Run the command with -ls to check what you are removing



    find /media/bkfolder/ -mtime +7 -name '*.gz' -ls
    . Then pull up the command from history and append -exec rm {} ;



    Limit the damage a find command can do. If you want to remove files from just one directory, -maxdepth 1 prevents find from walking through subdirectories or from searching the full system if you typo /media/bkfolder /.



    Other limits I add are more specific name arguments like -name 'wncw*.gz', adding a newer-than time -mtime -31, and quoting the directories searched. These are particularly important if you are automating cleanups.



    find "/media/bkfolder/" -maxdepth 1 -type f -mtime +7 -mtime -31 -name 'wncw*.gz' -ls -exec rm {} ;






    share|improve this answer








    New contributor




    zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      1














      Be careful removing files with find. Run the command with -ls to check what you are removing



      find /media/bkfolder/ -mtime +7 -name '*.gz' -ls
      . Then pull up the command from history and append -exec rm {} ;



      Limit the damage a find command can do. If you want to remove files from just one directory, -maxdepth 1 prevents find from walking through subdirectories or from searching the full system if you typo /media/bkfolder /.



      Other limits I add are more specific name arguments like -name 'wncw*.gz', adding a newer-than time -mtime -31, and quoting the directories searched. These are particularly important if you are automating cleanups.



      find "/media/bkfolder/" -maxdepth 1 -type f -mtime +7 -mtime -31 -name 'wncw*.gz' -ls -exec rm {} ;






      share|improve this answer








      New contributor




      zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        1












        1








        1







        Be careful removing files with find. Run the command with -ls to check what you are removing



        find /media/bkfolder/ -mtime +7 -name '*.gz' -ls
        . Then pull up the command from history and append -exec rm {} ;



        Limit the damage a find command can do. If you want to remove files from just one directory, -maxdepth 1 prevents find from walking through subdirectories or from searching the full system if you typo /media/bkfolder /.



        Other limits I add are more specific name arguments like -name 'wncw*.gz', adding a newer-than time -mtime -31, and quoting the directories searched. These are particularly important if you are automating cleanups.



        find "/media/bkfolder/" -maxdepth 1 -type f -mtime +7 -mtime -31 -name 'wncw*.gz' -ls -exec rm {} ;






        share|improve this answer








        New contributor




        zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        Be careful removing files with find. Run the command with -ls to check what you are removing



        find /media/bkfolder/ -mtime +7 -name '*.gz' -ls
        . Then pull up the command from history and append -exec rm {} ;



        Limit the damage a find command can do. If you want to remove files from just one directory, -maxdepth 1 prevents find from walking through subdirectories or from searching the full system if you typo /media/bkfolder /.



        Other limits I add are more specific name arguments like -name 'wncw*.gz', adding a newer-than time -mtime -31, and quoting the directories searched. These are particularly important if you are automating cleanups.



        find "/media/bkfolder/" -maxdepth 1 -type f -mtime +7 -mtime -31 -name 'wncw*.gz' -ls -exec rm {} ;







        share|improve this answer








        New contributor




        zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 27 mins ago









        zekezeke

        111




        111




        New contributor




        zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        zeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.

















            protected by Sergiy Kolodyazhnyy 25 mins ago



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?



            Popular posts from this blog

            GameSpot

            日野市

            Tu-95轟炸機