Filter files by string timestamp in filename
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I've got a folder with several thousand files with names like ousjgforuigor-TIMESTAMP.txt
The timestamp is a standard Unix timestamp (e.g. 1543932635). Is there an easy way to list only files with a filename-timestamp > a provided one?
The number of characters before the timestamp is variable, but the name always ends with -TIMESTAMP.txt
I could write a bash script to do this, but that seems like overkill.
files regular-expression timestamps filter
add a comment |
up vote
2
down vote
favorite
I've got a folder with several thousand files with names like ousjgforuigor-TIMESTAMP.txt
The timestamp is a standard Unix timestamp (e.g. 1543932635). Is there an easy way to list only files with a filename-timestamp > a provided one?
The number of characters before the timestamp is variable, but the name always ends with -TIMESTAMP.txt
I could write a bash script to do this, but that seems like overkill.
files regular-expression timestamps filter
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I've got a folder with several thousand files with names like ousjgforuigor-TIMESTAMP.txt
The timestamp is a standard Unix timestamp (e.g. 1543932635). Is there an easy way to list only files with a filename-timestamp > a provided one?
The number of characters before the timestamp is variable, but the name always ends with -TIMESTAMP.txt
I could write a bash script to do this, but that seems like overkill.
files regular-expression timestamps filter
I've got a folder with several thousand files with names like ousjgforuigor-TIMESTAMP.txt
The timestamp is a standard Unix timestamp (e.g. 1543932635). Is there an easy way to list only files with a filename-timestamp > a provided one?
The number of characters before the timestamp is variable, but the name always ends with -TIMESTAMP.txt
I could write a bash script to do this, but that seems like overkill.
files regular-expression timestamps filter
files regular-expression timestamps filter
asked Dec 4 at 19:14
Techrocket9
1134
1134
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Using zsh's expression-as-a-glob-qualifier,
t=1543951252 zsh -c 'datefilter() ts=$REPLY##*-; ts=$ts%*.txt; ((ts >= $t)) ; print -l *-<->.txt(+datefilter)'
The overall command (towards the end) is print -l
, which prints each argument on a separate line. Well, the overall command is a presumed bash shell call to zsh
that sets the environment variable t
to some given value. Instead of printing the filenames, you could put them in an array or delete them or do anything else you want with them.
The glob qualifier *-<->.txt
picks up potentially-matching filenames -- ones that begin with anything (*
), followed by a dash (-
), followed by any range of numbers (zsh's range operator <->
), followed by .txt
; that globbing is then sent to the glob qualifier (+datefilter)
, which is a call to the corresponding function.
The datefilter
function takes the incoming filename (in $REPLY
) and prunes it down to the timestamp value. It returns true if that timestamp value is greater than or equal to the given timestamp in t
. Files that succeed in that test are kept as filenames; the rest are dropped.
You could do something similar in bash by manually looping over the glob:
for f in *-*.txt
do
ts=$f##*-
ts=$ts%.txt
[[ ts -ge t ]] && printf '%sn' "$f"
done
Although the bash wildcard *
could pick up stray filenames such as foo-bar.txt
where bar
is not required to be a number. You'd have to hard-code in some assumptions otherwise, such as:
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt; do # ...
or
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.txt; do # ...
to force some number of digits to appear between the dash and the period.
add a comment |
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: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485974%2ffilter-files-by-string-timestamp-in-filename%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Using zsh's expression-as-a-glob-qualifier,
t=1543951252 zsh -c 'datefilter() ts=$REPLY##*-; ts=$ts%*.txt; ((ts >= $t)) ; print -l *-<->.txt(+datefilter)'
The overall command (towards the end) is print -l
, which prints each argument on a separate line. Well, the overall command is a presumed bash shell call to zsh
that sets the environment variable t
to some given value. Instead of printing the filenames, you could put them in an array or delete them or do anything else you want with them.
The glob qualifier *-<->.txt
picks up potentially-matching filenames -- ones that begin with anything (*
), followed by a dash (-
), followed by any range of numbers (zsh's range operator <->
), followed by .txt
; that globbing is then sent to the glob qualifier (+datefilter)
, which is a call to the corresponding function.
The datefilter
function takes the incoming filename (in $REPLY
) and prunes it down to the timestamp value. It returns true if that timestamp value is greater than or equal to the given timestamp in t
. Files that succeed in that test are kept as filenames; the rest are dropped.
You could do something similar in bash by manually looping over the glob:
for f in *-*.txt
do
ts=$f##*-
ts=$ts%.txt
[[ ts -ge t ]] && printf '%sn' "$f"
done
Although the bash wildcard *
could pick up stray filenames such as foo-bar.txt
where bar
is not required to be a number. You'd have to hard-code in some assumptions otherwise, such as:
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt; do # ...
or
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.txt; do # ...
to force some number of digits to appear between the dash and the period.
add a comment |
up vote
2
down vote
accepted
Using zsh's expression-as-a-glob-qualifier,
t=1543951252 zsh -c 'datefilter() ts=$REPLY##*-; ts=$ts%*.txt; ((ts >= $t)) ; print -l *-<->.txt(+datefilter)'
The overall command (towards the end) is print -l
, which prints each argument on a separate line. Well, the overall command is a presumed bash shell call to zsh
that sets the environment variable t
to some given value. Instead of printing the filenames, you could put them in an array or delete them or do anything else you want with them.
The glob qualifier *-<->.txt
picks up potentially-matching filenames -- ones that begin with anything (*
), followed by a dash (-
), followed by any range of numbers (zsh's range operator <->
), followed by .txt
; that globbing is then sent to the glob qualifier (+datefilter)
, which is a call to the corresponding function.
The datefilter
function takes the incoming filename (in $REPLY
) and prunes it down to the timestamp value. It returns true if that timestamp value is greater than or equal to the given timestamp in t
. Files that succeed in that test are kept as filenames; the rest are dropped.
You could do something similar in bash by manually looping over the glob:
for f in *-*.txt
do
ts=$f##*-
ts=$ts%.txt
[[ ts -ge t ]] && printf '%sn' "$f"
done
Although the bash wildcard *
could pick up stray filenames such as foo-bar.txt
where bar
is not required to be a number. You'd have to hard-code in some assumptions otherwise, such as:
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt; do # ...
or
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.txt; do # ...
to force some number of digits to appear between the dash and the period.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Using zsh's expression-as-a-glob-qualifier,
t=1543951252 zsh -c 'datefilter() ts=$REPLY##*-; ts=$ts%*.txt; ((ts >= $t)) ; print -l *-<->.txt(+datefilter)'
The overall command (towards the end) is print -l
, which prints each argument on a separate line. Well, the overall command is a presumed bash shell call to zsh
that sets the environment variable t
to some given value. Instead of printing the filenames, you could put them in an array or delete them or do anything else you want with them.
The glob qualifier *-<->.txt
picks up potentially-matching filenames -- ones that begin with anything (*
), followed by a dash (-
), followed by any range of numbers (zsh's range operator <->
), followed by .txt
; that globbing is then sent to the glob qualifier (+datefilter)
, which is a call to the corresponding function.
The datefilter
function takes the incoming filename (in $REPLY
) and prunes it down to the timestamp value. It returns true if that timestamp value is greater than or equal to the given timestamp in t
. Files that succeed in that test are kept as filenames; the rest are dropped.
You could do something similar in bash by manually looping over the glob:
for f in *-*.txt
do
ts=$f##*-
ts=$ts%.txt
[[ ts -ge t ]] && printf '%sn' "$f"
done
Although the bash wildcard *
could pick up stray filenames such as foo-bar.txt
where bar
is not required to be a number. You'd have to hard-code in some assumptions otherwise, such as:
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt; do # ...
or
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.txt; do # ...
to force some number of digits to appear between the dash and the period.
Using zsh's expression-as-a-glob-qualifier,
t=1543951252 zsh -c 'datefilter() ts=$REPLY##*-; ts=$ts%*.txt; ((ts >= $t)) ; print -l *-<->.txt(+datefilter)'
The overall command (towards the end) is print -l
, which prints each argument on a separate line. Well, the overall command is a presumed bash shell call to zsh
that sets the environment variable t
to some given value. Instead of printing the filenames, you could put them in an array or delete them or do anything else you want with them.
The glob qualifier *-<->.txt
picks up potentially-matching filenames -- ones that begin with anything (*
), followed by a dash (-
), followed by any range of numbers (zsh's range operator <->
), followed by .txt
; that globbing is then sent to the glob qualifier (+datefilter)
, which is a call to the corresponding function.
The datefilter
function takes the incoming filename (in $REPLY
) and prunes it down to the timestamp value. It returns true if that timestamp value is greater than or equal to the given timestamp in t
. Files that succeed in that test are kept as filenames; the rest are dropped.
You could do something similar in bash by manually looping over the glob:
for f in *-*.txt
do
ts=$f##*-
ts=$ts%.txt
[[ ts -ge t ]] && printf '%sn' "$f"
done
Although the bash wildcard *
could pick up stray filenames such as foo-bar.txt
where bar
is not required to be a number. You'd have to hard-code in some assumptions otherwise, such as:
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt; do # ...
or
for f in *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.txt; do # ...
to force some number of digits to appear between the dash and the period.
answered Dec 4 at 20:02
Jeff Schaller
37.8k1053122
37.8k1053122
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485974%2ffilter-files-by-string-timestamp-in-filename%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown