-1

I am not bash expert. I need to write a script to copy latest (by date) file from one folder to another as following

from

/test/a/date1.bak
/test/a/date2.bak
/test/a/date3.bak 
/test/b/date1.bak
/test/b/date2.bak
/test/b/date3.bak

to

/test/a/sec/date1.bak
/test/b/sec/date1.bak

On the second step I need to delete oldest file by date in the destination directory

from

/test/a/sec/date1.bak
/test/a/sec/date2.bak
/test/b/sec/date1.bak
/test/b/sec/date2.bak

to

/test/a/sec/date1.bak
/test/b/sec/date1.bak
terdon
  • 54,564

1 Answers1

0

ok got it. Terdon I am need to use files modification date. Here what I did for the file copy:

for BDIR in ls -1 .; do TO_BACK=$(ls -t $BDIR | grep ".bak$" | head -1); if [ "$TO_BACK" ]; then echo "copy $BDIR/$TO_BACK to $BDIR/sec/$TO_BACK"; fi; done