# Identify most recent directory in a directory.

**URL:** <https://forum.ansible.com/t/identify-most-recent-directory-in-a-directory/37411>\
**Category:** Ansible Project\
**Created:** [December 1, 2022, 9:55pm UTC](https://forum.ansible.com/t/identify-most-recent-directory-in-a-directory/37411 "2022-12-01T21:55:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Wood](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jason\_Wood](https://forum.ansible.com/u/Jason_Wood)\
**Post date:** [December 1, 2022, 9:55pm UTC](https://forum.ansible.com/t/identify-most-recent-directory-in-a-directory/37411/1 "2022-12-01T21:55:12Z")

</div>

After performing a shell script, a new directory is created and I need to work with that directory, but I don’t know the name.

How can I get the name of the most recent directory created?

The name will be formatted like 2022-12-01\_1702, so the directory I need will be the last if sorted alphanumerically, if that helps.

I found resources for looking at files, but not directories. If it’s necessary to find this directory based on the creation date of its contents, there are files located at [dirname]/Databases/Filename.xxx

Thanks!

---

<div class="post-metadata">

**Author:** ![Thanh\_Nguyen\_Duc](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@Thanh\_Nguyen\_Duc](https://forum.ansible.com/u/Thanh_Nguyen_Duc)\
**Post date:** [December 1, 2022, 10:59pm UTC](https://forum.ansible.com/t/identify-most-recent-directory-in-a-directory/37411/2 "2022-12-01T22:59:13Z")

</div>

Is it the shell script also be triggered via ansible? If yes then you can register the path and work with it later. If not then do you know roughly when script be triggered then can just use shell command to find the directory with created date.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 2, 2022, 12:10am UTC](https://forum.ansible.com/t/identify-most-recent-directory-in-a-directory/37411/3 "2022-12-02T00:10:38Z")

</div>

Use Ansible module \*find\* and sort the directories by \*ctime\*. For  
example, given the tree

> tree /tmp/test

/tmp/test  
├── 2022-12-01\_1701  
├── 2022-12-01\_1702  
└── 2022-12-01\_1703

Declare the variable

&nbsp;&nbsp;last\_dir: "{{ (out.files|sort(attribute='ctime')|last).path }}"

The tasks below

&nbsp;&nbsp;&nbsp;&nbsp;- find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /tmp/test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file\_type: directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: last\_dir

give

&nbsp;&nbsp;last\_dir: /tmp/test/2022-12-01\_1703

If you want to take look at the \*ctime\*

&nbsp;&nbsp;path\_ctime: "{{ out.files|json\_query('.[path, ctime]') }}"

give

&nbsp;&nbsp;path\_ctime:  
&nbsp;&nbsp;&nbsp;&nbsp;- [/tmp/test/2022-12-01\_1702, 1669938403.313556]  
&nbsp;&nbsp;&nbsp;&nbsp;- [/tmp/test/2022-12-01\_1701, 1669938401.2335565]  
&nbsp;&nbsp;&nbsp;&nbsp;- [/tmp/test/2022-12-01\_1703, 1669938405.0495553]

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [December 23, 2022, 3:55am UTC](https://forum.ansible.com/t/identify-most-recent-directory-in-a-directory/37411/4 "2022-12-23T03:55:38Z")

</div>

> Use Ansible module \*find\* and sort the directories by \*ctime\*. For  
> example, given the tree

This is only reliable if nothing else writes anything else in that  
tree between the first command, tand the "tree" command.

---

<div class="post-metadata">

**Author:** ![Thanh\_Nguyen\_Duc](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@Thanh\_Nguyen\_Duc](https://forum.ansible.com/u/Thanh_Nguyen_Duc)\
**Post date:** [December 23, 2022, 4:12am UTC](https://forum.ansible.com/t/identify-most-recent-directory-in-a-directory/37411/5 "2022-12-23T04:12:31Z")

</div>

I don’t think you can achieve what you want if the file keep generating. Ansible is not real time monitor/scanning. The most recently at the point of time when you trigger only.

Thanks and Best Regards,

Thanh.
