Solved

Using Terminal to Delete Contents of User's Docs Folder

Posted on 2004-08-06
4
420 Views
Last Modified: 2010-04-14
I need to use terminal a UNIX command to delete the contents of a directory, but need to do it recursively through a parent directory...

I have done this with a specific document in the past, using...

find [path] -name "[nameofdocument]" -delete
(find /Users -name "afpDelete*" -delete)... is specifically what I used

This time, however, I want to delete the contents of a directory.
Specifically, I want to go through the Users folder (/Users/) and find the Documents (/Users/[user]/Documents/] folder for each user and delete the contents of their documents folder.

Any ideas? Can't really find what I need in man pages.  Oh, by the way, I am using bash.

Thanks in advance for any input!
0
Comment
Question by:nonesuch
4 Comments
 
LVL 30

Expert Comment

by:weed
ID: 11737091
Do you need to delete every users docs at once, or one user at a time?
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 250 total points
ID: 11737576
Try:

for f in `/bin/ls -1dp /Users/* | grep '/$'` ; do rm -rf "${f}Documents/*"; done;

But why would you want to delete all your users' data from just a single directory?
Normally I would not ask such things, however this has a high potential for malicious
use.  I can imagine a case for removing all the data for a single user or even all the
data for all users, but deleting the contents of only the Documents folder for all users
seems ... odd.

0
 
LVL 3

Author Comment

by:nonesuch
ID: 11738508
This is for users who need a "fresh" Documents folder once a year.  They are students and get an archive of their Documents  on CD at the end of the school year.  The following Fall, they need an "empty" Docs folder in order to work more efficiently.  If there are documents that they want from the previous year, they can either get them from their CD or they can login to our archive server and pull stuff in.  This was supposed to happen on a student by student basis in their final visit to the lab, however, the lab instructor forgot and that put us in our current position.  School starts next week... we are in a bind to get this done and my terminal skills aren't all that smooth.

Thanks for the input, I will give this a test try via echo.  Rest assured that this won't be used maliciously.

Also, as I ponder your rather elegant solution, I am certain that there would also be a way to pass a list of short names (in other words, a list of home folder names) and have it only "do" this to those folders.  Any ideas?
0
 

Expert Comment

by:mrblackhat
ID: 11813630
rm -rf /username

From the documents folder.  That will remove 'rm' files -recursively 'r' -without prompting you 'f'.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Install Sharp MX2310u on mac OS 4 569
iPhone 5s Went Crazy 23 171
Mac OSX can't search on smb  via finder 10 292
Apple macbook pro losing network/ wifi after sleep 4 56
Apple's Mac OS X has become an official member of the malware club. The Flashback Trojan has affected over half million Macs, worldwide. It is behavior that ultimately gets malware onto a person’s computer. Obsolete or out-of-date software helps…
A common question or need, when setting-up a new Mac for someone would be to make all of the applications, installed, available from the dock. Many people often do not realize an application is installed unless it is in the dock. Creating a custo…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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