LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 06-02-2006, 09:52 AM   #1
ft77
LQ Newbie
 
Registered: Jun 2006
Distribution: Ubuntu Dapper
Posts: 1

Rep: Reputation: 0
Question 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?

Thanks
 
Old 06-02-2006, 10:47 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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:
Code:
#! /bin/bash

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

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

Last edited by spirit receiver; 06-02-2006 at 10:49 AM.
 
Old 06-02-2006, 10:56 AM   #3
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
Actually, if it was something as simlpe as "yesterday" you can

Code:
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.
 
1 members found this post helpful.
Old 06-02-2006, 10:56 AM   #4
madluther
Member
 
Registered: Aug 2004
Distribution: LFS
Posts: 350

Rep: Reputation: 30
Or,

Code:
date -d "-1 day" +"%Y%m%d"
Mad.
 
  


Reply

Tags
bash, shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date in a filename. Garp Linux - General 7 12-09-2005 02:57 PM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 09:41 AM
Script add date to filename amphion Linux - Newbie 2 06-02-2004 08:12 AM
use date as output filename? wijnands Linux - Newbie 1 05-12-2004 04:39 PM
Script to change several filename jan_81 Linux - Newbie 5 04-28-2004 12:10 PM


All times are GMT -5. The time now is 01:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration