Find all files created by a certain user and delete them

Hi! I’m new to this forum and still new at Applescript.

I’m administering a college computer lab, and it’s time to do some drive cleaning between semesters. I found a script to delete files off the desktop without much trouble, but deleting files saved elsewhere throughout the hard drive seems to be harder to script. (This script will be used with Apple Remote Desktop.)

Is there a way to search for all the files created by a certain user (for example, all our machines have a “student” account) and then delete them? I’ve done some searching, but so far I’ve only found how to delete files created within a certain timeframe, like this:

try
	set myFolder to (choose folder)
	set myTime to display dialog "Delete files older than how many days?" default answer "60"
	set myWildcard to display dialog "Enter a wildcard search string." default answer "PV*.txt"
	set myScript to "find \"" & POSIX path of myFolder & "\" -type f -name \"" & text returned of myWildcard & "\" -mtime +" & text returned of myTime & " -exec rm {} \\;"
	do shell script myScript
	display dialog "Deletion completed successfully"
on error err
	display dialog err
end try

I’m worried the above script might delete updated applications.

I’ve also found this script, “How can I move files in Folder A to the trash, except for Folder B?” from the OS X Finder section:

property doNotDelete : {alias "Macintosh HD:Folder B:", alias "Macintosh HD:Folder C:", alias "Macintosh HD:Folder D:"}

tell application "Finder"
   move every item of doNotDelete to desktop -- Moves Folders B, C and D to the Desktop
   move every item of alias "Macintosh HD:Folder A:" to trash -- Moves everything else in Folder A to Trash
   move every item of doNotDelete to alias "Macintosh HD:Folder A:" -- Moves Folders B, C and D back to their original location (Folder A)
end tell

I’m worried that script might require me to name every folder I want to search… but I want to search the entire hard drive.

Would some combination of the two scripts above work?

I’ve also thought about writing a script that would delete certain types of files - but our students use lots of software and create a wide variety of file types.

Over the summer we’ll reload all the machines with a new drive image, but I’m hoping to come up with a less intense script to run before classes start again on January 24th.

Hi,

the find command includes a -user switch to specify a user


try
	set myFolder to (choose folder)
	set {text returned:myTime} to display dialog "Delete files older than how many days?" default answer "60"
	set {text returned:myWildcard} to display dialog "Enter a wildcard search string." default answer "PV*.txt"
	set {text returned:myUser} to display dialog "Enter a user name." default answer short user name of (system info)
	set myScript to "find " & quoted form of POSIX path of myFolder ¬
		& " -type f -name " & quoted form of myWildcard ¬
		& " -mtime +" & myTime & " -user " & myUser & " -exec rm {} \\;"
	do shell script myScript
	display dialog "Deletion completed successfully"
on error err
	display dialog err
end try


Wow, thanks!

I’m having a bit of trouble determining what to use for the wildcard search string. I tried putting in just “" and got the error “Can’t get every document”. Then I tried ".psd” since I know there are photoshop files to be deleted, but I got the same error again. Is there a wildcard for search for just files and folders and not applications or preferences files?

As for which folder to set it to, can I set it to the root user folder somehow? I tried setting it to the name of the hard drive and got the error “The variable HardDriveName is not defined”. Then I tried setting it to the documents folder instead, which didn’t give the same error.

I appear to be in slightly over my head. If there is a relevant tutorial I’ll be happy to read it.

you can exclude paths with the negation character ! and the -path switch (e.g. ! -path .app).
Consider that the asterisk characters must be escaped

OK, so I’ve gotten the script to work properly, but I don’t think it does quite what I was hoping for. I mean, I was hoping for something I could run using Apple Remote Desktop over the network on a several rooms full of computers. Having to choose a folder and confirm a bunch of prompts isn’t exactly the automation I can use.

I need to be able to delete folders students may have created, as well as a wide variety of file types. I don’t think that script can find and delete folders at all. And I’ve had no luck getting it to delete every file in one given folder. Using * as a Boolean wildcard doesn’t seem to work. (And why would I escape it out if I’m using it as a wildcard?)

So I went back to the other script:

property doNotDelete : {alias "Macintosh HD:Folder B:", alias "Macintosh HD:Folder C:", alias "Macintosh HD:Folder D:"}

tell application "Finder"
move every item of doNotDelete to desktop -- Moves Folders B, C and D to the Desktop
move every item of alias "Macintosh HD:Folder A:" to trash -- Moves everything else in Folder A to Trash
move every item of doNotDelete to alias "Macintosh HD:Folder A:" -- Moves Folders B, C and D back to their original location (Folder A)
end tell

I can’t get this one to work at all. I just get “The operation could not be completed.” Here are the modifications I used that may have caused it to fail. The hard drives are all named “AnimationMac”:

property doNotDelete : {alias "AnimationMAC:Applications:", alias "AnimationMAC:System:", alias "AnimationMAC:Library:"}

tell application "Finder"
	move every item of doNotDelete to desktop -- Moves Folders B, C and D to the Desktop
	move every item of alias "AnimationMac:Users:student:" to trash -- Moves everything else in Folder A to Trash
	move every item of doNotDelete to alias "AnimationMac:Users:student:" -- Moves Folders B, C and D back to their original location (Folder A)
end tell

My brother offered some useful advice:

