• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Bash script to remove files in directory but exlude by owner

Hello,

I'm new to Bash scripting, and need a scrip to run as a cron that will remove all files recursively
in specific directories, but exclude any files owned by specific users. How can this be done?

Thank you
0
rugman_66
Asked:
rugman_66
  • 2
1 Solution
 
farzanjCommented:
You need to use a simple find command like
/path/to/dir is the path of directory under which you want to do a recursive search
In this command, I didn't want to remove files owned by root

find /path/to/dir -not -user root -type f -exec rm '{}' \;
0
 
farzanjCommented:
If you want to ignore more than one user's you can do:
type f means type

#!/bin/bash

find /path/to/dir -not -user UserA -not -user UserB -type f -exec rm '{}' \;
0
 
rugman_66Author Commented:
Thanks farzanj

I added a few options, and works great!

-John
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now