Delete specific files from all users documents

geekhelptx
New Contributor

I am looking for a simple way to delete specific files from all users documents.  I would expect the following would work, but it does not seem to.  Am I missing something here?

 

#!/bin/sh

rm -rf "/Users/*/Documents/Power Point Files/ppt1.potx"
rm -rf "/Users/*/Documents/Power Point Files/ppt2.potx"

4 REPLIES 4

andrew_nicholas
Valued Contributor

You're likely going to be better served by running a while loop over the user directories. I'll also harken against using the -r flag for anything in a user directory unless you're extremely sure your syntax is correct. Probably best to add a simple if statement to detect the presence off the file in question and then delete just that item. 

sgiesbrecht
Contributor III

Why not use a Policy > Files and Processes to search and Delete file if found

You can only do 1 file at a time this way right?  Would be a pain if you need to delete several files. 

geekhelptx
New Contributor

I got it working as follows:

 

lastUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`
rm "/Users/$lastUser/Documents/Folder Example/PPY file 1.potx"
rm "/Users/$lastUser/Documents/Folder Example/PPY file 2.potx"