-1

I am trying to write a Bash script which will delete a bunch of folders which are older than 30 days,by comparing the folder names to the system time.

These are the folders :

20201001/ 20201002/ 20201003/ 20201106/ 20201108/ 20201109/ 20201201/

I am trying to read the these folder names in a bash script and compare it with system timestamp and delete the folders which are 30 days old.

This is what i have right now :

now="$(date +'%Y%m%d')"
printf "$now"
echo
echo */

This is the output i get :

20201203
20201001/ 20201002/ 20201003/ 20201106/ 20201108/ 20201109/ 20201201/

echo */ gives a list of folder but how to pass it into the deletion command line:

find list -mtime +30 -exec rm {} \;

Httqm
  • 799
  • 7
  • 13
  • 1
    How do you measure folder age? Is the folder age measured using it's filename or modification timestamp as stored on filesystem? – KamilCuk Dec 03 '20 at 11:05
  • folder age = folder name(the string) - current system timestamp –  Dec 03 '20 at 11:07
  • I do not understand. Which folders are older then 30 days? Is a folder created today with a name `19900101` older then 30 days? – KamilCuk Dec 03 '20 at 11:11
  • 1
    example `20201203-20201001`..here `20201203` is todays timestamp..`20201001` is the folder name –  Dec 03 '20 at 11:12
  • Is folder name not same as ctime? – rohit89 Dec 03 '20 at 12:53

2 Answers2

0

Since I've had similar requirements and questions in the past I wanted to share my solution approach here.

For testing here, I've created a data structure with test/<nameFolderByDate>. If listing the file statistics via stat, some meta data including access, modification and change time is given.

stat -c "%y %s %n" test/*
2020-12-03 14:11:38.788388456 +0100 4096 test/20200101
2020-12-03 14:11:41.288389539 +0100 4096 test/20200201
2020-12-03 14:11:42.984390273 +0100 4096 test/20200301
2020-12-03 14:09:14.747326011 +0100 4096 test/20200401

As one can see the timestamps might (sometimes) not fit to the structure <nameFolderByDate>.

Therefore I've created a small script which sets the modification time (mtime) to the date within the name. I use this approach under other circumstances too.

cat cleanupData.sh
#!/bin/bash

echo ""
echo "Set modification time to date in name"

for FILENAME in test/*; do

    echo ${FILENAME}

    NAMETIME=$(echo ${FILENAME} | cut -d "/" -f 2)
    echo ${NAMETIME}

    TIMESTAMP=$(date --date="${NAMETIME}" "+%Y%m%d%H%M")
    echo ${TIMESTAMP}

    touch -m -a -t ${TIMESTAMP} ${FILENAME}

done

exit

After normalizing the data structure the result with stat is

./cleanupData.sh

Set modification time to date in name
test/20200101
20200101
202001010000
test/20200201
20200201
202002010000
test/20200301
20200301
202003010000
test/20200401
20200401
202004010000

stat -c "%y %s %n" test/*
2020-01-01 00:00:00.000000000 +0100 4096 test/20200101
2020-02-01 00:00:00.000000000 +0100 4096 test/20200201
2020-03-01 00:00:00.000000000 +0100 4096 test/20200301
2020-04-01 00:00:00.000000000 +0200 4096 test/20200401

as expected in this case and find can be used as required, i.e.

find test -mtime +300
test/20200101
test/20200201

I will leave other parts of creating a fully functional script to the audience.

Thanks also to

U880D
  • 8,601
  • 6
  • 24
  • 40
0

You can skip echo and use find command directly with following options:

-maxdepth 1         -> exclude subdirectories
! -path .           -> exclude current working directory
-type d             -> exclude files
-mtime +30          -> exclude directory modified last 30 days
-exec rm -r {} \;   -> to remove the directories


find . -maxdepth 1 ! -path . -type d -mtime +30 -exec rm -r {} \;

Otherwise, if you want to pass directory list to find command you can use ls and xargs commands:

ls -d */ | xargs -I % find % -type d -maxdepth 0 -mtime +30 -exec rm -r {} \;

Edit: For a security point of view it's better to use $arg instead of % as explain here: Running multiple commands with xargs . It also allow you to use echo instead of ls command

echo */ | xargs sh -c 'for arg do find $arg -type d -maxdepth 0 -mtime +30 -exec rm -r {} \; ; done'
Danael
  • 31
  • 3