0

I am trying to delete files (log files) which are more that 5 days old and I need to keep one specific file without deleting, I tried it using

find . -type f -mtime +5 ! -name 'test2' -exec rm -f {} \;

But this is not successful. The OS im using is Centos 7.

4
  • an alternative could be to echo the results of find . -type f -mtime +5 to a file, remove 'test2' from the list and then use a for loop to delete all items listed in the file.
    – Centimane
    Commented Oct 13, 2015 at 13:28
  • 5
    How is it "not successful"? Does it delete all files including test2? No files at all? Do you get an error message? Also please explore the possibility of using logrotate for this, if your requirements are at all complicated.
    – Wildcard
    Commented Oct 13, 2015 at 13:30
  • 1
    It works on my system linux mint 17.2 , can you please show us the output of ls -la .
    – lese
    Commented Oct 13, 2015 at 13:35
  • @Dave, he's running centos so has GNU versions of the tools available: find . -type f -mtime +5 -print0 | grep -Zzv '^test2$' | xargs -0r rm -f
    – cas
    Commented Oct 14, 2015 at 0:35

1 Answer 1

2

As you can see I've created for testing the following structure:

mkdir test && cd test && touch file1 file2 file3 lol test2
touch -t 200805101024 file*

obtaining this:

ls -la
total 8
drwxr-xr-x 2 root   root   4096 Oct 13 18:09 .
drwxr-xr-x 4 phphil phphil 4096 Oct 13 18:09 ..
-rw-r--r-- 1 root   root      0 May 10  2008 file1
-rw-r--r-- 1 root   root      0 May 10  2008 file2
-rw-r--r-- 1 root   root      0 May 10  2008 file3
-rw-r--r-- 1 root   root      0 Oct 13 18:09 lol
-rw-r--r-- 1 root   root      0 Oct 13 18:09 test2

And after executing your correct command:

find . -type f -mtime +5 ! -name 'test2' -exec rm -f {} \;

The situation is as expected:

ls -la
total 8
drwxr-xr-x 2 root   root   4096 Oct 13 18:09 .
drwxr-xr-x 4 phphil phphil 4096 Oct 13 18:09 ..
-rw-r--r-- 1 root   root      0 Oct 13 18:09 lol
-rw-r--r-- 1 root   root      0 Oct 13 18:09 test2

Edit : Finding informations about commands, or command params :

When I'm not sure about the behaviour of a certain command and/or parameter, I proceed as follow :

man find | less

then you can search what you are looking for inside the manual of the command as follow:

  • type /
  • write your search terms which I beleave they are -mtime
  • then you can go to the next occurrence of your search terms by typing N , or to the previous one by typing n

I have find out this interesting informations:

   -mtime n
          File's data was last modified n*24 hours ago.  See the comments for -atime to understand how  round‐
          ing affects the interpretation of file modification times.
6
  • You did not test what the question is.
    – schily
    Commented Oct 13, 2015 at 15:46
  • What you mean? you talking about the "two conditions", I think he was talking about 1) 5 days old files 2) except a file 'test2'
    – lese
    Commented Oct 13, 2015 at 16:06
  • Oh you right! I got it, I will do it and fix the answer
    – lese
    Commented Oct 13, 2015 at 16:07
  • @lese thank you, when we put -mtime +5 does it count from the current system date and time both from the time it is executed to 5 days back? sorry I'm bit new to linux.
    – Sineth
    Commented Oct 14, 2015 at 8:22
  • I think this is an other question and should not be related with the acceptance of the answer : ) , anyway I have edit my answer in order to help you find what you looking for
    – lese
    Commented Oct 14, 2015 at 9:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .