Shell - Find the latest file which matches a given pattern

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite
1












I have the following files.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* 
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder300/ifolder1/version_b
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder301/ifolder2/version_c
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder302/ifolder3/version_d
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder300/version_2
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder301/version_3
-rw-r--r-- 1 root root 0 Jul 19 13:36 /client/folder302/version_4


I am trying to get the latest version file for a pattern matching an ID. Example is shown below.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a


The latest version is version_a in the above example.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299 | tail -1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a


I am told that this approach is not good to find a file (Why *not* parse `ls`? )and am looking for an alternative way like https://stackoverflow.com/a/26766782/9316558. Please let me know if something is not clear.



Update:



From below answer by Jasen, I could get the latest file in the path /client



find /client -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1


But, the above command gives the latest file. I am looking for finding the latest version file.







share|improve this question





















  • Will the latest version file always be the one that sorts last, or should file timestamps be used, or some other scheme?
    – Kusalananda
    Jul 19 at 8:55










  • @Kusalananda Yes, the latest version file will always be the one that sorts last. I mean the one that is created recently.
    – Raj
    Jul 19 at 9:29











  • Sorry I should have been more specific. Sorts last, by name? ... taking into account that version_3 sorts after version_10.
    – Kusalananda
    Jul 19 at 9:30










  • @Raj: Does it have to be bash? If you would write your script in Zsh, you could do for instance a files=( /client/*299*/ver*(Nom) ) to get an array of files sorted in ascending order by modification time (o means sorting, m means modification time, N causes an empty array to be generated if no matching files exist). If you need descending order, use (^Nom) instead.
    – user1934428
    Jul 19 at 9:37






  • 1




    Please re-tag your Question to Solaris, as it's apparent from comments that you're only connecting from Linux, then running find on Solaris.
    – Jeff Schaller
    Jul 19 at 19:24
















up vote
0
down vote

favorite
1












I have the following files.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* 
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder300/ifolder1/version_b
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder301/ifolder2/version_c
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder302/ifolder3/version_d
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder300/version_2
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder301/version_3
-rw-r--r-- 1 root root 0 Jul 19 13:36 /client/folder302/version_4


I am trying to get the latest version file for a pattern matching an ID. Example is shown below.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a


The latest version is version_a in the above example.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299 | tail -1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a


I am told that this approach is not good to find a file (Why *not* parse `ls`? )and am looking for an alternative way like https://stackoverflow.com/a/26766782/9316558. Please let me know if something is not clear.



Update:



From below answer by Jasen, I could get the latest file in the path /client



find /client -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1


But, the above command gives the latest file. I am looking for finding the latest version file.







share|improve this question





















  • Will the latest version file always be the one that sorts last, or should file timestamps be used, or some other scheme?
    – Kusalananda
    Jul 19 at 8:55










  • @Kusalananda Yes, the latest version file will always be the one that sorts last. I mean the one that is created recently.
    – Raj
    Jul 19 at 9:29











  • Sorry I should have been more specific. Sorts last, by name? ... taking into account that version_3 sorts after version_10.
    – Kusalananda
    Jul 19 at 9:30










  • @Raj: Does it have to be bash? If you would write your script in Zsh, you could do for instance a files=( /client/*299*/ver*(Nom) ) to get an array of files sorted in ascending order by modification time (o means sorting, m means modification time, N causes an empty array to be generated if no matching files exist). If you need descending order, use (^Nom) instead.
    – user1934428
    Jul 19 at 9:37






  • 1




    Please re-tag your Question to Solaris, as it's apparent from comments that you're only connecting from Linux, then running find on Solaris.
    – Jeff Schaller
    Jul 19 at 19:24












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have the following files.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* 
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder300/ifolder1/version_b
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder301/ifolder2/version_c
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder302/ifolder3/version_d
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder300/version_2
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder301/version_3
-rw-r--r-- 1 root root 0 Jul 19 13:36 /client/folder302/version_4


I am trying to get the latest version file for a pattern matching an ID. Example is shown below.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a


The latest version is version_a in the above example.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299 | tail -1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a


I am told that this approach is not good to find a file (Why *not* parse `ls`? )and am looking for an alternative way like https://stackoverflow.com/a/26766782/9316558. Please let me know if something is not clear.



Update:



