Shell Script not showing accurate Time Stamp and Size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script not showing accurate Time Stamp and Size
# 1  
Old 05-17-2010
Shell Script not showing accurate Time Stamp and Size

Hey guys - I have made this script and for some reason, I dont see time stamp as "Month-Day-YYYY Hours-Mins" - all i see is Month and Day.

Also, my file size is approximated. For example, if the size is 19,606KB - the script shows as 20M. Is there a way to see the exact file size?

How do i accomplish the above tasks?

Code:
#!/bin/bash

# setting default path
PATH="/usr/bin:/bin:/usr/sbin"
NOW=$(date +"%Y-%m-%d")

LOG_FILE=/tmp/myemail.txt
FILE_NOT_FOUND="/tmp/missingfiles.txt"
CURRENT_FILE="/tmp/currentfiles.txt"
OLD_FILE="/tmp/oldfiles.txt"

# make sure we create a new copy of these files
rm -f $LOG_FILE $FILE_NOT_FOUND $CURRENT_FILE $OLD_FILE 

# List the directories and files to process. 

DIR="dir1 dir2"

FILE="file 1 file 2"



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

# Define functions

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



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)
    FILE_SIZE=$(du -sh $1 | cut -f1)
        if [ $NOW == $VAR2 ]; then
            echo "Current File Found: ${1} $FILE_TIME - SIZE $FILE_SIZE" | tee -a ${CURRENT_FILE}
        else
            erroremail=1
            echo "Old File Found: ${1} $FILE_TIME SIZE - $FILE_SIZE" | tee -a ${OLD_FILE}
        fi

    else
        erroremail=1
        echo "File Not Found: ${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



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

#Email 

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



if [ $erroremail -eq 1 ] ; then

       SUBJECT="ERROR: File Problem"

else

       SUBJECT="OK: File Current"

fi

# lets populate the sendmail files

echo "To:someone@nowhere.com" >>$LOG_FILE
echo "Subject:$SUBJECT" >> $LOG_FILE
# Append list of current files to Email body
# Make sure we display error message only once

error_display=false
# Append list of old files to Email body
if [ -s $OLD_FILE ]; then
    echo "*********************ERROR MESSAGES********************" >> $LOG_FILE
    error_display=true    
    echo "List of old files" >> $LOG_FILE
    cat $OLD_FILE >> $LOG_FILE
fi
        
# blank lines between two data
echo >> $LOG_FILE
echo >> $LOG_FILE

# Append list of missing files to Email body
if [ -s $FILE_NOT_FOUND ]; then
    if [ "$error_display" = "false" ]; then
        echo "*********************ERROR MESSAGES********************" >> $LOG_FILE
    fi        
    echo "List of missing files" >> $LOG_FILE
    cat $FILE_NOT_FOUND >> $LOG_FILE
fi
# blank lines between two data
echo >> $LOG_FILE
echo >> $LOG_FILE

if [ -s $CURRENT_FILE ]; then
    echo "****************GOOD MESSAGES*************************" >> $LOG_FILE
    echo "List of current files" >> $LOG_FILE
    cat $CURRENT_FILE >> $LOG_FILE
fi

    

# send an email using /sbin/sendmail

sendmail -t < $LOG_FILE

if [ $? -eq 0 ]; then
    echo "Sent mail successfully"
else
    echo "Error sending mail"
fi

# cleanup files
rm -f   $LOG_FILE $FILE_NOT_FOUND $CURRENT_FILE $OLD_FILE

