fetch the newest file in a directory.

732 views
Skip to first unread message

Scuderia FormulaOne

unread,
Jan 30, 2017, 8:54:35 AM1/30/17
to Ansible Project



I would like to get the most recent file,  in a specific directory,  from remote nodes:


I am thinking about running a command similar to:

ls -lrt /tmp/backup |tail -1 | awk '{print $9}

then putting the results of that in the fetch command. 

Any suggestions?  Does this seem like a good path?



Kai Stian Olstad

unread,
Jan 30, 2017, 10:36:18 AM1/30/17
to ansible...@googlegroups.com
I think this is you only option since Ansible doesn't have any modules
to do this, but you can make it a little shorter by removing -l.

ls -t /tmp/backup | head -1
or
ls -tr /tmp/backup | tail -1

--
Kai Stian Olstad

Brian Coca

unread,
Jan 30, 2017, 11:15:27 AM1/30/17
to ansible...@googlegroups.com
You can do 2 steps, first run the find module in that directory, then using the map filter, sort by mtime and use the `last` filter to get the most recent file.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages