Deleting files older than 30 days based on filename as date

Ari

If I have a folder of files with their filenames as the date they were created:

2019_04_30.txt
2019_04_15.txt
2019_04_10.txt
2019_02_20.txt
2019_01_05.txt

How would I compare the files names against todays current date

$ date "+%Y_%m_%d"
>>> 2019_04_30

If the file names date is greater than 30 days then delete it. I would expect to end up with

2019_04_30.txt
2019_04_15.txt
2019_04_10.txt

I don't have to follow this naming convention, I could use a more suitable date format.

darxmurf

Here is a bash solution.

f30days=$(date +%s --date="-30 days")
for file in 20*.txt; do
    fdate=$(echo $file | tr _ -)
    fsec=$(date +%s --date=${fdate/.txt/})
    if [[ $fsec -lt $f30days ]]; then
        echo "rm $file"
    fi
done

I ended it with "echo rm $file" instead of really deleting your files, this will test the result before.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Gracefully deleting files older than 30 days

From Dev

Delete files with filename date older than 7 days

From Dev

Deleting records older than a month/30 days

From Dev

update column if date is older than 30 days

From Dev

update column if date is older than 30 days

From Dev

How to delete records from database table older than 30 days with based on date column

From Dev

remove line in text file with bash if the date is older than 30 days

From Dev

DateTime not working: See if date is older than 30 days

From Dev

How to zip files older than 30 days in java

From Dev

Powershell move files older than x match on date in filename

From Dev

Java Delete files older than N days with Path, Filename and N days as variable

From Dev

Between older than 30 days and less than 90days

From Dev

Purge by deleting all records older than 180 days from the current date in SQL Server

From Dev

Deleting Files Older Than 7 Days using PowerShell on Windows Server 2012

From Dev

Best Way to check if a java.util.Date is older than 30 days compared to current moment in time?

From Dev

Windows Script to move files older than 30 days with a specific extension to another folder

From Dev

How to archive files older than 7 days with creating one archive for all files with same date?

From Dev

If file modification date is older than N days

From Dev

Find a date older than 15 days in laravel

From Dev

Delete files older than xx days

From Dev

Delete files older than 500 days

From Dev

Delete files older than X days +

From Dev

Delete files older than 500 days

From Dev

Delete files older than X days +

From Dev

Delete all files older than X days

From Dev

Script for moving files that are older than 5 days

From Dev

Remove files older than 5 days in UNIX (date in file name, not timestamp)

From Dev

Open files older than 3 days of date stamp in file name - Python 2.7

From Dev

Select users which last message is older than 30 days

Related Related

  1. 1

    Gracefully deleting files older than 30 days

  2. 2

    Delete files with filename date older than 7 days

  3. 3

    Deleting records older than a month/30 days

  4. 4

    update column if date is older than 30 days

  5. 5

    update column if date is older than 30 days

  6. 6

    How to delete records from database table older than 30 days with based on date column

  7. 7

    remove line in text file with bash if the date is older than 30 days

  8. 8

    DateTime not working: See if date is older than 30 days

  9. 9

    How to zip files older than 30 days in java

  10. 10

    Powershell move files older than x match on date in filename

  11. 11

    Java Delete files older than N days with Path, Filename and N days as variable

  12. 12

    Between older than 30 days and less than 90days

  13. 13

    Purge by deleting all records older than 180 days from the current date in SQL Server

  14. 14

    Deleting Files Older Than 7 Days using PowerShell on Windows Server 2012

  15. 15

    Best Way to check if a java.util.Date is older than 30 days compared to current moment in time?

  16. 16

    Windows Script to move files older than 30 days with a specific extension to another folder

  17. 17

    How to archive files older than 7 days with creating one archive for all files with same date?

  18. 18

    If file modification date is older than N days

  19. 19

    Find a date older than 15 days in laravel

  20. 20

    Delete files older than xx days

  21. 21

    Delete files older than 500 days

  22. 22

    Delete files older than X days +

  23. 23

    Delete files older than 500 days

  24. 24

    Delete files older than X days +

  25. 25

    Delete all files older than X days

  26. 26

    Script for moving files that are older than 5 days

  27. 27

    Remove files older than 5 days in UNIX (date in file name, not timestamp)

  28. 28

    Open files older than 3 days of date stamp in file name - Python 2.7

  29. 29

    Select users which last message is older than 30 days

HotTag

Archive