'Find and copy specific files by date

I've been trying to get a script working to backup some files from one machine to another but have been running into an issue.

Basically what I want to do is copy two files, one .log and one (or more) .dmp. Their format is always as follows:

something_2022_01_24.log something_2022_01_24.dmp

I want to do three things with these files:

  • find the second to last one .log file (i.e. something_2022_01_24.log is the latest,I want to find the one before that say something_2022_01_22.log)
  • get a substring with just the date (2022_01_22)
  • copy every .dmp that matches the date (i.e something_2022_01_24.dmp, something01_2022_01_24.dmp)

For the first one from what I could find the best way is to do: ls -t *.log | head-2 as it displays the second to last file created.

As for the second one I'm more at a loss because I'm not sure how to parse the output of the first command.

The third one I think I could manage with something of the sort:

[ -f "/var/www/my_folder/*$capturedate.dmp" ] && cp "/var/www/my_folder/*$capturedate.dmp" /tmp/

What do you guys think is there any way to do this? How can I compare the substring?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source