For example, I have a directory of files like:. I want the newest file that starts with 'b2'. How do I do this in bash? The ls command has a parameter -t to sort by time. You can then grab the first newest with head But beware: Why you shouldn't parse the output of ls. My personal opinion: parsing ls is only dangerous when the filenames can contain funny characters like spaces or newlines. If you can guarantee that the filenames will not contain funny characters then parsing ls is quite safe.
If you are developing a script which is meant to be run by many people on many systems in many different situations then I very much do recommend to not parse ls. Here is how to do it "right": How can I find the latest newest, earliest, oldest file in a directory?
You can use -c to sort by creation time. Note : this will break with filenames containing newlines. Note: xargs use system limits to the size of the argument list. If this size exceeds, xargs will call ls multiple times. This will break the sorting and probably also the final output.
Note 2: use find. Note 3: As pointed out by starfry - -r argument for xargs is preventing the call of ls -1 -t , if no files were matched by the find. Thank you for the suggesion. It uses only Bash builtins, and should handle files whose names contain newlines or other unusual characters.
Or if brittle bash parlor tricks subcontracting to python interpreter is more your angle, this does the same thing:. Here's a way to do it in Perl:. That's a Schwartzian transform used there. You can use stat with a file glob and a decorate-sort-undecorate with the file time added on the front:. For such things, I tend to use Ruby since it is very awk like in the ease of writing small, throw away scripts yet has the power of Python or Perl right from the command line. Don't use in production.
If you want files from the last two days then you'll be better off using the command below:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Bash function to find newest file matching pattern Ask Question.
Asked 10 years, 8 months ago. Active 8 days ago. Viewed k times. Improve this question. Lesmana The sorting is the key step to get your newest file — Wolfgang Fahl. Active 2 days ago. Viewed 40 times. Improve this question. Can you try placing a. You do not use any regex in any of the commands, you are using glob patterns.
The pattern with the -name option is expanded as the shell's pathname expansion. The special characters in the pattern are treated as the wildcards of the type glob, not those in the regex. If you want to use regex, try something like: find.
There is an "extended" wildcard facility in Bash but that also is not regex. Add a comment. Active Oldest Votes. Improve this answer. Dominique Dominique I'm also wondering the same thing, but my question's been closed. Try it with a file like so, called foo. Improve this answer. I undetstand that it is some kind of regex, but when I replace it with my own word and own txt file it doesn't work — applenic.
The easier way to do this, is using grep or egrep. Rui F Ribeiro Bradley D Bradley D 2 2 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta.
0コメント