I have several folders on one server numbered 1-10. Each folder has multiple files. I need to find the latest file with a pattern name for example :

file 1 : NA_UW_CLUSTER_RESULTS_210916.zip file 2 : UW_CLUSTER_RESULTS_210916.zip

I need to select 'file 2' and copy it to another server having multiple corresponding folders numbered 1-10.

So i need to copy latest file (UW_CLUSTER_RESULTS_210916.zip) with matching pattern from 'folder 1' in 'A' server to 'folder 1' in 'B' server.

  • What you did, till now, for the solution – TPS Sep 21 '16 at 7:07
  • I have found the latest file using the command : ls -1rt UW_CLUSTER_RESULTS* | tail -1 However, i cannot use that as different folders will have different prefix to file names e.g. MH_CLUSTER_RESULTS – Sanket Shevkar Sep 21 '16 at 7:44

Maybe this is good enough for your use case:

ls -1 -td /dir1/prefix1* /dir2/prefix2* | head -n 1

Or more robust:

find /dir1 /dir2 -type f \( -path "*dir1/prefix1*" -o -path "*dir2/prefix2*" \) -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
| improve this answer | |

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.