linux - delete directories according to directory name date -


the following command line delete directories older 180 days

 find /path/to/base/dir -type d -ctime +180 -exec rm -rf {} \; 

but syntax looking on directory time stamp

for example if time stamp of directory

 jan  2 08:17 

then directory deleted

but want remove directory according directory date name

for example

directories date names:

 drwxr-xr-x 2 root root 4096 dec  2 08:17 01012014  drwxr-xr-x 2 root root 4096 dec  2 08:17 01022014  drwxr-xr-x 2 root root 4096 dec  2 08:17 01032014 

should deleted because older 180 days according date name

please advice how implemente in bash script

remark - dir name daymonthyear

try doing this:

find /path/to/base/dir -type d -exec bash -c '     cd ${1%/*}     dir=${1##*/} day=${dir:0:2} month=${dir:2:2} year=${dir:4:4}     [[ $dir =~ [0-9]{8} && $year$month$day < 20140605 ]] && echo rm -rf "$1" ' -- {} \; 
  • find can execute external command, here yield
  • -exec switch can permit that
  • -- stop of arguments
  • {} current parameter bash invocation, file full path
  • ${1%/*} remove what's after last leading / : path (parameter expansion)
  • ${1##*/} remove what's before first / : dirname (parameter expansion)

so split day, month, year parameter expansions compare later lexically. use form yyyymmdd when can, it's easiest sort , manipulate. can test command ensure want, then, remove echo within snippet.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -