7 Replies Latest reply: Dec 4, 2013 12:16 PM by kazyskiyn0w RSS

    shell to remove files of two days ago or later

    kazyskiyn0w

      Hello experts and gurus,

       

      can someone tell me where can I find a documentation or web page that shows an easy and simple way to remove old files by date (for example, remove files older than 2 days: for example, if today is monday, I need to remove files from saturday or later). I researched but didn't find it. I am not shell script expert, but I'm studying linux.

       

      Many thanks,

        • 1. Re: shell to remove files of two days ago or later
          kazyskiyn0w

          I mean, I researched but didn't find anything "reliable" so  I decided to ask you guys.

           

          Any information would be greatly appreciated ....

           

          Thanks

          • 2. Re: shell to remove files of two days ago or later
            Zack.L

            Hi kaz,

             

            I used the below code ( help from forums ) to check remove files older than 3 days and output to cleanup.log listing deleted files if any:

             

             

            #!/bin/sh

            find /my_directory -type f -mtime +2 -exec rm -fvr {} \; > /tmp/cleanup.log

            exit

             

             

            You can use Linux man command to understand more about various options above.

             

            HTH

            Zack

             

             

             

            • 3. Re: shell to remove files of two days ago or later
              Dude!

              There was actually no need to limit the search results to regular files and then remove directories and their contents recursively. The find command has a few action commands built-in, which does not need to pass results to an external program for processing. Please see the following:

               

               

              Terminal - Example

              # su - dude
              $ touch testFile1 testfile2 TESTfile3

               

              $ find ./ -type f -iname 'testfile*' -ls -delete
              261742    0 -rw-rw-r--   1 dude     dude            0 Nov 28 08:41 ./testfile2
              261747    0 -rw-rw-r--   1 dude     dude            0 Nov 28 08:41 ./TESTfile3
              261689    0 -rw-rw-r--   1 dude     dude            0 Nov 28 08:41 ./testFile1

               

              $ find ./ -type f -name "testfile*" -ls -delete
              $

               

               

              Also note the effect of single and double quoting the find name argument and command line globbing, which many Linux/Unix system administrators with a MS Windows background may not be aware off. The shell resolves wildcards and variables rather than passing the argument to the find command unless it is single quoted:

               

              Terminal - Example

              $ touch 'test$File1' 'test$file2' 'TEST$file3'
              $ mkdir test

               

               

               

               

              $ find ./ -type f -iname 'test$file*' -ls
              261689    0 -rw-rw-r--   1 dude     dude            0 Nov 28 08:53 ./test$File1
              261747    0 -rw-rw-r--   1 dude     dude            0 Nov 28 08:53 ./TEST$file3
              261742    0 -rw-rw-r--   1 dude     dude            0 Nov 28 08:53 ./test$file2


              $ find ./ -type f -iname test$file* -ls
              find: paths must precede expression: test$File1

               

              $ strace find ./ -type f -iname test$file* -ls
              execve("/bin/find", ["find", "./", "-type", "f", "-iname", "test", "test$File1", "test$file2", "-ls"]

              $ strace find ./ -type f -iname 'test$file*' -ls
              execve("/bin/find", ["find", "./", "-type", "f", "-iname", "test$file*", "-ls"]

               

              $ strace find ./ -type f -iname "test$file*" -ls
              execve("/bin/find", ["find", "./", "-type", "f", "-iname", "test*", "-ls"]

               


              • 4. Re: shell to remove files of two days ago or later
                Zack.L

                Hi dude,

                 

                Thanks as always for very informative and instructive post.

                 

                Regards

                Zack

                • 5. Re: shell to remove files of two days ago or later
                  kazyskiyn0w

                  Thanks Zack and Dude for the answers.

                  • 6. Re: shell to remove files of two days ago or later
                    kazyskiyn0w

                    Hello,

                    If I use the parameter  -mtime +2 does it mean it'll find the files older than 2 days?

                     

                    And what if it is negative (-2)

                     

                    Thanks

                    • 7. Re: shell to remove files of two days ago or later
                      kazyskiyn0w

                      In the command

                      find ./ -type f -name "testfile*" -ls -delete

                       

                      how can I delete files older than 2 days?

                       

                      Thanks,