2013/10/16

Remove Directories Using BASH Find Command

This post is to help me remember how to use the BASH find command to search and remove directories on my linux server. I currently am running an rsync backup command and storing the backups on the server. Each backup is stored as a folder as a date and time. For example '2013-1016-1400' would be a backup that occurred on October 16, 2013 at 1400 hours (2 pm).

List of Commands with Explanation

list only directories
  ls -d */

Search examples

! = do not find
example: ! -name "2013*" # do not find file name beginning with "2013"

Find year 2013, months 5-8, day 31, but not time 22**
 find . -maxdepth 1 -name "2013-0[578][3][01]-*" ! -name "*-22??"


Find year 2013, months 5-8, day 31, but not time 22**, and then delete directory
find . -maxdepth 1 -name "2013-0[578][3]1-*" ! -name "*31-22*" -print0 | xargs -0 rm -vR

Find year 2013, month 6, all days, but not date and time 0628-2001
 find . -maxdepth 1 -name "2013-06*-*" ! -name "*0628-2001"

Find year 2013, month 6, all days, but not date and time 0628-2001, and then delete directory
 find . -maxdepth 1 -name "2013-06*-*" ! -name "*0628-2001" -print0 | xargs -0 rm -vR








No comments:

Post a Comment