Identify most recent directory in a directory.

22 views
Skip to first unread message

Jason Wood

unread,
Dec 1, 2022, 4:57:42 PM12/1/22
to Ansible Project
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!

Thanh Nguyen Duc

unread,
Dec 1, 2022, 5:59:30 PM12/1/22
to ansible...@googlegroups.com
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.

Sent from my iPhone

On 2 Dec 2022, at 05:57, Jason Wood <ja...@definedatabase.com> wrote:

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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3b559b55-8658-4df0-b32b-a227def8ca40n%40googlegroups.com.

Vladimir Botka

unread,
Dec 1, 2022, 7:11:14 PM12/1/22
to Jason Wood, ansible...@googlegroups.com
Use Ansible module *find* and sort the directories by *ctime*. For
example, given the tree

shell> tree /tmp/test
/tmp/test
├── 2022-12-01_1701
├── 2022-12-01_1702
└── 2022-12-01_1703

Declare the variable

last_dir: "{{ (out.files|sort(attribute='ctime')|last).path }}"

The tasks below

- find:
path: /tmp/test
file_type: directory
register: out
- debug:
var: last_dir

give

last_dir: /tmp/test/2022-12-01_1703

If you want to take look at the *ctime*

path_ctime: "{{ out.files|json_query('[].[path, ctime]') }}"

give

path_ctime:
- [/tmp/test/2022-12-01_1702, 1669938403.313556]
- [/tmp/test/2022-12-01_1701, 1669938401.2335565]
- [/tmp/test/2022-12-01_1703, 1669938405.0495553]

--
Vladimir Botka

Nico Kadel-Garcia

unread,
Dec 22, 2022, 10:56:03 PM12/22/22
to ansible...@googlegroups.com, Jason Wood
On Thu, Dec 1, 2022 at 7:10 PM Vladimir Botka <vbo...@gmail.com> wrote:
>
> 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.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20221202011038.0ed02422%40gmail.com.

Thanh Nguyen Duc

unread,
Dec 22, 2022, 11:12:54 PM12/22/22
to ansible...@googlegroups.com, Jason Wood
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.

> On 23 Dec 2022, at 11:55, Nico Kadel-Garcia <nka...@gmail.com> wrote:
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAOCN9rw4aus7R6ZtB6z-oOQeFDa8BgwDyhFPcqVUXqdcLJi9yg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages