Skip to main content
Question

Delete specific files from all users documents

  • July 7, 2022
  • 4 replies
  • 24 views

geekhelptx
Forum|alt.badge.img+3

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

Forum|alt.badge.img+13
  • Honored Contributor
  • 365 replies
  • July 7, 2022

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. 


Forum|alt.badge.img+10
  • Valued Contributor
  • 134 replies
  • July 8, 2022

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


geekhelptx
Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 3 replies
  • July 8, 2022

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
Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 3 replies
  • July 8, 2022

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"