1

I am trying to get files older than 3 days (by modified date, mtime) by find command (i want to exec rm command in future, doesnt matter why :D). Problem is I am not root user and there are mix of the files, of course. It looks, like simple script as hell gonna to stuck when occurs first permission denied message.

#!/bin/sh
find /tmp* -type f -mtime +3 -group jenkins -print
# -exec rm {} \;

Output is just error message (owned by root user) find: ‘/tmp/systemd-private-dbe33161924b4616a037608d052f48d0-httpd.service-sFr4Wd’: Permission denied

Notice- there are many files which should occur in find command:

    ll /tmp | less
    ....
    drwxr-xr-x 2 jenkins jenkins 4096 Dec 11 02:33 02a47e28-4254-45d4-b69d-ed33b9ef3bad
    drwxr-xr-x 2 jenkins jenkins 4096 Dec  6 15:10 03a1acc7-3040-430a-b5c3-9ce646407b93
    drwxr-xr-x 2 jenkins jenkins 4096 Dec  5 12:27 062eb875-3216-4b2a-bae2-66106b66b0cd
    drwxr-xr-x 2 jenkins jenkins 4096 Dec  9 06:51 0a6f8afc-0976-441b-980b-d0502f3903b1
    drwxr-xr-x 2 jenkins jenkins 4096 Dec  5 13:07 1512475627515-0
    drwxr-xr-x 2 jenkins jenkins 4096 Dec  5 13:07 1512475627836-0
    drwxr-xr-x 2 jenkins jenkins 4096 Dec  5 13:07 1512475629807-0
    drwxr-xr-x 2 jenkins jenkins 4096 Dec  5 13:07 1512475629829-0
    drwxr-xr-x 2 jenkins jenkins 4096 Dec  5 13:07 1512475629843-0
    .....
    -rw-rw-r-- 1 root    root     301 Dec  5 14:37 informix_disc.log
    -rw-rw-r-- 1 root    root     714 Dec  5 14:37 oracle_disc.log
    -rw-rw-r-- 1 root    root     602 Dec  5 14:37 sybase_disc.log
    drwx------ 3 root    root    4096 Nov 20 13:47 systemd-private-dbe33161924b4616a037608d052f48d0-httpd.service-sFr4Wd
    drwxr-xr-x 2 root    root    4096 Oct 30 09:39 vmware-config0
    drwxr-xr-x 2 root    root    4096 Nov 17 08:51 vmware-config1
...

I have tried to avoid permission denied message following:

 1. 2>/dev/null OR 2>&- (add on the end, error output to null redirect)
 2. find -type d ! -readable -prune -o ...  (exclude not readable folders)
 3. find /tmp* -type f -mtime +3 -group jenkins -print 2>&1 | grep -v "Permission denied" (should be same as above)
 4. find -user jenkins, as well as -user $(whoami)
 5. -perm -u+rwx

When I try to redirect error output to /dev/null, command output is blank

Kindly please,

  • am I doing something wrong, or what is the reason it gots stucked after first permission denied message?
  • Is there any better simple way how to do what I want?
  • IS any way how to ignore not permitted folders and make it working?

Thanks

xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
  • Thanks for marking as duplicate, but if you will read the question, you will notice, mentioned solutions are not working for my case :) @Inian BTW in marked thread he wants just to exclude messages, I want to make my script working :D – xxxvodnikxxx Dec 12 '17 at 09:48
  • Have you tried doing the find with -user , so you just list files for user that scripts is running as ? – AndyM Dec 12 '17 at 10:46
  • I cannot see a duplicate flag on the post ? what about this https://stackoverflow.com/questions/762348/how-can-i-exclude-all-permission-denied-messages-from-find – AndyM Dec 12 '17 at 10:50
  • 1
    There's some very trigger happy close votes on SO lately. To help you, I'd suggest restructuring this question as *"How do I `find -exec` only on files I have access to"*. Make sure you google the same and read the manuals first. I'd really strongly suggest typing `find man linux` into google, and search the manual for `permissions` and `user`. – Philip Couling Dec 12 '17 at 10:51
  • Hi @AndyM , of course I have also tried it with -user , but result is just the same , and yes, script runs under "jenkins" user and I want to delete files created and owned by this user only Btw can see the duplicate flag, but if you will read solutions in linked topic and my post, suggested solutions are somehow not working for me .. In linked post they have the problem with annoying message, my problem is it wont to find files- from some reason it got probably stucked on permission message – xxxvodnikxxx Dec 12 '17 at 11:01
  • I guess the problem is because it tries also to search in subfolders, but on access to not permitted it got this error, its logically, but I have no idea how to make it continue and working well – xxxvodnikxxx Dec 12 '17 at 11:03
  • Do you actually need to reinvent this wheel? It might be simpler just to install and configure `tmpreaper`. – Toby Speight Dec 12 '17 at 15:41
  • Never heared about, but it looks like it is not provided on our distro, and ofc I am not permitted to install it :) Thanks for tip, but doesnt matter, it is working somehow, so np – xxxvodnikxxx Dec 12 '17 at 15:45

1 Answers1

1

Oh, problem was probably in logic finding files, I have made two commands (find directories and files separately)

#!/bin/sh
find /tmp -user $(whoami) -type d -mtime +3 -exec rm -rf {} \;
find /tmp -user $(whoami) -type f -mtime +3 -exec rm -f {} \;

And It looks like it works well, permission denied message still occurs, but commands are working.

xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
  • I'm glad you found a solution, but I'm still mystified by your original symptom: are you sure the command got _stuck_, as opposed to just taking a long time to complete? – mklement0 Dec 12 '17 at 22:29
  • It was probably getting stucked, because all output which I got is permission denied and no files were listed around the message. – xxxvodnikxxx Dec 13 '17 at 08:11