Hello KLUG,<div>Looking at this again now. So my list file "dir_list" contains all the directory's I want to preserve e.g.</div><div><br></div><div>/Users/administrator</div><div>/Users/user1</div><div>/Users/user5</div>
<div><br>How could I script to delete all directory's in /Users/* excluding the ones in dir_list. David's suggestion of using the find command to generate a list file of the current /Users/* contents is OK, but how do I use this to compare against the exclusion list?</div>
<div><br></div><div>Cheers,</div><div><br></div><div>Nathan. </div><div><br></div><div><br><div class="gmail_quote">On Fri, Jul 15, 2011 at 11:04 PM, Nathan Friend <span dir="ltr"><<a href="mailto:nathan.friend@gmail.com">nathan.friend@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">mmmm OK that's an interesting idea. So the first part of the script builds the contents of the exclusion file...<div>
<br></div><div>Cheers,</div><div><br></div><div><font color="#888888">Nathan.</font><div><div></div><div class="h5"><br><div><br><div class="gmail_quote">On Fri, Jul 15, 2011 at 6:49 PM, David Halliday <span dir="ltr"><<a href="mailto:david.halliday@gmail.com" target="_blank">david.halliday@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>why not create the list using the find command.<div><br></div><div>find directories matching pattern: find -type d -name fred*</div>
<div>then pipe the output into grep: grep -v -f myFilesToKeep.txt</div><div><br></div>
<div>Using -v reverses the output so it only outputs lines that aren't in the list (one per line).</div><div><br></div><div>so: </div><div><div><font face="'courier new', monospace">amon@amon1:~$ find / -maxdepth 1 -type d</font></div>
<div><font face="'courier new', monospace">/</font></div><div><font face="'courier new', monospace">/srv</font></div><div><font face="'courier new', monospace">/sbin</font></div>
<div><font face="'courier new', monospace">/var</font></div><div><font face="'courier new', monospace">/selinux</font></div><div><font face="'courier new', monospace">/cdrom</font></div>
<div><font face="'courier new', monospace">/lib32</font></div><div><font face="'courier new', monospace">/media</font></div><div><font face="'courier new', monospace">/home</font></div>
<div><font face="'courier new', monospace">/sys</font></div><div><font face="'courier new', monospace">/tmp</font></div><div><font face="'courier new', monospace">/mnt</font></div>
<div><font face="'courier new', monospace">/opt</font></div><div><font face="'courier new', monospace">/dev</font></div><div><font face="'courier new', monospace">/usr</font></div>
<div><font face="'courier new', monospace">/root</font></div><div><font face="'courier new', monospace">/bin</font></div><div><font face="'courier new', monospace">/boot</font></div>
<div><font face="'courier new', monospace">/etc</font></div><div><font face="'courier new', monospace">/lost+found</font></div><div><font face="'courier new', monospace">/lib</font></div>
<div><font face="'courier new', monospace">/proc</font></div><div><font face="'courier new', monospace">amon@amon1:~$ more myFilesToKeep.txt </font></div><div>
<font face="'courier new', monospace">tmp</font></div><div><font face="'courier new', monospace">sys</font></div><div><font face="'courier new', monospace">amon@amon1:~$ find / -maxdepth 1 -type d | grep -v -f myFilesToKeep.txt </font></div>
<div><font face="'courier new', monospace">/</font></div><div><font face="'courier new', monospace">/srv</font></div><div><font face="'courier new', monospace">/sbin</font></div>
<div><font face="'courier new', monospace">/var</font></div><div><font face="'courier new', monospace">/selinux</font></div><div><font face="'courier new', monospace">/cdrom</font></div>
<div><font face="'courier new', monospace">/lib32</font></div><div><font face="'courier new', monospace">/media</font></div><div><font face="'courier new', monospace">/home</font></div>
<div><font face="'courier new', monospace">/mnt</font></div><div><font face="'courier new', monospace">/opt</font></div><div><font face="'courier new', monospace">/dev</font></div>
<div><font face="'courier new', monospace">/usr</font></div><div><font face="'courier new', monospace">/root</font></div><div><font face="'courier new', monospace">/bin</font></div>
<div><font face="'courier new', monospace">/boot</font></div><div><font face="'courier new', monospace">/etc</font></div><div><font face="'courier new', monospace">/lost+found</font></div>
<div><font face="'courier new', monospace">/lib</font></div><div><font face="'courier new', monospace">/proc</font></div></div><div><br></div><div>so:</div>
<div><br></div><div>for MYDIR in `<span style="font-family:'courier new', monospace">find / -maxdepth 1 -type d | grep -v -f myFilesToKeep.txt</span>` </div><div>do</div><div> rm -rf $MYDIR</div>
<div>done</div><div><br></div><div><b>NOTE: Do not run this as is! unless you want to be left with only /tmp and /sys on your system!</b></div><div><b><br>
</b></div><div>But that's a starting point, play with the man pages for find and grep.</div><div><br><div class="gmail_quote"><div><div></div><div>On 15 July 2011 14:22, Nathan Friend <span dir="ltr"><<a href="mailto:nathan.friend@gmail.com" target="_blank">nathan.friend@gmail.com</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div>Afternoon KLUG,<div>Last year I wanted a bash script to delete folders if they existed in a predefined list file. The end result was:</div>
<div><br></div><div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">
<div style="color:rgb(80, 0, 80)"><div>while read dir</div><div>do</div><div>if [ -d $dir ]</div><div>then</div></div><div>rm -R $dir</div><div>echo "Directory $dir found and deleted."</div><div>else</div>
<div>echo "Directory $dir not found."</div><div>fi</div><div>done < dir_list</div></div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px"><br></div>
<div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px"><br></div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">
dir_list file contains</div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">/<span style="background-color:rgb(255, 255, 204);color:rgb(34, 34, 34);background-repeat:initial initial">Users</span>/user1</div>
<div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">/<span style="background-color:rgb(255, 255, 204);color:rgb(34, 34, 34);background-repeat:initial initial">Users</span>/user2</div>
<div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">/<span style="background-color:rgb(255, 255, 204);color:rgb(34, 34, 34);background-repeat:initial initial">Users</span>/user3</div>
<div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px"><br></div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">
This year I need to delete all files in the /Users/ folder except the ones listed in dir_list.</div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px"><br></div>
<div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">Any ideas?</div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">
<br></div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">Cheers,</div><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">
<br></div><font color="#888888"><div style="border-collapse:collapse;color:rgb(51, 51, 51);font-family:arial, sans-serif;font-size:13px">Nathan.</div></font></div>
<br></div></div>_______________________________________________<br>
Kent mailing list<br>
<a href="mailto:Kent@mailman.lug.org.uk" target="_blank">Kent@mailman.lug.org.uk</a><br>
<a href="https://mailman.lug.org.uk/mailman/listinfo/kent" target="_blank">https://mailman.lug.org.uk/mailman/listinfo/kent</a><br></blockquote></div><br></div>
<br>_______________________________________________<br>
Kent mailing list<br>
<a href="mailto:Kent@mailman.lug.org.uk" target="_blank">Kent@mailman.lug.org.uk</a><br>
<a href="https://mailman.lug.org.uk/mailman/listinfo/kent" target="_blank">https://mailman.lug.org.uk/mailman/listinfo/kent</a><br></blockquote></div><br></div></div></div></div>
</blockquote></div><br></div>