How can I get the last modified file with some extension in bash and than do something on it?
Asked
Active
Viewed 206 times
1 Answers
0
Give this a try, if the extension is .jpg:
last_modified_file="$(find . -type f -name \*.jpg -printf "%T@ %p\0" | awk 'BEGIN {RS="\0";} {if (NR==1){minmtime=$1; $1=""; lastmodified=$0;} else if (minmtime<$1){minmtime=$1; $1=""; lastmodified=$0;}} END{print substr(lastmodified,4)}')"
# do something
printf "The last modified file is named %s\n" "${last_modified_file}"
The find lists all files in current directory and sub directories with a filename ending with .jpg, and awk prints only the last modified one.
The name of the file found is stored in the variable last_modified_file.

Jay jargot
- 2,745
- 1
- 11
- 14