44

Is there no way to get a file listing out from a Google Cloud Storage bucket that is sorted by date descending? This is very frustrating. I need to check the status of files that are uploaded and the bucket has thousands of objects.

gsutil ls does not have the standard linux -t option.

Google cloud console also lists it but does not offer sorting options.

Dhiraj Gupta
  • 9,704
  • 8
  • 49
  • 54

2 Answers2

79

I use this as a workaround:

gsutil ls -l gs://[bucket-name]/ | sort -k 2

This outputs full listing including date as the second field, sort -k 2 then sorts by this field.

Gerard
  • 2,832
  • 3
  • 27
  • 39
torno
  • 1,162
  • 1
  • 8
  • 14
6

The only ordering supported by GCS is lexicographic.

As a workaround, if it's possible for you to name your objects with a datestamp, that would give you a way to list objects by date.

Mike Schwartz
  • 11,511
  • 1
  • 33
  • 36
  • 1
    I'm guessing this is by design? To allow infinitely long directories / buckets? – Dhiraj Gupta Mar 02 '18 at 05:54
  • 1
    I've been looking for official documentation of this feature (lexicographic object list ordering) and have been unable to find it. Are there any links that you know of? –  Mar 22 '18 at 19:01
  • The ordering must be deterministic since they allow paging of results, it's just not necessarily alphabetic (or controllable?). I'd love to see some official documentation on this as well as I haven't yet been able to find it. – Sebastien Martin Jun 28 '18 at 19:08
  • 3
    I too was looking for official documentation about this behavior. I found this reference: https://cloud.google.com/storage/docs/listing-objects "This page shows you how to list the objects stored in your Cloud Storage buckets, which are ordered in the list lexicographically by name". However, it's not on the official API page: https://cloud.google.com/storage/docs/json_api/v1/objects/list – bboe Feb 19 '20 at 18:55