I will get every day the below files but some of them did not arrive on the same day. I have to get the count only with the todays date.
Finally, two things i need to do.
Find the 8 files with todays date[What ever the files with today's date and get the total file count only for those files.]
Code:
c90.txt
n5.txt
t1.txt
k3.txt
h9.txt
s1.txt
n2.txt
a123.txt
Out of 8 files, only 5 files with today'date then i have to get the count for only those 5 files.
Example:
ls -lrt c90.txt n5.txt t1.txt k3.txt h9.txt s1.txt n2.txt a123.txt
-rw-rw-r-- kkk klkl 980 Apr 26 19:00 c90.txt
-rw-rw-r-- kkk klkl 80 Apr 26 19:00 n5.txt
-rw-rw-r-- kkk klkl 12890 Apr 26 19:00 t1.txt
-rw-rw-r-- kkk klkl 890801 Apr 27 19:00 k3.txt
-rw-rw-r-- kkk klkl 9089810 Apr 27 19:00 h9.txt
-rw-rw-r-- kkk klkl 718980 Apr 27 19:00 s1.txt
-rw-rw-r-- kkk klkl 12980 Apr 27 19:00 n2.txt
-rw-rw-r-- kkk klkl 678980 Apr 27 19:00 a123.txt
wc -l c90.txt n5.txt t1.txt k3.txt h9.txt s1.txt n2.txt a123.txt
10 c90.txt
20 n5.txt
30 t1.txt
40 k3.txt
50 h9.txt
60 s1.txt
70 n2.txt
80 a123.txt
360 total
Based on the above, If i take the counts for the all the files i will get total is 360 but only for todays date files count is 300
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)