Skip to content

Instantly share code, notes, and snippets.

@cmbaughman
Created February 1, 2024 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmbaughman/d1f4c8212b8a7cf29061be9014fd9630 to your computer and use it in GitHub Desktop.
Save cmbaughman/d1f4c8212b8a7cf29061be9014fd9630 to your computer and use it in GitHub Desktop.
Linux copy files based on date

To copy some files from one directory to another can be done kinda nicely with the find command.

  1. First define the start date: touch --date "2024-01-01" /tmp/start
  2. Define the end date: touch --date "2024-02-01" /tmp/end
  3. Now simply craft a find command like this:
find /var/logs/some_dir -type f -newer /tmp/start -not -newer /tmp/end -exec cp "{}" /opt/another/directory \;

NOTE: In the above command, the -exec part of the find uses "{}" as where it puts each filename when executing the command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment