Search code examples
linuxbashshellunix-timestamp

Print the path of a file a day before and a day after in Shell script


I am having a shell script like below

#!/bin/bash

TIMESTAMP=`date "+%Y-%m-%d"`
path=/home/$USER/logging/${TIMESTAMP}/status/${TIMESTAMP}.fail_log

echo filePath=$path

In this script I want to print the path of the failed logs for that particular timestamp.

Now I am able to get the echo to print the path.

How do I print a day before and day after the timestamp? Is it possible to do that?

How Can I do that in a single line of code? Can we do that?


Solution

  • To get tomorrow's data, you can do:

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

    To get yesterday's data, you can do:

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

    To use it in script:

    #!/bin/bash
    
    nextDate=$(date -d '+1 day' "+%Y-%m-%d")
    prevDate=$(date -d '-1 day' "+%Y-%m-%d")
    
    nextDatePath=/home/$USER/logging/${TIMESTAMP}/status/${nextDate}.fail_log
    
    prevDatePath=/home/$USER/logging/${TIMESTAMP}/status/${prevDate}.fail_log