0

I have an executable which creates either file1 or file2 along with other files. I am trying to write a shell script which should output the latest created file. If the executable creates file1, the output should be file1. Please note that I already have both file1 and file2 exists in the directory along with other files. The executable overwrites it. If there are no file1/file2 exist, below script works but this doesn't work if both file1 and file2 exists.

if [ -e "file1" ]; then
   output="file1"
else
   output="file2"
fi
kumar
  • 3

1 Answers1

0

The quick and easy way.

output="$(ls -td control.tk input.scs 2> /dev/null|head -1)"

However I would only use this if you can make sure that the files don't contain funny characters like newlines or spaces. If you can guarantee that the filenames do not contain funny characters then parsing ls is quite safe.