create t a filelist with the latest file by YYYYMMDD and move to subfolder

Hi all,

I am receiving files named like:
ABC_20120730.csv
ABC_20120801.csv
ABC_20120812.csv

They are residing in a folder named Incoming.
I am supposed to write a script that will create a filelist which will contain only the name of the latest file beginning with ABC_, by YYYYMMDD sufix.

Then I am supposed to move the file from the filelist into a subfolder Incoming/Processing.

Please help.
Thanks!

echo "mv $(ls -ltr /Incoming/ABC_*  | tail -1 | awk '{print $NF}') /Incoming/Processing"

Remove echo if the o/p is as expected

Your directions a not entirely clear, but I believe the following performs the actions you requested and verifies that each action was performed successfully.

#!/bin/ksh
# The following assumes that this script will be run in the parent directory of the
# Incoming directory.  If this script will be run while sitting in the Incoming directory,
# remove the following cd and error checking.  Otherwise, cd to an absolute pathname
# for the Incoming directory.

if ! cd Incoming
then    echo "$0": processing aborted >&2
        exit 1
fi
ls -r ABC_[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9].csv|(
        if read oldest
        then    if mv $oldest Processing/
                then    echo $oldest moved to Incoming/Processing.
                        exit 0
                else    echo $oldest not moved. >&2
                        exit 2
                fi
        else    echo $0: No matching Incoming file found. >&2
                exit 3
        fi
)

Although this script uses ksh, it should also work with at least sh and bash.

1 Like

Thanks for your quick replies!

pravin27, unfortunatelly haven't been able to execute the code successfully. Probably due to my lack of knowlege and understanding.
I am a rookie, I'm afraid :frowning:

Don Kragun, that worked perfectly, problem solved.