2

I am able to delete files or directories older than 1 day using below command

find /u01/Release/* -mtime +1 -exec rm -r {} \;

but why I am getting error in the output of this command:

ls -lrt 
drwxr-xr-x 3 tomcat8 tomcat8      60 Oct  4 07:11 build_180
drwxrwxr-x 6 root    root        309 Sep 21  2017 redis-3.2.11
-rw-r--r-- 1 root    root    1550452 Oct  4 15:23 redis-3.2.11.tar.gz

find: ‘/u01/Release/redis-3.2.11’: No such file or directory

after executing command

ls -lrt 
drwxr-xr-x 3 tomcat8 tomcat8      60 Oct  4 07:11 build_180
-rw-r--r-- 1 root    root    1550452 Oct  4 15:23 redis-3.2.11.tar.gz
  • It's not because a directory hasn't been modified in the last 10 years, that none of the files in it are recent. Modifying the content of a file doesn't modify the directory it's in. It's when you add or remove entries to/from a directory that the directory is modified. For instance / is hardly ever modified. You wouldn't want to remove every file because / is old. – Stéphane Chazelas Oct 4 '18 at 10:36
  • When you say older than 1 day, do you mean at least 2 days old (which is what -mtime +1 checks) or more than 24 hour old? – Stéphane Chazelas Oct 4 '18 at 10:41
2

This is very well known issue. The problem is that find first removes directory and then tries to process files inside it. A solution is to first deal with stuff inside directory and only then delete directory. That's what -depth option is for, so:

find /u01/Release/* -depth -mtime +1 -exec rm -r {} \;

should do the trick. You may also be interested in the shorter form using -delete action (which implies -depth):

find /u01/Release/* -mtime +1 -delete

As always man find is your friend.

  • 1
    It's -mtime +0 for files older than 24 hours – Stéphane Chazelas Oct 4 '18 at 10:31
  • Note that when you remove a file from a directory, that updates its mtime. If a/b/c is 1 minute old, a/b 5 days old and a 5 year old, then it will remove a/b (and c in the process), but not a. – Stéphane Chazelas Oct 4 '18 at 10:35
  • @StéphaneChazelas Yes, sure, I bet star glob is most probably also not what OP needs, but that's other topic. – jimmij Oct 4 '18 at 10:38
0

Try the below, Better to use --force which will delete files and directories.

find /u01/Release/* -mtime +1 -exec rm -rf {} \;


-r, -R, --recursive
              remove directories and their contents recursively
-f, --force
                  ignore nonexistent files, never prompt
  • i tried that but still get this message find: ‘/u01/Release/redis-3.2.11’: No such file or directory – RAJ METRE Oct 4 '18 at 10:13
  • ok, that means no older files are available under the directory /u01/Release/redis-3.2.11 – EBIN GLADSON Oct 4 '18 at 10:15
  • /u01/Release/redis-3.2.11 this is older directory. it is deleting that directory with that message – RAJ METRE Oct 4 '18 at 10:20
  • share the ls -d /u01/Release/redis-3.2.11 output here – EBIN GLADSON Oct 4 '18 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.