Terry Chen
Terry Chen

Reputation: 469

How to get the actual size of LFS file?

I have files tracked with git LFS, and I use git ls-tree command to list the files with file size, but I found the size of LFS tarcked files is very small(134 Bytes, its actual size is more than 100MB), I know it's the size of LFS pointer file. Does any one know how to get the actual file size?

use command "git ls-tree -l HEAD" to list the repository tree

Upvotes: 8

Views: 8066

Answers (2)

GuyPaddock
GuyPaddock

Reputation: 2517

The earlier answer is fine if you want human-readable sizes in listings, but if you want the size in bytes and are interested in finding the largest files in the repo, it's a bit lacking. Unfortunately, in the current version of Git LFS (3.5.1) there doesn't appear to be support for specifying bytes as the unit nor for sorting.

So, here's a Python script you can run within your repository to get files sorted largest to smallest with sizes in bytes:

import os
import subprocess

# Fetch all LFS files
subprocess.run(["git", "lfs", "fetch", "--all"], check=True)

# Get a list of all LFS-tracked files with full OIDs
result = subprocess.run(["git", "lfs", "ls-files", "--long"], capture_output=True, text=True, check=True)
lfs_files = result.stdout.splitlines()

# Parse the LFS file list and get their sizes
sizes = []
for line in lfs_files:
    parts = line.split()
    oid = parts[0]  # The first part is the full OID
    path = ' '.join(parts[2:])  # The path starts from the third part onward
    object_path = os.path.join(".git/lfs/objects", oid[:2], oid[2:4], oid)
    if os.path.isfile(object_path):
        size = os.path.getsize(object_path)
        sizes.append((oid, path, size))

# Sort the sizes list from largest to smallest
sizes.sort(key=lambda x: x[2], reverse=True)

# Print the sorted sizes
for oid, path, size in sizes:
    print(f"{path} ({oid}): {size} bytes")

Upvotes: 0

phd
phd

Reputation: 94397

git lfs ls-files -s

-s --size: Show the size of the LFS object between parenthesis at the end of a line.

Upvotes: 23

Related Questions