-1

In a directory (/var/log ) there are lots of .gz file .i need to get/find the filename of a .gz file(.eg xyz_.gz, xyz1.gz ,xyz2.gz) that was created with latest timestamp (that was newly created in the directory ) whose partial file name is ("xyz")

  • Please show some sample input and sample output in your post in code tags for better understanding of this question. – RavinderSingh13 Jun 24 '18 at 15:33
  • This might help: [Bash function to find newest file matching pattern](https://stackoverflow.com/q/5885934/3776858) – Cyrus Jun 24 '18 at 15:33
  • have a directory /var/log which has files like /xyz_201806240108.gz , /xyz_201806241803.gz , i need to find the file that was last created in directory with partial name starting with xyz * ,using python – user1573232 Jun 24 '18 at 16:00

2 Answers2

-1

You can use stat with a file glob:

$ stat -f "%m%t%N" /var/log/*.gz | sort -rn | head -1 | cut -f2-

The %m is the file time and the %N is the file name. That pattern is then used for a decorate-sort-undecorate to find the newest.

You can add sed to that to filter file names that have a regex pattern if desired.

dawg
  • 98,345
  • 23
  • 131
  • 206
-1

You can use ls -ltr xyz*.gz and have the gz-file with the shortest modification time at the bottom of the listing

Thomas K.
  • 1
  • 1