Find most recent file

Find the most recent file (named test) in a directory (recursively through subdirectories) and copy the most recent "test" file to another directory (/tmp/test/.)
raaj4354Asked:
Who is Participating?
 
nemws1Connect With a Mentor Database AdministratorCommented:
Found this with a quick web search:
find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "

Open in new window


Did some testing and reading and it works well (if a bit slowly)
0
 
raaj4354Author Commented:
find -type f -name test -exec ls -ltr {} \;

this gives me the file listing with the path at the end. I need to copy this file to another folder.
0
 
Seth SimmonsConnect With a Mentor Sr. Systems AdministratorCommented:
nemws1 find command is correct
you could take it a step further and include the copy process

cp $(find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ") <destination>
0
 
nemws1Database AdministratorCommented:
Good one seth2740!  Don't you love the power of UNIX?!  I teach high school kids IT and we're actually doing UNIX command line now.

I keep telling them ... you build the tool you want out of smaller tools (they're slowing getting it).
0
All Courses

From novice to tech pro — start learning today.