I am having a directory as


Where it has text files as


How to get the latest file based on timestamp and copy the old file into another directory and remove it from present directory which means it would extract CCD_FILE_121020150300035 from /var/tmp/kick and make a copy of CCD_FILE_121020150300033 in /var/tmp/Vgh and remove this text file from /var/tmp/kick

I am using ksh shell.

targetfile="$(ls /var/tmp/kick/CCD_FILE_*.txt | sort -r | tail -n1)"
if [[ -f "${targetfile}" ]]; then
    mv ${targerfile} /var/tmp/Vgh/
| improve this answer | |
  • What will happen if there is one text file present in /var/tmp/kick and that is the latest time stamp and no previous files are their @DopeGhoti – Pooja Dec 10 '15 at 18:45
  • If there is at least one matching file, the oldest based on the timestamp in the filename will be moved. If there is exactly one match, then /var/tmp/kick will be left devoid of matching files. – DopeGhoti Dec 10 '15 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.