From below answer by Jasen, I could get the latest file in the path /client



find /client -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1


But, the above command gives the latest file. I am looking for finding the latest version file.







share|improve this question













I have the following files.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* 
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder300/ifolder1/version_b
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder301/ifolder2/version_c
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder302/ifolder3/version_d
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder300/version_2
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder301/version_3
-rw-r--r-- 1 root root 0 Jul 19 13:36 /client/folder302/version_4


I am trying to get the latest version file for a pattern matching an ID. Example is shown below.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a


The latest version is version_a in the above example.



root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299 | tail -1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a


I am told that this approach is not good to find a file (Why *not* parse `ls`? )and am looking for an alternative way like https://stackoverflow.com/a/26766782/9316558. Please let me know if something is not clear.



Update:



From below answer by Jasen, I could get the latest file in the path /client



find /client -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1


But, the above command gives the latest file. I am looking for finding the latest version file.









share|improve this question












share|improve this question




share|improve this question








edited Jul 24 at 10:33
























asked Jul 19 at 8:45









Raj

36




36











  • Will the latest version file always be the one that sorts last, or should file timestamps be used, or some other scheme?
    – Kusalananda
    Jul 19 at 8:55










  • @Kusalananda Yes, the latest version file will always be the one that sorts last. I mean the one that is created recently.
    – Raj
    Jul 19 at 9:29











  • Sorry I should have been more specific. Sorts last, by name? ... taking into account that version_3 sorts after version_10.
    – Kusalananda
    Jul 19 at 9:30










  • @Raj: Does it have to be bash? If you would write your script in Zsh, you could do for instance a files=( /client/*299*/ver*(Nom) ) to get an array of files sorted in ascending order by modification time (o means sorting, m means modification time, N causes an empty array to be generated if no matching files exist). If you need descending order, use (^Nom) instead.
    – user1934428
    Jul 19 at 9:37






  • 1




    Please re-tag your Question to Solaris, as it's apparent from comments that you're only connecting from Linux, then running find on Solaris.
    – Jeff Schaller
    Jul 19 at 19:24
















  • Will the latest version file always be the one that sorts last, or should file timestamps be used, or some other scheme?
    – Kusalananda
    Jul 19 at 8:55










  • @Kusalananda Yes, the latest version file will always be the one that sorts last. I mean the one that is created recently.
    – Raj
    Jul 19 at 9:29











  • Sorry I should have been more specific. Sorts last, by name? ... taking into account that version_3 sorts after version_10.
    – Kusalananda
    Jul 19 at 9:30










  • @Raj: Does it have to be bash? If you would write your script in Zsh, you could do for instance a files=( /client/*299*/ver*(Nom) ) to get an array of files sorted in ascending order by modification time (o means sorting, m means modification time, N causes an empty array to be generated if no matching files exist). If you need descending order, use (^Nom) instead.
    – user1934428
    Jul 19 at 9:37






  • 1




    Please re-tag your Question to Solaris, as it's apparent from comments that you're only connecting from Linux, then running find on Solaris.
    – Jeff Schaller
    Jul 19 at 19:24















Will the latest version file always be the one that sorts last, or should file timestamps be used, or some other scheme?
– Kusalananda
Jul 19 at 8:55




Will the latest version file always be the one that sorts last, or should file timestamps be used, or some other scheme?
– Kusalananda
Jul 19 at 8:55












@Kusalananda Yes, the latest version file will always be the one that sorts last. I mean the one that is created recently.
– Raj
Jul 19 at 9:29





@Kusalananda Yes, the latest version file will always be the one that sorts last. I mean the one that is created recently.
– Raj
Jul 19 at 9:29













Sorry I should have been more specific. Sorts last, by name? ... taking into account that version_3 sorts after version_10.
– Kusalananda
Jul 19 at 9:30




Sorry I should have been more specific. Sorts last, by name? ... taking into account that version_3 sorts after version_10.
– Kusalananda
Jul 19 at 9:30












@Raj: Does it have to be bash? If you would write your script in Zsh, you could do for instance a files=( /client/*299*/ver*(Nom) ) to get an array of files sorted in ascending order by modification time (o means sorting, m means modification time, N causes an empty array to be generated if no matching files exist). If you need descending order, use (^Nom) instead.
– user1934428
Jul 19 at 9:37




