0

I have a directory with multiple files (say /home/initialdirectory) that has:

C-2343.csv
F-4455.csv
G-4433.txt
C-8788.csv

I want to move all .csv files that were modified between Monday 0:00:00 and Sunday 0:00:00 of the previous week and start with character "C", to another directory (say home/initialdirectory/backup).

I want to implement this in a cron that will be executed on Sunday 0:00:00.

Yannis_Tr
  • 15
  • 5

1 Answers1

0

Using this good method for using find

Getting dates is explained in this answer

Expanding filenames in find command explains the complexity of passing the filename into what amounts to a shell script.

Command below finds all files newer than last monday and not newer than sunday (could be last-sunday), the file name must start with C and end with .csv, after finding the file execute a shell and pass the found path in as argument 1 $src and then replace olddir with newdir in $dst. If no dst directory exist, create dst directory. Move the file from olddir to newdir.

gfind olddir \
-newermt "$(date -dlast-monday +%Y%m%d)" \
! -newermt "$(gdate -dsunday +%Y%m%d)" \
-name C*\.csv \
-exec bash -c \
'src=$1 && \
dst="${1/#olddir/newdir}" && \
[ -d $(dirname $dst) ] || \
mkdir -p $(dirname $dst) && \
mv $src $dst' \
bash \{\} \;
atl
  • 575
  • 3
  • 6