I'm a novice bash scripter. Need help with the following. I have a script that uses inotifywait to watch a directory. Another process will drop a csv file in nightly. The file will be of the format filename_YYYYMMDD.csv. My script takes the oldest timestamped file and renames it filename.csv, so it's ready to be consumed by another external process. In a perfect world, one files comes in, gets renamed without timestamp, and is processed. The external process moves the file to an archived folder. If the external process is down, the timestamped files will accumulate in the folder. My script renames the first file when it hits the directory, but the rest will go untouched until that first file is archived. Then my script finds the oldest file, renames it, and exits.
When the timestamped file first hits the directory, my script checks for a timestamp in the name:
regex='_[0-9]{4}[0-9]{2}[0-9]{2}(.csv)$'
if [[ $file =~ $regex ]]; then
This works fine. I check for a file already existing named filename.csv. If none, this file gets renamed.
When a file is processed and archived, the script detects that file is gone, gets the basename and looks for the next oldest file matching that basename, renames it, and exits.
Finally, here's where I need help. How can I modify the below code to look for the oldest file matching a pattern? If I just look for basename, I pick up a malformed filename, like filename_YYMM, which I don't want.
unset -v oldest
for f in $mybasename*; do
[[ -z $oldest || $f -ot $oldest ]] && oldest=$f
done
Right now $mybasename is just filename. How can I get the for to use a regex like:
regex='$mybasename_[0-9]{4}[0-9]{2}[0-9]{2}(.csv)$'
for f in $regex; do
[[ -z $oldest || $f -ot $oldest ]] && oldest=$f
done
Thanks!
man ln) to point to the file to process (ln -s filename_YYMMDD.csv filename.csv), usestat(man stat) output the filename and other data, sortman sort,cutoff (man cut) the other data, and move the link along (delete and re-create the link).