0

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?

Marcus Koz
  • 255
  • 5
  • 21
  • 1
    Even in the worst case, using `ls -t | head -2` comes to mind. Surely that is available on HP-UX? – Joseph Myers Feb 20 '15 at 20:19
  • Thanks for the quick response. Those commands are available, however when running the commands in a test directory 6 out of a total of 8 files are listed. @JosephMyers – Marcus Koz Feb 20 '15 at 20:23
  • @JosephMyers Would you be able to extend your command to list and copy the two most recent files that fit a specific format? – Marcus Koz Feb 23 '15 at 16:55
  • I would like to, but I only provide answers that I know that will work. I am not familiar with the HP-UX environment, only many other Unix and Linux variants. If I were on the Debian/Ubuntu branch of Linux, for example, I would use `ls -t | head -2 | xargs cp -it TargetDir` and be done with it. But the `-t` flag isn't available on FreeBSD, for instance. And `xargs` itself might not even be on HP-UX for all I know. Even the syntax of `head` and `tail` are different on your system. So use the example I mentioned and see if you can work with it. If you solve it, you can write the answer! – Joseph Myers Feb 23 '15 at 20:08
  • Well, actually, since you are only using two files, and assuming they don't have any dangerous characters nor whitespace, you can using tick marks for expansion, which should be cross-platform (but might not! I don't k now HP-UX). `cp -i \`ls -1t | head -n 2\` TargetDirectory` – Joseph Myers Feb 23 '15 at 20:10

1 Answers1

0

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
Marcus Koz
  • 255
  • 5
  • 21