deleting directories from a list


 
Thread Tools Search this Thread
Special Forums Hardware Filesystems, Disks and Memory deleting directories from a list
# 1  
Old 04-10-2006
Question deleting directories from a list

I have a need to remove a bunch of old user home directories from my
OS X server. Using BASH and ls, I've been able to redirect the directory structure to a file with 'ls -l >userslist'. I've then manually sifted through the list for directories whos owner doesn't match the a given directory name (I'm
sure I could have used grep to filter this for me but hey, that's why I'm here asking questions! I'm new to these tools.)

Anywhoo, what I want to do now is take this list of names and pipe it
into 'rm' to delete them. Which command would read my list line by line to stream into a pipe over to 'rm' to delete each directory? This is a simple one for most UNIX admins I'm sure and I could just do this with the GUI but I'm forcing myself to remove the training wheels.

Any takers? Thanks in advance.

Julius
# 2  
Old 04-10-2006
There are many ways to do this, but here's one. Make sure you
don't run
ls -a > file_list
or there'll be trouble with rm -rf Smilie

Code:
# ls -l
total 12
drwxr-xr-x  2 root root 4096 Apr 10 16:04 dir_1
drwxr-xr-x  2 root root 4096 Apr 10 16:04 dir_2
drwxr-xr-x  2 root root 4096 Apr 10 16:04 dir_3
-rw-r--r--  1 root root    0 Apr 10 16:02 file_1
-rw-r--r--  1 root root    0 Apr 10 16:02 file_2
-rw-r--r--  1 root root    0 Apr 10 16:02 file_3
# ls > file_list
# while read file; do
>   [[ -d "${file}" ]] && rm -rf ${file}
> done < ./file_list
# ls -l
total 4
-rw-r--r--  1 root root  0 Apr 10 16:02 file_1
-rw-r--r--  1 root root  0 Apr 10 16:02 file_2
-rw-r--r--  1 root root  0 Apr 10 16:02 file_3
-rw-r--r--  1 root root 49 Apr 10 16:05 file_list
#

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

List the directories, having given pattern in the directories name, sorted by creation date

It is for HP-Unix B.11.31. Requirement: 1. List the directories, having given pattern in the directories name, sorted by creation date. Example: Directories with name "pkg32*" or "pkg33*" 2. On the output of 1. list the directories by creation date as sort order, with creation date... (2 Replies)
Discussion started by: Siva SQL
2 Replies

2. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

3. Shell Programming and Scripting

Deleting directories

I want to delete directories which are empty and directories which are 2 level deep. example: /app/data/logs/G07696115/G07696115 So if a same directory is there inside G07696115 with the same name G07696115, i want to find and delete that alone(Make sure you do not touch directory any... (4 Replies)
Discussion started by: gtm004
4 Replies

4. Shell Programming and Scripting

deleting files in sub directories!

Hello out there, I want to setup a crontab feature to remove temporary pdf files from sub directories that are older than 30 days. I want to presevere the directory structer though. I got this far for a command. Will this remove the pdf's in subdirectories or just directly under the pdf folder? If... (6 Replies)
Discussion started by: vsekvsek
6 Replies

5. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

6. Shell Programming and Scripting

How to list all the directories, sub directories in a mount along with size in ascending order?

Hi , I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In a particular mount, have to list all the directories and sub directories along with size of the directory and sub directory in ascending order. Please help me in this regard and many... (4 Replies)
Discussion started by: nmakkena
4 Replies

7. UNIX for Dummies Questions & Answers

deleting older directories

how can we delete directories older than 6 months (9 Replies)
Discussion started by: dnat
9 Replies

8. UNIX for Dummies Questions & Answers

deleting all the sub directories

hello., i am creating a certain sub directories as apart of my requirement, and then later on i have to delete all those sub directories.. ____________________________________________________ DIR1="/home/pavi/cvs/20071426/TEMP" echo " DIR1 is : " $DESTDIR1 echo... (2 Replies)
Discussion started by: pavan_test
2 Replies

9. UNIX for Dummies Questions & Answers

Deleting unamed directories

SCO 5.06 Someone broke into my NT box (grrr) and somehow, they created a bunch of directories w/ subdirectories that appear un-named. I mounted the drive on my unix box using NFS and when I listed the directory in unix, Im getting a bunch of ascii charachters. drwxr-xr-x 8 root group... (4 Replies)
Discussion started by: gseyforth
4 Replies

10. UNIX for Dummies Questions & Answers

deleting nfs directories...

Hi I have Solaris 8 in several Ultras and they share some directories between each others using nfs. (you know, one shares the other mounts the remote directory into a local...). The problem is that one guy (me) deleted a shared directory and now the computer that was mounting it remotely is... (3 Replies)
Discussion started by: piltrafa
3 Replies
Login or Register to Ask a Question