Need last month files after 10th of every month

Hi,
I need all file names in a folder which has date >= 10th of last month,

Example
: files in folder

AUTO_F1_20140610.TXT
BUTO_F1_20140616.TXT
CUTO_F1_20140603.TXT
FA_AUTO_06012014.TXT
LA_AUTO_06112014.TXT
MA_AUTO_06212014.TXT
ZA_AUTO_06232014.TXT

Output:

AUTO_F1_20140610.TXT
BUTO_F1_20140616.TXT
LA_AUTO_06112014.TXT
MA_AUTO_06212014.TXT
ZA_AUTO_06232014.TXT

The format of date is not fixed in your files !?

Hello nani1984,

  • What have you tried so far?
  • What errors/output are you getting?
  • What OS and version are you using?
  • What shell or other tools would you prefer to code in?
  • How do you think you can identify the files? (file name embedded date or last update time?)

Most importantly, What have you tried so far?

There are probably many ways to achieve what you want but you need to be clear about what you want and what you have. Showing some effort and therefore the specific thing that you are stuck with is likely to get a better response. Can you help us to help you?

For a given date (tell us which one you are basing it on), please show a sample with ls -l the range of files you are likely to have, showing us which you want to process and which to ignore. It will focus the discussion.

When you are discussion files based on the 10th or later of the month, it would be good to know what differences there will be with running the process before or after 10th. Does it make a difference?

Do you have any concerns over scheduling the work?

Thanks, in advance,
Robin
UK

Hi Robin,

i need to run this process every last day of the month and i need to move all the files which i received on after 10th of that month to the different folder, here i am considering the dae embeded in the file.
For example:

AUTO_F1_20140610.TXT -- this file , i received on 06/10/2014
LA_AUTO_06112014.TXT -- this file , i received on 06/11/2014

Please consider answering the other questions to give us the best opportunity to help you.

Please wrap code and data input/output in CODE tags, like this:-

to produce the following (fixed character width, space respected):-

This is my code

It makes it far easier to read. It was adjusted for you by Scrutinizer this time. Having file names of different format will make it a little more awkward, but not insurmountable.

Considering ? to be a single character and * to be zero or more, are your files all of two formats, ?UTO_*_YYYYMMDD and ??_?UTO_DDMMYYYY ? That's simple enough to work on, but clarity would be useful.

Have I guessed the date format correctly even? I'm assuming that we have either most significant to least (Year, Month, Day) or least significant to most (Day, Month, Year) If you have the Day in the middle, we need to know. It's not clear from your text as you just give us:-

.... which to a British reader (and many others) is 6th October 2014 and 6th November 2014 respectively. They are both in the future for now, but you could be running your clocks forward for some reason so we need to check.

Regards,
Robin

Sorry for not mentioning,

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 .

I tried to extract the dates in the files using below CODE

 ls -1 *.TXT|awk 'BEGIN{FS="[_.]"}{print $(NF-1)}'

It gives just dates like this "20140610" , i got all the dates, but stuck there to move forward "to move all the files which we received after 10 of the month" .

Please let me know if any questions.

Tossed this together. Should give you a base to start.

#!/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

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:-

# 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 :eek: 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.

Robin

I didnt get tha above code, i have some questions.

for files in ${pth}/*;

what is the files in the above code, it is not reading anything.

 if [ $# -ne 2 ]; then
    echo "Usage: ${0##*/} </path/to/directory> <four-digit year>"
    exit 1
fi

In the above code

{0##*/}

, it is commenting the whole line if we use

##

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:

#!/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:

mv LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT /path/to/destination/directory

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.