I tried the find, and it produced a tremendously long list that I wasn’t sure what to do with. When I tried to “rm” those files I got a lot of “Permission Denied” and not even the Documents folder was emptied. When I tried “rm” I got a bunch of “Permission Denied” errors. So I googled a bit and tried “sudo” first. Here’s what I got back:

Eventually I came up with a script to do this: rm -rf *

This deleted files that the “student” user (the default account) has permission to delete, leaving applications in tact. It was crashing Apple Remote Desktop to run that “rm” over the network, plus there are some drive partitions on some of the machines, (named “Media01” or “Restore”) so I ultimately came up with this script:

set CR to ASCII character of 13
tell application "Terminal" to activate
tell application "System Events"
    tell process "Finder" to keystroke "rm -rf *" & CR
    delay 60
    tell process "Finder" to keystroke "cd /Volumes/Media01" & CR
    tell process "Finder" to keystroke "rm -rf *" & CR
    delay 10
    tell process "Finder" to keystroke "cd /Volumes/Media02" & CR
    tell process "Finder" to keystroke "rm -rf *" & CR
    delay 10
    tell process "Finder" to keystroke "cd /Volumes/Media03" & CR
    tell process "Finder" to keystroke "rm -rf *" & CR
    delay 10
    tell process "Finder" to keystroke "cd /Volumes/Restore" & CR
    tell process "Finder" to keystroke "rm -rf *" & CR
    delay 10
    tell process "Finder" to keystroke "exit" & CR
    delay 1
    tell application "Terminal" to quit
end tell

As my brother pointed out this is an insane script that probably deletes way more files than I ought to. If I totally kill a machine, I can always reload it with our standard disc image.

It probably would’ve been better to do what I originally set out to: delete all the files created by the user “student”.

ninjaconsultant:

You seem intent on doing this your own way, but indeed, you are playing with fire.

StefanK is one of the most talented (and generous) contributors here at MacScripter, and you would be well advised to ‘work with him’… if he is still willing to help.

Instead, you’ve struck out on your own, and attempted to move critical system related folders out of the root folder (definitely not a good idea), and then (in another effort) used Terminal to run nearly random ‘rm’ shell scripts using GUI scripting… another approach that an experienced scripter would frown on… or laugh at.

I am far from expert in AppleScript, but even I can see that your ‘unbridled enthusiasm’ will almost certainly (soon) get you into deep trouble.

Here is another approach to removing user folders… but it does not take into account who the current user might be. If you are the admin, and know your admin name then (obviously) don’t include it in the deletion process.

Again, while you may want to learn on your own, it’s best to take into account the advice you solicited yourself… rather than simply discarding it.

Peter B.




set users_path to (path to users folder)

tell application "System Events" to set user_folders to name of every folder of users_path

set folder_names to choose from list user_folders with multiple selections allowed

if folder_names is false then
	error number -128
end if

repeat with this_folder_name in folder_names
	set target_folder_path to users_path & this_folder_name & ":" as text
	tell application "Finder" to move item target_folder_path to trash
end repeat


I wouldn’t say I totally discarded his advice… I learned something from it. However, it wasn’t the automation I’d hoped for. I want to run a script over the network, without having to go to each machine individually (or control each remotely) and enter a number of days and user name 50 times for each machine the script runs on. I don’t want to have to answer prompts if I can avoid it.

Thank you Peter, for your script. It seems to delete folders but not files for some reason. Again, it looks like I would have to control each machine individually and handpick the folders to be cleaned out every time. (Unless I’m missing something obvious…)

Part of my trouble that I don’t think I’ve been clear on is that the students (the users) are saving files wherever they can throughout the machine. (They don’t have administrator privileges by default.) Sure, I could use my super-dangerous rm script to empty the likely directories, like the Documents and Movies folders, the Desktop, and the partitioned “Media01” and “Media02” drives, but if students have “hidden” files elsewhere on the machines my script won’t “find” them and delete them. (The students have actually told me they do this, I’m not being paranoid.)

Another friend offered some help with this command:

sudo find / -user -exec ls -ltrF {} ; > ~/Desktop/files.txt

I had to log in as administrator to get this to work, and it produced an incredibly long list of files, many of which I probably don’t want to delete. The idea is to then run this command later:

sudo find / -user student -exec /bin/rm -rf {} ;

I guess I’m having trouble distinguishing between files created by users that I do want to delete, like these:

/Users/student/Documents/new/new.ftpr
/Volumes/MEDIA01/FrogHop/frog.jump.jpg*
/Users/student/Downloads/MelancholyBrushes/MelancholyBrushes_8.png

And files I probably shouldn’t erase, like these:

/Users/student/Library/Application Support/Adobe/Flash CS4/en/Configuration/Classes/mx/controls/listclasses/DataSelector.as*
/Applications/Dragon Burn.app/Contents/Frameworks/mcpcmaout.framework/Versions/1.2.7281/mcpcmaout*
/Applications/Utilities/Smultron.app/Contents/Resources/Syntax Definitions/vb.plist*

If the user student has no admin rights, he cannot create files at another location as his own user folder (and the external volumes)
So refine the search to Users/student

sudo find /Users/student -user student .

the admin password is required to obtain root privileges (sudo) for deleting items in folders which originally don’t belong to the current user