0

I'm trying to simplifly a script into a single line which can find recursively files created yesterday, zip, move and remove depending of a condition.

I have this structure:

../main -> child1 (content: file01.csv, file02.xlsx, file03_20201028.xls, file04_20201028.xls)
      |____ child2 (content: file05.xls, file06.xlsx, file07_20201028)
              |___  child3 (file08.xls, file09.xlsx, file10_20201028.xls, file11_20201028.xls)

I have made this:

find $ORIGINPATH -type f -mtime -1 | zip -@ $ORIGINPATH/Backup_$YESTERDAY.zip
find $ORIGINPATH -type f -iname *$YESTERDAY* -exec rm -f {} \;
mv $ORIGINPATH/Backup_$YESTERDAY.zip $DESTINATIONPATH

I need to delete the files with date in their names and keep the rest.

Thanks in advance!

1
  • 3
    Which part of your current solution is not working?
    – thanasisp
    Oct 28 '20 at 9:32
0

For a one-liner, this will work:

find $ORIGIN -type f -mtime -1 -exec zip -u ${DEST}/backup_${YESTERDAY}.zip '{}' \; -delete

You will receive an error from zip on the first file, as -u usually updates existing archives (but creates one if not present and returns a warning).

Be aware that -mtime -1 does not care about the date but about the last 24 hours! So if you run it at 23:59h you will remove today's files! If you want to search for a date string, use the following option:

Say our string is defined as YESTERDAY=20201028

find $ORIGIN -type f -name \*${YESTERDAY}\*

Note the missing hard quotes and the need for escaping the asterisks.

2
  • I think that the question asks for zipping all files modified yesterday, moving this zip file, and removing all files their name is matching 'YYYYDDMM' of yesterday. These two file sets are not necessarily the same files. Perhaps some more input from the poster could clarify this.
    – thanasisp
    Oct 28 '20 at 13:58
  • You might be right - definitely needs clarification.
    – FelixJN
    Oct 28 '20 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.