@Raj: Does it have to be bash? If you would write your script in Zsh, you could do for instance a files=( /client/*299*/ver*(Nom) ) to get an array of files sorted in ascending order by modification time (o means sorting, m means modification time, N causes an empty array to be generated if no matching files exist). If you need descending order, use (^Nom) instead.
– user1934428
Jul 19 at 9:37




1




1




Please re-tag your Question to Solaris, as it's apparent from comments that you're only connecting from Linux, then running find on Solaris.
– Jeff Schaller
Jul 19 at 19:24




Please re-tag your Question to Solaris, as it's apparent from comments that you're only connecting from Linux, then running find on Solaris.
– Jeff Schaller
Jul 19 at 19:24










1 Answer
1






active

oldest

votes

















up vote
1
down vote













you can combine find and sort



find -path "some pattern" -printf "%T@ %Pn" | sort -n | tail -1





share|improve this answer























  • Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %Pn" | sort -n | tail -1
    – Raj
    Jul 19 at 10:49











  • possibly -path "*299*" it understands the normal shell wildcards
    – Jasen
    Jul 19 at 10:50











  • it turns out -name was wrong and you need -path th match on directory names. answer edited.
    – Jasen
    Jul 19 at 10:54











  • yeah, you can add a start location before -path if you want search other than the current directory, see the man page for find - there's many other options.
    – Jasen
    Jul 19 at 11:16










  • Thanks for the update. The command seems to work if executed from inside the folder where I need this value from. root@RV-VMBOX:/client# find -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1 1530797283.5560000000 ifolder299/ifolder/version_a In my case, I need to execute this command remotely as a user of the system. So, I understand that this will search in the home directory of the above user, where this folder doesn't exist. Is there a way in which we can extend the -path's argument to be more precise with the path?
    – Raj
    Jul 19 at 11:18










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457157%2fshell-find-the-latest-file-which-matches-a-given-pattern%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













you can combine find and sort



find -path "some pattern" -printf "%T@ %Pn" | sort -n | tail -1





share|improve this answer























  • Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %Pn" | sort -n | tail -1
    – Raj
    Jul 19 at 10:49











  • possibly -path "*299*" it understands the normal shell wildcards
    – Jasen
    Jul 19 at 10:50











  • it turns out -name was wrong and you need -path th match on directory names. answer edited.
    – Jasen
    Jul 19 at 10:54











  • yeah, you can add a start location before -path if you want search other than the current directory, see the man page for find - there's many other options.
    – Jasen
    Jul 19 at 11:16










  • Thanks for the update. The command seems to work if executed from inside the folder where I need this value from. root@RV-VMBOX:/client# find -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1 1530797283.5560000000 ifolder299/ifolder/version_a In my case, I need to execute this command remotely as a user of the system. So, I understand that this will search in the home directory of the above user, where this folder doesn't exist. Is there a way in which we can extend the -path's argument to be more precise with the path?
    – Raj
    Jul 19 at 11:18














up vote
1
down vote













you can combine find and sort



find -path "some pattern" -printf "%T@ %Pn" | sort -n | tail -1





share|improve this answer























  • Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %Pn" | sort -n | tail -1
    – Raj
    Jul 19 at 10:49











  • possibly -path "*299*" it understands the normal shell wildcards
    – Jasen
    Jul 19 at 10:50











  • it turns out -name was wrong and you need -path th match on directory names. answer edited.
    – Jasen
    Jul 19 at 10:54











  • yeah, you can add a start location before -path if you want search other than the current directory, see the man page for find - there's many other options.
    – Jasen
    Jul 19 at 11:16










  • Thanks for the update. The command seems to work if executed from inside the folder where I need this value from. root@RV-VMBOX:/client# find -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1 1530797283.5560000000 ifolder299/ifolder/version_a In my case, I need to execute this command remotely as a user of the system. So, I understand that this will search in the home directory of the above user, where this folder doesn't exist. Is there a way in which we can extend the -path's argument to be more precise with the path?
    – Raj
    Jul 19 at 11:18












up vote
1
down vote










up vote
1
down vote









you can combine find and sort



find -path "some pattern" -printf "%T@ %Pn" | sort -n | tail -1





share|improve this answer















you can combine find and sort



find -path "some pattern" -printf "%T@ %Pn" | sort -n | tail -1






