I have multiple sub-directories (which are named using GUIDs), which contain results files from computer simulations that I have been running. The files are all named in the following way: hydro_configuration_xxxx.xyz, where xxxx is a four-digit number from 0000 upwards; the latest file has the highest four-digit number. I would like to be able to list the latest file in each subdirectory, along with the directory that it is in.
So, for example, given sub-directory fc86783b-bcc5-4456-8964-049682ee81aa containing files hydro_configuration_0000.xyz to hydro_configuration_0123.xyz, and directory e608273d-35bd-442c-9de1-0ec184aa0d95 containing files hydro_configuration_0000.xyz to hydro_configuration_3564.xyz, I need a command that produces the output:
./fc86783b-bcc5-4456-8964-049682ee81aa/hydro_configuration_0123.xyz
./e608273d-35bd-442c-9de1-0ec184aa0d95/hydro_configuration_3564.xyz
Answer
Create bash script latestfile.sh
with the following content:
#!/bin/bash
ls -1tr "$1" | tail -n1
Run chmod +x latestfile.sh
to make it executable.
Then execute the following command:
find test -mindepth 1 -type d -name "hydro_configuration_*" -printf "%p/" -a -exec ./latestfile.sh {} \;
This will find all directories matching the pattern and print their names followed by a /
, followed by the name of the latest file in there.
No comments:
Post a Comment