The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.
List files, displaying the most recently created or changed files first, by using the ls -t command. Sorts listings by latest time stamp first. Directory you want to search.
The ls
command has a parameter -t
to sort by time. You can then grab the first (newest) with head -1
.
ls -t b2* | head -1
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?
unset -v latest
for file in "$dir"/*; do
[[ $file -nt $latest ]] && latest=$file
done
The combination of find
and ls
works well for
The solution:
find . -name "my-pattern" -print0 |
xargs -r -0 ls -1 -t |
head -1
Let's break it down:
With find
we can match all interesting files like this:
find . -name "my-pattern" ...
then using -print0
we can pass all filenames safely to the ls
like this:
find . -name "my-pattern" -print0 | xargs -r -0 ls -1 -t
additional find
search parameters and patterns can be added here
find . -name "my-pattern" ... -print0 | xargs -r -0 ls -1 -t
ls -t
will sort files by modification time (newest first) and print it one at a line. You can use -c
to sort by creation time. Note: this will break with filenames containing newlines.
Finally head -1
gets us the first file in the sorted list.
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. Run
xargs --show-limits
to check the limits on you system.
Note 2: use find . -maxdepth 1 -name "my-pattern" -print0
if you don't want to search files through subfolders.
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.
This is a possible implementation of the required Bash function:
# Print the newest file, if any, matching the given pattern
# Example usage:
# newest_matching_file 'b2*'
# WARNING: Files whose names begin with a dot will not be checked
function newest_matching_file
{
# Use ${1-} instead of $1 in case 'nounset' is set
local -r glob_pattern=${1-}
if (( $# != 1 )) ; then
echo 'usage: newest_matching_file GLOB_PATTERN' >&2
return 1
fi
# To avoid printing garbage if no files match the pattern, set
# 'nullglob' if necessary
local -i need_to_unset_nullglob=0
if [[ ":$BASHOPTS:" != *:nullglob:* ]] ; then
shopt -s nullglob
need_to_unset_nullglob=1
fi
newest_file=
for file in $glob_pattern ; do
[[ -z $newest_file || $file -nt $newest_file ]] \
&& newest_file=$file
done
# To avoid unexpected behaviour elsewhere, unset nullglob if it was
# set by this function
(( need_to_unset_nullglob )) && shopt -u nullglob
# Use printf instead of echo in case the file name begins with '-'
[[ -n $newest_file ]] && printf '%s\n' "$newest_file"
return 0
}
It uses only Bash builtins, and should handle files whose names contain newlines or other unusual characters.
Use the find command.
Assuming you're using Bash 4.2+, use -printf '%T+ %p\n'
for file timestamp value.
find $DIR -type f -printf '%T+ %p\n' | sort -r | head -n 1 | cut -d' ' -f2
Example:
find ~/Downloads -type f -printf '%T+ %p\n' | sort -r | head -n 1 | cut -d' ' -f2
For a more useful script, see the find-latest script here: https://github.com/l3x/helpers
Unusual filenames (such as a file containing the valid \n
character can wreak havoc with this kind of parsing. Here's a way to do it in Perl:
perl -le '@sorted = map {$_->[0]}
sort {$a->[1] <=> $b->[1]}
map {[$_, -M $_]}
@ARGV;
print $sorted[0]
' b2*
That's a Schwartzian transform used there.
Dark magic function incantation for those who want the find ... xargs ... head ...
solution above, but in easy to use function form so you don't have to think:
#define the function
find_newest_file_matching_pattern_under_directory(){
echo $(find $1 -name $2 -print0 | xargs -0 ls -1 -t | head -1)
}
#setup:
#mkdir /tmp/files_to_move
#cd /tmp/files_to_move
#touch file1.txt
#touch file2.txt
#invoke the function:
newest_file=$( find_newest_file_matching_pattern_under_directory /tmp/files_to_move/ bc* )
echo $newest_file
Prints:
file2.txt
Which is:
The filename with the oldest modified timestamp of the file under the given directory matching the given pattern.
You can use stat
with a file glob and a decorate-sort-undecorate with the file time added on the front:
$ stat -f "%m%t%N" b2* | sort -rn | head -1 | cut -f2-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With