How to count the number of files in the bucket folder with gsutil

Is it possible to count the number of files in directory folders?

Like:

gsutil ls -count -recursive gs://bucket/folder Result: 666 files 

I just want the total number of files to be compared with the number in the sync folder on my server.

I do not understand it in the manual.

+7
google-cloud-storage
source share
2 answers

gsutil ls command with parameters -l (long list) and -R (recursive list) will list the entire bucket recursively and then produce the total number of all objects, both files and directories, at the end:

 $ gsutil ls -lR gs://pub 104413 2011-04-03T20:58:02Z gs://pub/SomeOfTheTeam.jpg 172 2012-06-18T21:51:01Z gs://pub/cloud_storage_storage_schema_v0.json 1379 2012-06-18T21:51:01Z gs://pub/cloud_storage_usage_schema_v0.json 1767691 2013-09-18T07:57:42Z gs://pub/gsutil.tar.gz 2445111 2013-09-18T07:57:44Z gs://pub/gsutil.zip 1136 2012-07-19T16:01:05Z gs://pub/gsutil_2.0.ReleaseNotes.txt ... <snipped> ... gs://pub/apt/pool/main/p/python-socksipy-branch/: 10372 2013-06-10T22:52:58Z gs://pub/apt/pool/main/p/python-socksipy-branch/python-socksipy-branch_1.01_all.deb gs://pub/shakespeare/: 84 2010-05-07T23:36:25Z gs://pub/shakespeare/rose.txt TOTAL: 144 objects, 102723169 bytes (97.96 MB) 

If you really want to get the total, you can pass the output to the tail command:

 $ gsutil ls -lR gs://pub | tail -n 1 TOTAL: 144 objects, 102723169 bytes (97.96 MB) 

UPDATE

Gsutil now has the du command. This makes it easy to get an invoice:

 $ gsutil du gs://pub | wc -l 232 
+19
source share

Do you want gsutil ls -count -recursive in gs://bucket/folder ? Good; gsutil ls gs://bucket/folder/** will list only the full URLs of the file paths under gs://bucket/folder without a footer or lines ending with a colon. The pipeline that on wc -l will give you a result line counter.

gsutil ls gs://bucket/folder/** | wc -l

0
source share

All Articles