AUTO_F1_20140610.TXT -- this file , i received on 06/10/2014---June/10/2014
LA_AUTO_06112014.TXT -- this file , i received on 06/11/2014---June/11/2014
so i received the above files on 10 & 11th of June.
we can receive files in any format , either YYYYMMDD or DDMMYYYY.
Okay, now I'm a little confused as you say the files are one thing then your format is another.
I think it safest to assume you really mean it's either YYYYMMDD or MMDDYYYY
Consider this rather bald code:-
Code:
# Set the three values for the current date
date +'%Y %M %D' | read Cur_Y Cur_M Cur_D
target_dir=/tmp/${Cur_Y}_${Cur_M} # Not sure where you want to put them
# Get last month's numeric value
((LastM=$Cur_M=1))
if [ $Last_M -eq 0 ] # Have we gone back from January?
then
Last_M=12 # Set to December
((Last_Y=$Cur_Y-1)) # Take one from year
else
Last_Y=$Cur_Y
fi
# Process files based on the expression and the date range we need
for file in ?UTO_??_${Last_Y}${Last_M}[1-3][0-9].TXT ??_?UTO_${Last_M}[1-3][0-9]${Last_Y}.TXT
do
printf "Found file \"$file\" to move.\n"
mv $file $target_dir
done
The last bit looks frightful but it's really just glueing the various variables together. It is looking for files in the current directory matching the two expressions. Looking at the first one, we have:-
? representing any single character
UTO_ as literal text
?? any two characters
_ as literal text
${Last_Y}${Last_M} being the value of the two variables. The braces explicitly mark the variable names
[1-3][0-9] literal character 1 followed by any character from the range zero to nine, i.e. the day is 10th to 39th (if they exist
.TXT as literal text
Apart from calling the date command, these are all processed within the shell, so if you have lots of files, you don't have the overhead of repeatedly calling external programs which can slow you down considerably.
Does this get you somewhere towards your need? Apologies if I've missed the point.
Tossed this together. Should give you a base to start.
Code:
#!/bin/bash
#
# check command-line for correct usage
if [ $# -ne 2 ]; then
echo "Usage: ${0##*/} </path/to/directory> <four-digit year>"
exit 1
fi
# store the given path
pth=$1
# store the year
year=$2
# loop through the given directory
# check the index where 2014
for files in ${pth}/*; do
chkIndex=$(echo ${files##*/} | grep -bo $year | cut -d: -f1)
if [ "$chkIndex" -ge 12 ]; then
day=$(echo ${files##*/} | cut -c 11-12)
else
day=$(echo ${files##*/} | cut -c 15-16)
fi
if [ $day -ge 10 ]; then
echo $files
fi
done
# done
exit 0
./fileLst.sh /tmp/testdir 2014
/tmp/testdir/AUTO_F1_20140610.TXT
/tmp/testdir/BUTO_F1_20140616.TXT
/tmp/testdir/LA_AUTO_06112014.TXT
/tmp/testdir/MA_AUTO_06212014.TXT
/tmp/testdir/ZA_AUTO_06232014.TXT
I didnt get tha above code, i have some questions.
Code:
for files in ${pth}/*;
what is the files in the above code, it is not reading anything.
2.
Code:
if [ $# -ne 2 ]; then
echo "Usage: ${0##*/} </path/to/directory> <four-digit year>"
exit 1
fi
There seems to be a lot of complicated processing going on for something that seems pretty simple. I'm also not sure that I understand which files are to be moved. If you run your script on the last day of July, is it supposed to move files dated June 10-30, or files dated July 10-31?
I think you said you want to move June files if you run it in July, so try something like the following:
Code:
#!/bin/ksh
cd /path/to/source/directory
# Get current month and year:
read m y <<-EOF
$(date +'%m %Y')
EOF
# Calculate previous monht:
if [ $m = 01 ]
then m=12
y=$((y - 1))
else m=$(printf '%02d' $((${m#0} - 1)))
fi
# Move files with names of the form *MMDDYYYY.TXT and *YYYYMMDD.TXT where MM
# and YYYY match the previous month and DD matches a day >= 10 to a new
# directory.
echo mv *$m[1-3][0-9]$y.TXT *$y$m[1-3][0-9].TXT /path/to/destination/directory
If you want it to remove July files when you run it in July, remove the code in red and change previous in the comment to current.
This was tested with ksh but will also work with bash or any other shell that supports POSIX shell parameter expansions, command substitution, and arithmetic expansions. If the echo shows that the right list of files are being passed to mv, remove the echo on the last line of the script to actually move the files. When running this script any time in July 2014 with the list of files you provided in the 1st message in this thread, the above script produces the output:
If there are too many files to move, the mv could fail due to ARG_MAX limits. If this is a problem in your situation, there are several ways to work around it.
Hi All,
How to get last month files. Ex : 1st Jan i have to get Dec 31 days files and on Feb 1st i have to get Jan 31 days files and on Mar 1st i have to get Feb 28 days files. Below are the example files with date and timestamp.
aaa.txt.timestamp
aaa.txt.timestamp
aaa.txt.timestamp
Please... (7 Replies)
Hello,
i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m
i need to write this script in php or bash or sh or mysql or perl in normal time o unix time
i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Hi,
I have a script that accepts an input date from the user in yyyy-mm-dd format.
I need to get the mm-dd part and convert it to month name.
example:
2011-11-15
I want that to become "Nov 15"
I don't have the GNU date, I am using an AIX os.
Thanks. (1 Reply)
Hi All
I am trying to remove files from february, only using the following commands:
find . -mtime 70 -exec rm {} \;, but I dont seem to get them deleted.
But I am confused, now, because I have been told to use -atime, like
find . -atime 75 -exec rm {} \;Please can you help!
FR (8 Replies)
Hello All,
I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer.
Any ideas would be appreciated! (5 Replies)
Hi,
I need to create a script which should delete all the log files which are 1 month old or older than that.
For ex:
Today's date -- > 09/30/2010
Directory --> /user/work/log/
Files-->
log.07182010 created on 07/18/2010
log.08182010 created on 08/18/2010
log.09182010 ... (4 Replies)
Hi,
I want to delete files of a particular month in a particular directory. Please tell me a script for this so that it will search a particular kind of file in that directory and delete them.I am using HP-UX. (1 Reply)
Hi,
My task to to delete files which are of previous months.
I have files named as follows *CCYYMMDD.xls. on a particular day i have delete previous months files
i.e in Dec i have delete all nov files which look like 200511DD.XLS
in Jan i have to delete all Dec files 200512DD.xls
... (7 Replies)
Hi,
I am trying to do achieving of files by months.
find /test -name \*.* -mtime +30
will give me the result of all modified files after 30 days.
But lets say i want to list all files that is modified in last months... what is the command to do it?
Thanks! (13 Replies)