-   Linux - General (
-   -   Script to change date in filename minus one day (

ft77 06-02-2006 09:52 AM

Script to change date in filename minus one day
Hi, this is my first post at LQ. I have been listening to the podcast for a while and have just joined. I know the value of the site as I have found the answers to questions in the past by way of Google directing me here.
I look forward learning some new skills and helping out where I can.

I would like to help to change some filenames.

Currently they are in a WORDS1_YYYY_MM_DD_WORDS2 and I would like to change the both sets of words and change the date to be in the same format but minus one day.

I know it's easy to change the words with a simple sed command but I don't know how to fix the date section. Can someone help me out with this please?


spirit receiver 06-02-2006 10:47 AM

So, welcome to LQ (although I'm not yet naturalized myself, I guess).

I think the easiest solution to your problem is to convert the date to seconds since epoch and then subtract 86400. From your post I understand that you know how to extract the date and convert it to the format "YYYYMMDD" . Then you can use something like:

#! /bin/bash

# date set to YYYYMMDD
TODAY=$(date +'%Y%m%d')

echo "Yesterday was" $(date --date=@$(( $(date --date=$TODAY +%s) - 86400 )) +'%Y %m %d')

Hobbletoe 06-02-2006 10:56 AM

Actually, if it was something as simlpe as "yesterday" you can


date --date='1 day ago' +%Y%m%d
I'm having a problem trying to figure out how to feed it a date other than today. I'll keey looking though.

madluther 06-02-2006 10:56 AM



date -d "-1 day" +"%Y%m%d"

All times are GMT -5. The time now is 11:27 AM.