# 2  
Old 05-17-2010
If you need see the real size, change below line: (in fact I don't prefer the cut command)

Code:
FILE_SIZE=$(du -sh $1 | cut -f1)

to

Code:
FILE_SIZE=$(ls -l $1 | cut -d" " -f5)

and leave this for you to update by yourself. below line used for file time, which shows only Month and day.

Code:
FILE_TIME=$(ls -l $1| cut -d" " -f6,7)


Last edited by rdcwayx; 05-17-2010 at 09:50 PM..
# 3  
Old 05-17-2010
The timestamp info for ls will change to drop the HH:MM info after six months. It will then read the YYYY stat for the file in question.

rdcwayx is correct to advise you pick the proper column for the file size out of ls, unless your original intention with du was to give a 'human readable' file size... You can still get the file stat info to indicate the real time stamp, but you'd need to dig harder to get it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

AIX dump device not showing accurate size

I am trying to configure dump devices on my AIX server. Running 7100-03-04-1441. My dump device needs to be about 2GB in size. My PP Size is 1024MB, so I create the device with 2 PPs. When I run lslv on the dump device, it shows the 2 PPs, and a PP Size of 1024 megabytes. However, a dumpcheck... (4 Replies)
Discussion started by: paqman
4 Replies

2. Shell Programming and Scripting

Shell Script | Parse log file after a given date and time stamp

I am developing one script which will take log file name, output file name, date, hour and minute as an argument and based on these inputs, the script will scan and capture all the error(s) that have been triggered from a given time. Example: script should capture all the error after 13:50 on Jan... (2 Replies)
Discussion started by: ROMA3
2 Replies

3. Shell Programming and Scripting

Perl:Script to append date and time stamp

Help with Perl script : I have a web.xml file with a line <display-name>some_text_here</display-name> Need to append the current date and time stamp to the string and save the XML file Something like <display-name>some_text_here._01_23_2014_03_56_33</display-name> -->Finally want... (5 Replies)
Discussion started by: gaurav99
5 Replies

4. Red Hat

Store time stamp for .csh shell users

How do I store the time stamps of each command executed for C shell users? However, I am able to set this for Bash users with this command: export HISTTIMEFORMAT="%F %T " This is how a typical .cshrc look like for a Cshell user: # more .cshrc # necessary to get hostname... (3 Replies)
Discussion started by: hedkandi
3 Replies

5. Shell Programming and Scripting

Run a script when a file is modified/time stamp changed

Hi, I need to run a script file which uses a file and that file is modified as and when some alarms generated, it is not based on any fixed time period.. it may be modified even once in a minute for some time and once in 30 min or once in 20 min. Hence i need to watch for the timestamp change of... (3 Replies)
Discussion started by: aemunathan
3 Replies

6. Hardware

time accurate simulator

Hi all, Does anybody know how to build a cycle accurate simulator? I've looked at QEMU and SESC simulator, but they are only cycle accurate for the CPU part. I'm looking for a simulator which can estimate the performance including CPU and I/O. For example, when I configure the simulator by SSD... (2 Replies)
Discussion started by: qqkk88
2 Replies

7. Shell Programming and Scripting

Changing File Time Stamp (Bash Script)

I need some help recovering from a "slight" screwup. We just moved 3 TB of data from one RAID Array to another. Low lever archive files. This was done with a regular cp (for some reason) and now we have lost all the timestamps on the files, and we urgently need to get the timestamps back on these... (7 Replies)
Discussion started by: chj
7 Replies

8. Shell Programming and Scripting

append date time stamp via ftp script

I have searched several thread and not found my solution, so I am posting a new qustion. I have a very simple script on an AIX server that FTPs 2 files to a MS FTP server. These 2 files are created on the AIX server every hour, with a static name. I need to FTP the files to the MS server, but... (1 Reply)
Discussion started by: sknisely
1 Replies

9. Shell Programming and Scripting

time stamp perl script error out of range 1..31

Hi, while running the perl script i am getting this error message , Day '' out of range 1..31 at rsty.sh line 44 what do iam missing in the script, any suggestion #!/usr/bin/perl use Time::Local; my $wday = $ARGV; my $month = $ARGV; # convert the month shortname into 0-11 number if... (4 Replies)
Discussion started by: saha
4 Replies

10. Shell Programming and Scripting

script for listing files of today's time stamp

Hi, I need to write a script,which behaves like this, Given the folder name, it should list the files with today's timestamp. Please direct me on this. Thanks. (2 Replies)
Discussion started by: kid123
2 Replies
Login or Register to Ask a Question