[COLUG] gawk question

William Yang wyang at gcfn.net
Sun Aug 20 08:23:19 EDT 2006


Nathan Overley wrote:
> Thanks for the suggestions. Is there a way to dynamically assign the STUDENT 
> variable with the names of the users directories?

STUDENTS="User1 User2 ... UserN"

for i in ${STUDENTS}; do
	STUDENTDIR=`getpwent | gawk -v user="${i}" -F: \
				'( $1 == user ) { print $6 }'`
	CWD=`pwd`
	cd $STUDENTDIR
	find . -maxdepth 1 -type -d \! -name career | xargs echo rm -rf
	cd $CWD
done

# always be very careful with automated find/remove combinations.
# use this code at your own risk.


> Thanks,
> Nathan
> 
>> ===== Original Message From jmglov at wmalumni.com, Central OH Linux User Group 
> <colug432 at colug.net> =====
>> On 19/08/06, Steven Huwig <shuwig at columbus.rr.com> wrote:
>>
>>>    $ find -d . -mindepth 2 \! -name career -exec rm -d -i {} \;
>>>
>>> The -i flag at the end will prompt you for every file deletion, since
>>> I didn't test that command line thoroughly.
>> Good suggestion. With dangerous commands, I usually do this first:
>>
>> STUDENTS="User1 User2 ... UserN"
>> cd /home
>> for i in ${STUDENTS}; do
>> find $i/ -maxdepth 1 -type -d \! -name career | xargs echo rm -rf
>> done
>>
>> (Thanks for the "\! -name foo" trick--I did not know that bit of find
>> syntax). Then, I inspect the output, and if satisfied, remove the
>> "echo" (or, in most cases, replace it with "sudo"), as such:
>>
>> STUDENTS="User1 User2 ... UserN"
>> cd /home
>> for i in ${STUDENTS}; do
>> find $i/ -maxdepth 1 -type -d \! -name career | xargs sudo rm -rf
>> done
>>
>> The key point to take away is to be *very* careful with destructive
>> commands, especially when executing them as root!
>>
>> -Josh
>> _______________________________________________
>> colug432 mailing list colug432 at colug.net
>> http://www.colug.net/mailman/listinfo/colug432
> 
> 
> 
> _______________________________________________
> colug432 mailing list colug432 at colug.net
> http://www.colug.net/mailman/listinfo/colug432
> 


-- 
William Yang
wyang at gcfn.net


More information about the colug432 mailing list