Shell - Find the latest file which matches a given pattern
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
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.
bash shell files solaris date
 |Â
show 3 more comments
up vote
0
down vote
favorite
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.
bash shell files solaris date
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 thatversion_3
sorts afterversion_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 afiles=( /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 runningfind
on Solaris.
â Jeff Schaller
Jul 19 at 19:24
 |Â
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
bash shell files solaris date
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.
bash shell files solaris date
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 thatversion_3
sorts afterversion_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 afiles=( /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 runningfind
on Solaris.
â Jeff Schaller
Jul 19 at 19:24
 |Â
show 3 more comments
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 thatversion_3
sorts afterversion_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 afiles=( /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 runningfind
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
 |Â
show 3 more comments
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
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
 |Â
show 4 more comments
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
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
 |Â
show 4 more comments
up vote
1
down vote
you can combine find and sort
find -path "some pattern" -printf "%T@ %Pn" | sort -n | tail -1
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
 |Â
show 4 more comments
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
you can combine find and sort
find -path "some pattern" -printf "%T@ %Pn" | sort -n | tail -1
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
 |Â
show 4 more comments
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
 |Â
show 4 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 afterversion_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