Sponsored Content
Full Discussion: Count for only todays files.
Top Forums Shell Programming and Scripting Count for only todays files. Post 302799799 by Don Cragun on Saturday 27th of April 2013 05:44:39 PM
Old 04-27-2013
The following Korn shell script uses an indexed array and a test operator that are not defined by the standards, but should work with any ksh or bash shell. With a ksh88 version of the Korn shell, this script may fail if you try to count the lines in more than 1023 files in a single invocation (with ksh93, 4095). I don't know the minimum limits on indexed arrays for bash. This script will process any files with a modification timestamp no older than midnight on the morning of the day the script is invoked. (If you have files with timestamps in the future, they will also be counted. This isn't likely to happen in real world conditions, so I didn't write code to eliminate files from the future. This script will not be bothered by filenames containing whitespace characters.)
Code:
#!/bin/ksh
# Usage: lctoday file...
# This script will evaluate the pathnames given as operands and use "wc -l" to
# count lines in any of them that are regular files creater or modified at or
# after midnight today.  The total number of lines found will be printed to
# standard output.

# Initialize variables:
IAm=${0##*/}                    # This script's name
tmpf=${TMPDIR:-/tmp}/today.$$   # File with timestamp set to midnight today
unset list                      # initialize list of files to process

# Verify that we have at least one operand:
if [ $# -lt 1 ]
then    printf "Usage: %s file...\n" "$IAm" >&2
        exit 1
fi

# Create a temp file with midnight this morning as its timestamp:
if ! eval $(date '+touch -t %Y%m%d0000') "$tmpf"
then    printf "%s: Could not create file marking midnight" "$IAm" >&2
        exit 1
fi

# Select regular files with today's timestamp from the given pathname operands:
for i in "$@"
do      if [ -r "$i" ] && [ -f "$i" ] && [[ ! "$i" -ot "$tmpf" ]]
        then    # We have a readable regular file that was created or modified
                # today.
                list[${#list[@]}]="$i"  # Add it to the array of files to count.
        fi
done

# We have our list, get the line count of all of the files in the list.
wc -l "${list[@]}" /dev/null | {
        # If there is more than one file in the list, the last line wc prints
        # will be a total for all files processed and the word "total".  If
        # there is only one file, the only liee wc prints will contain the
        # total for that file and the filename.  Either way, we just need to
        # save the 1st word from the last line.
        # The /dev/null operand protects us if a single file is given and its
        # pathname contains a newline character, and gets rid of a special case
        # that would be otherwise needed if no files are selected.
        while read -r count other
        do      sum=$count
        done
        # Print the total number of lines reported by wc.
        echo $sum
}

# Cleanup.  Remove the temp file we created:
rm "$tmpf"

This script may look big, but most of it is comments. To run it, save it in a file named lctoday, make it executable by running the command:
Code:
chmod +x lctoday

and invoke it with the list of files you want it to evaluate. For example:
Code:
./lctoday *.txt

or
Code:
./lctoday c90.txt n5.txt t1.txt k3.txt h9.txt s1.txt n2.txt a123.txt

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script help to eliminate files of todays date

Hi I am very new to shell scripting and have written a script (below). However the directory I am searching will contain a file with a .trn extension each day which I want to eliminate. Each day the file extension overnight will change to trx, if this fails I want to know. Basically what I... (2 Replies)
Discussion started by: richM
2 Replies

2. Shell Programming and Scripting

Count todays created files and old files

Hello experts, I used following approach to get listing of all files of remote server. Now I have remote server file information on same server. I am getting listing in the output.txt I want to count today's created files and old files. I want to compare the numbers... (11 Replies)
Discussion started by: dipeshvshah
11 Replies

3. Shell Programming and Scripting

Find files having todays date and hostname in its name.

Hello, I am quite new to unix/shell and want to write a script using bash which will process the files. Basically i want to search files having name as "date+hostname+somestring.out" i am using below variables and then will use them in find command :- TODAY_DATE=$('date +%d')... (5 Replies)
Discussion started by: apm
5 Replies

4. UNIX for Dummies Questions & Answers

need to zip all the files excluding todays file

HI All, At present in my server the log folder was filled up and causing memory issue. So I am planning to write a script in such a way that the files which are older than 30 days will be deleted and also need to find the files which were not compressed and need to compress this file.... (4 Replies)
Discussion started by: arumilli
4 Replies

5. Shell Programming and Scripting

Find and copy files based on todays date and search for a particular string

Hi All, I am new to shell srcipting. Problem : I need to write a script which copy the log files from /prod/logs directory based on todays date like (Jul 17) and place it to /home/hzjnr0 directory and then search the copied logfiles for the string "@ending successfully on Thu Jul 17". If... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

6. Shell Programming and Scripting

How to list todays and yesterdays .rej files from a directory?

I am trying to display todays and yesterdays .rej files from a directory. ls -lrt *.rej | grep 'Aug 12' ; ls -lrt *.rej | grep 'Aug 13' Which is working as above. But i want take 'Aug 12' and 'Aug 13' from a variable and the command should work everyday. I am able to get todays files by... (9 Replies)
Discussion started by: GopalKrishnaP
9 Replies

7. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

8. UNIX for Dummies Questions & Answers

Script to keep todays files based on Timestamp

Hi i need to keep todays files based on timestamp and archive the remaining files ex: Managerial_Country_PRD_20150907.csv Managerial_Country_PRD_20150906.csv Managerial_Country_PRD_20150905.csv (2 Replies)
Discussion started by: ram1228
2 Replies

9. Shell Programming and Scripting

Script to keep todays files based on Timestamp

Hi i need to keep todays files based on timestamp and archive the remaining files ex: Managerial_Country_PRD_20150907.csv Managerial_Country_PRD_20150907.csv Managerial_Country_PRD_20150906.csv Managerial_Country_PRD_20150905.csv (6 Replies)
Discussion started by: ram1228
6 Replies

10. Shell Programming and Scripting

Find todays files

I am trying to find todays files only in the "root directories like /etc, /opt etc. I do not want to search in my home directory or any of its subdirectories. That's hard because a directory listing does not show the year? -rw------- 1 andy andy 22487 Oct 12 13:40 .xsession-errors ... (8 Replies)
Discussion started by: drew77
8 Replies
All times are GMT -4. The time now is 09:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy