[Klug-general] Bash script to delete folders if they exists (revisited)

David Halliday david.halliday at gmail.com
Mon Aug 8 09:43:49 UTC 2011


Look again at my example. I use the "grep -v" to negate the behaviour of
grep

$ more file.txt
foo
bar
abc

$ grep "foo" file.txt
foo

$ grep -v "foo" file.txt
bar
abc


On 7 August 2011 22:30, Nathan Friend <nathan.friend at gmail.com> wrote:

> Hello KLUG,
> Looking at this again now.  So my list file "dir_list" contains all
> the directory's I want to preserve e.g.
>
> /Users/administrator
> /Users/user1
> /Users/user5
>
> 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?
>
> Cheers,
>
> Nathan.
>
>
> On Fri, Jul 15, 2011 at 11:04 PM, Nathan Friend <nathan.friend at gmail.com>wrote:
>
>> mmmm OK that's an interesting idea.  So the first part of the script
>> builds the contents of the exclusion file...
>>
>> Cheers,
>>
>> Nathan.
>>
>>
>> On Fri, Jul 15, 2011 at 6:49 PM, David Halliday <david.halliday at gmail.com
>> > wrote:
>>
>>>
>>> why not create the list using the find command.
>>>
>>> find directories matching pattern: find -type d -name fred*
>>> then pipe the output into grep: grep -v -f myFilesToKeep.txt
>>>
>>> Using -v reverses the output so it only outputs lines that aren't in the
>>> list (one per line).
>>>
>>> so:
>>> amon at amon1:~$ find / -maxdepth 1 -type d
>>> /
>>> /srv
>>> /sbin
>>> /var
>>> /selinux
>>> /cdrom
>>> /lib32
>>> /media
>>> /home
>>> /sys
>>> /tmp
>>> /mnt
>>> /opt
>>> /dev
>>> /usr
>>> /root
>>> /bin
>>> /boot
>>> /etc
>>> /lost+found
>>> /lib
>>> /proc
>>> amon at amon1:~$ more myFilesToKeep.txt
>>> tmp
>>> sys
>>> amon at amon1:~$ find / -maxdepth 1 -type d | grep -v -f myFilesToKeep.txt
>>> /
>>> /srv
>>> /sbin
>>> /var
>>> /selinux
>>> /cdrom
>>> /lib32
>>> /media
>>> /home
>>> /mnt
>>> /opt
>>> /dev
>>> /usr
>>> /root
>>> /bin
>>> /boot
>>> /etc
>>> /lost+found
>>> /lib
>>> /proc
>>>
>>> so:
>>>
>>> for MYDIR in `find / -maxdepth 1 -type d | grep -v -f myFilesToKeep.txt
>>> `
>>> do
>>>   rm -rf $MYDIR
>>> done
>>>
>>> *NOTE: Do not run this as is! unless you want to be left with only /tmp
>>> and /sys on your system!*
>>> *
>>> *
>>> But that's a starting point, play with the man pages for find and grep.
>>>
>>> On 15 July 2011 14:22, Nathan Friend <nathan.friend at gmail.com> wrote:
>>>
>>>> Afternoon KLUG,
>>>> Last year I wanted a bash script to delete folders if they existed in
>>>> a predefined list file.  The end result was:
>>>>
>>>> while read dir
>>>> do
>>>> if [ -d $dir ]
>>>> then
>>>> rm -R $dir
>>>> echo "Directory $dir found and deleted."
>>>> else
>>>> echo "Directory $dir not found."
>>>> fi
>>>> done < dir_list
>>>>
>>>>
>>>> dir_list file contains
>>>> /Users/user1
>>>> /Users/user2
>>>> /Users/user3
>>>>
>>>> This year I need to delete all files in the /Users/ folder except the
>>>> ones listed in dir_list.
>>>>
>>>> Any ideas?
>>>>
>>>> Cheers,
>>>>
>>>> Nathan.
>>>>
>>>> _______________________________________________
>>>> Kent mailing list
>>>> Kent at mailman.lug.org.uk
>>>> https://mailman.lug.org.uk/mailman/listinfo/kent
>>>>
>>>
>>>
>>> _______________________________________________
>>> Kent mailing list
>>> Kent at mailman.lug.org.uk
>>> https://mailman.lug.org.uk/mailman/listinfo/kent
>>>
>>
>>
>
> _______________________________________________
> Kent mailing list
> Kent at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/kent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/kent/attachments/20110808/17b9a6fb/attachment-0001.htm>


More information about the Kent mailing list