Search code examples
shellunixkshhp-ux

Find and copy the two most recent files added to a directory with a specific format


I'm currently writing a ksh script that will run every 5 minutes. I want to select the two most recently added files within a directory that have a specific format. The format of the file should be: OUS_*_*_*.html. The files should then be copied over to a destination directory.

I assume I can use find, but I am using HP-UX and it does not support the -amin, -cmin, -mmin options. Does anyone know how I can achieve this functionality?

Edit 1: I have found the following commands, each of which are supposed to return the single newest file, but in use more than one file is listed:

ls -Art | tail -n 1
ls -t | head -n1

Edit 2: I can see how the functionality of these commands should work, but ls -t lists files in a table format, and selecting the first line actually selects three separate file names. I attempted to use ls -lt, but now the first line is a string total 112 followed by the file names along with their access rights, time stamp, etc..

Edit 3: I found that the -1 (numeral 1, not l) option provides a list with just file names. Using the command ls -1t | head -n 2 I was able to gain the ability to list the two newest files.

Q: Is it possible to restrict the ls command to just look for files with the previously mentioned format?


Solution

  • I was able to use this block of code to list the most recently added files to a directory that conform to a specific format:

    ls -1t $fileNameFormat | head -n 2