Bash script to remove files in directory but exlude by owner

Posted on 2011-10-18
Last Modified: 2013-12-26

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
Question by:rugman_66
    LVL 31

    Expert Comment

    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 '{}' \;
    LVL 31

    Accepted Solution

    If you want to ignore more than one user's you can do:
    type f means type


    find /path/to/dir -not -user UserA -not -user UserB -type f -exec rm '{}' \;

    Author Comment

    Thanks farzanj

    I added a few options, and works great!


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now