[Webinar] Streamline your web hosting managementRegister Today


Shell script date function

Posted on 2010-03-29
Medium Priority
Last Modified: 2013-12-27
In a shell scipt i use test command to check for a file which has the date as part of its file name

File name example:- ABCDEFG_20100330.csv.gz

Shell script:-     if ( test ! -f /opt/ABCDEFG_`date +%Y%m%d`* )

This checks for a file which has the current date in its format.  
How do i do the same but check for the day before file. ( eg current date -1day)

I would also need to unzip and move this file to diffrerent directory

Question by:stopm
  • 3
  • 2
LVL 38

Accepted Solution

yuzh earned 500 total points
ID: 29035701
you can do:
yesterday=`TZ=$TZ+24 date +%Y%m%d`
then use:
if ( test ! -f /opt/ABCDEFG_${yesterday}* )
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 29722049
Hi Yuzh,

Can you please explain the below line, what is TZ and why you adding +24

yesterday=`TZ=$TZ+24 date +%Y%m%d`

you are calculation of yesterday is perfect

Author Comment

ID: 29899182
Hi Yuzh

Would the format be different if the script was #!/bin/bash  shell ?

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

LVL 38

Expert Comment

ID: 29899426
TZ  is the Timezone variable for your login shell, +24 add 24 hours to your current timezone, then out put the date (it is the same as yesterday), if you -24 hour from your timezone, it will output tommrrow's time.
Hope it can make sense to you, cheers!
LVL 38

Expert Comment

ID: 29900102
Sorry, didn't read your last comment.
the syntax works for sh/ksh/bash, so #!/bin/bash  should work.

Author Closing Comment

ID: 31708661
Being in NZ time zone i had to change it to +12 for "yesterday" before midday

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question