share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 19 at 10:54


























answered Jul 19 at 10:24









Jasen

1,925713




1,925713











  • Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %Pn" | sort -n | tail -1
    – Raj
    Jul 19 at 10:49











  • possibly -path "*299*" it understands the normal shell wildcards
    – Jasen
    Jul 19 at 10:50











  • it turns out -name was wrong and you need -path th match on directory names. answer edited.
    – Jasen
    Jul 19 at 10:54











  • yeah, you can add a start location before -path if you want search other than the current directory, see the man page for find - there's many other options.
    – Jasen
    Jul 19 at 11:16










  • Thanks for the update. The command seems to work if executed from inside the folder where I need this value from. root@RV-VMBOX:/client# find -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1 1530797283.5560000000 ifolder299/ifolder/version_a In my case, I need to execute this command remotely as a user of the system. So, I understand that this will search in the home directory of the above user, where this folder doesn't exist. Is there a way in which we can extend the -path's argument to be more precise with the path?
    – Raj
    Jul 19 at 11:18
















  • Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %Pn" | sort -n | tail -1
    – Raj
    Jul 19 at 10:49











  • possibly -path "*299*" it understands the normal shell wildcards
    – Jasen
    Jul 19 at 10:50











  • it turns out -name was wrong and you need -path th match on directory names. answer edited.
    – Jasen
    Jul 19 at 10:54











  • yeah, you can add a start location before -path if you want search other than the current directory, see the man page for find - there's many other options.
    – Jasen
    Jul 19 at 11:16










  • Thanks for the update. The command seems to work if executed from inside the folder where I need this value from. root@RV-VMBOX:/client# find -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1 1530797283.5560000000 ifolder299/ifolder/version_a In my case, I need to execute this command remotely as a user of the system. So, I understand that this will search in the home directory of the above user, where this folder doesn't exist. Is there a way in which we can extend the -path's argument to be more precise with the path?
    – Raj
    Jul 19 at 11:18















Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %Pn" | sort -n | tail -1
– Raj
Jul 19 at 10:49





Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %Pn" | sort -n | tail -1
– Raj
Jul 19 at 10:49













possibly -path "*299*" it understands the normal shell wildcards
– Jasen
Jul 19 at 10:50





possibly -path "*299*" it understands the normal shell wildcards
– Jasen
Jul 19 at 10:50













it turns out -name was wrong and you need -path th match on directory names. answer edited.
– Jasen
Jul 19 at 10:54





it turns out -name was wrong and you need -path th match on directory names. answer edited.
– Jasen
Jul 19 at 10:54













yeah, you can add a start location before -path if you want search other than the current directory, see the man page for find - there's many other options.
– Jasen
Jul 19 at 11:16




yeah, you can add a start location before -path if you want search other than the current directory, see the man page for find - there's many other options.
– Jasen
Jul 19 at 11:16












Thanks for the update. The command seems to work if executed from inside the folder where I need this value from. root@RV-VMBOX:/client# find -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1 1530797283.5560000000 ifolder299/ifolder/version_a In my case, I need to execute this command remotely as a user of the system. So, I understand that this will search in the home directory of the above user, where this folder doesn't exist. Is there a way in which we can extend the -path's argument to be more precise with the path?
– Raj
Jul 19 at 11:18




Thanks for the update. The command seems to work if executed from inside the folder where I need this value from. root@RV-VMBOX:/client# find -path "*299*" -printf "%T@ %Pn" | sort -n | tail -1 1530797283.5560000000 ifolder299/ifolder/version_a In my case, I need to execute this command remotely as a user of the system. So, I understand that this will search in the home directory of the above user, where this folder doesn't exist. Is there a way in which we can extend the -path's argument to be more precise with the path?
– Raj
Jul 19 at 11:18












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457157%2fshell-find-the-latest-file-which-matches-a-given-pattern%23new-answer', 'question_page');

);

Post as a guest













































































5 1 U,4dLPE
QZ dOSMqwwyZm3s,c7s9G8 6tFQ0YTV pczmBKEqODB 4Xokoo,ZsobIHAiOUT1uWsQaI,dS Ezxkc2ud9q

Popular posts from this blog

How to check contact read email or not when send email to Individual?

How many registers does an x86_64 CPU actually have?

Displaying single band from multi-band raster using QGIS