How to find yesterdays file - shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find yesterdays file - shell script
# 1  
Old 06-10-2010
How to find yesterdays file - shell script

Hey guys - i have a script (below) that searches for current files in a particular directory.

However i was wondering how to make it search for "yesterdays" file. For instance it looks for a file from yesterday and no older than that.

I used stat command to check for file information:


Code:
function checkStat

{

    # make sure stat command is installed

    which stat > /dev/null 2>&1

    if [ $? -eq 1 ]; then
        echo "stat command not found!"
        exit 2
    fi

}



function processFile

{

    if [ -s ${1} ]; then
        VAR="$(stat -c %y ${1})"
        VAR2="${VAR:0:10}"

        FILE_TIME=$(ls -l $1| cut -d" " -f6,7,8,9)
        FILE_SIZE=$(du -sh $1 | cut -f1)
        if [ $NOW == $VAR2 ]; then
            echo "Current File Found: ${1} DATED $FILE_TIME - SIZE $FILE_SIZE" | tee -a ${CURRENT_FILE}
        else
            erroremail=1
            echo "Outdated File Found: ${1} DATED $FILE_TIME - SIZE $FILE_SIZE" | tee -a ${OLD_FILE}
        fi

    else
        erroremail=1
        echo "File Not Found OR 0KB in SIZE: ${1}" | tee -a ${FILE_NOT_FOUND}
    fi



}



#############################

# Main

#############################



# Verify the stat command exists

checkStat



# Create log file

touch  ${LOG_FILE}



# Process the files

for d in ${DIR[@]}; do
    for f in ${FILE[@]}; do
        processFile "${d}/${f}"
    done
done

# 2  
Old 06-10-2010
touch filetimes to two dummy files, use find to gety a list of your files
example:
Code:
days_before()
{
   perl -e ' ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - (86400 * $ARGV[0]));
           printf "%d%02d%02d%s\n", $year+1900, $mon+1, $mday, "2359.59"; '  $1
}

touch -t $(days_before 1) dummy
touch -t $(days_before 2) ol_dummy
find . \( ! -newer dummy -a -newer ol_dummy \) -exec ls -l {} \;

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 06-11-2010
Thanks. I am trying to find a way to accomplish this in unix shell script.
# 4  
Old 06-11-2010
Unless you have GNU date there is no simple code to do this. CFA Johnson, a forum member, has a website with a shell script library to do date arithmetic. The code is good, but doing dates in shell in an involved.

Rolling your own date routines is not best practice. Finding yesterday when the date today is March 1 is a classic example. You have to know whether it is a leap year or not, then roll back to Feb 28 or 29.

The standard way to get yesterday:

calculate the current Julian day, subtract one, then convert Julian -> Gregorian date.

get current time in UNIX epoch seconds and subtract 86400, then convert epoch seconds to a Gregorian date.

For production systems always use known good libraries for date calculations.
# 5  
Old 06-11-2010
eerily similar thread and solution...restricted to the shell as well: https://www.unix.com/shell-programmin...ays-ago-2.html
This User Gave Thanks to curleb For This Post:
# 6  
Old 06-12-2010
Just a note - when you get working respsonses it makes folks here feel like it is homework, when it cannot be used. We have a special forum for homework.

awk, perl, python, grep are all external to shell and require special knowledge of syntax separate from shell. awk is everywhere, perl is close to everywhere; python and ruby are somewhat less ubiquitous.

Straight shell may be possible but for a lot of things; it will require a special tested shell library to run. Generally experienced programmers resort to simpler methods: another environment like awk.

If you have to have shell - get GNU date. That allows date arithmetic operations. Although using date is not straight shell either.

I don't know where folks draw the line between shell and other things, but in reality about 80% of the commonly used "shell" commands are not shell builtins, they are separate code - find, head, tail...etc. try the which command or the type builtin to see what a command really is.

bash will let you create custom builtins, you have to code it in C however.
# 7  
Old 06-13-2010
Thank you guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Shell script to find file type

1. The problem statement, all variables and given/known data: Write a shell script that takes a single command line parameter, a file path (might be relative or absolute). The script should examine that file and print a single line consisting of the phrase: Windows ASCII if the files is an... (4 Replies)
Discussion started by: kwatt019
4 Replies

2. UNIX for Dummies Questions & Answers

Find error in the shell script file

echo "******Select Option:******" echo "1 - script1" echo "2 - script2" echo "3 - script3 " read option echo "You have selected" $option"." if then /scratch/username/script1.sh elif then /scratch/username/script2.sh elif then /scratch/username/script3.sh else echo "Please try again... (12 Replies)
Discussion started by: Dish
12 Replies

3. Shell Programming and Scripting

How to find invalid URL in a text file using shell script?

How to find and remove invalid URLs in a text file using shell script? (1 Reply)
Discussion started by: vel4ever
1 Replies

4. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

5. Shell Programming and Scripting

Shell Script Find in File

Right, noob to shell scripting, playing a round for practice, wrote the following but it doesn't seem to work as expected, how could I fix/improve this script? #!/bin/bash #set -v #set -x case $# in 1) echo Searching for $1 in '*'; find . -iname '*' 2>/dev/null | xargs grep "$1" -sl... (3 Replies)
Discussion started by: Pezmc
3 Replies

6. Shell Programming and Scripting

Shell script to find specific file name and load data

I need help as to how to write a script in Unix for the following: We have 3 servers; The mainframe will FTP them to a folder. In that folder we will need the script to look and see if the specific file name is there and load it to the correct table. Can anyone pls help me out with... (2 Replies)
Discussion started by: msrahman
2 Replies

7. UNIX for Dummies Questions & Answers

shell script to find noof characters in a file name

hiiii shell script to find noof characters in a file name, when you run ls -l (using awk) I tried with this ls -l > temp awk -F"," '{print $1 " " expr length $9}' temp but it give some other value instead of file name length (error value like , 563,54,55,56....).How to prnint the... (10 Replies)
Discussion started by: krishnampkkm
10 Replies

8. Shell Programming and Scripting

Korn Shell Script - Getting yesterdays date

I need to get yesterdays date in the format yyyymmdd I can get today's date simply enough - 20031112 Is there any way to substract 1 from this easily enough in korn shell script? It has to be korn shell and not perl (20 Replies)
Discussion started by: frustrated1
20 Replies

9. Shell Programming and Scripting

using find to get all of yesterdays files

i tried to use "find" to get all of yesterdays files but missed something in the 24 hours logic. can anybody help me with this one? i thought that -daystart -atime 1 was enough but i got more files (2 Replies)
Discussion started by: progressdll
2 Replies

10. Shell Programming and Scripting

Shell script to Find file size

Hi, I am writing a script which takes the input file name and concat as a new file by appending a "1" to the file name. However i am not able to get the size of this new file. I am not sure where i am going wrong. Please check the script and help me get this working. #!/bin/sh ... (1 Reply)
Discussion started by: ragsnovel
1 Replies
Login or Register to Ask a Question