Reputation: 1277
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,
Thanks
Upvotes: 1
Views: 1076
Reputation: 1277
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.
Upvotes: 1