Skip to main content

How to Find a DVC-Tracked File in the DVC Cache

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

DVC keeps every version of your data in its cache, but not under the names you gave the files: each file is named after its MD5 hash. Once you know that, you can find any tracked file in the cache in a few seconds. This guide finds a tracked file and a tracked folder in the cache with DVC 3.67.1, checks that the cached copy is the same file, and shows why you can't edit it.

Syntax​

Syntax
dvc cache dir
cat <cache dir>/files/md5/<first 2>/<other 30>
PartWhat it means
dvc cache dirPrints where the project's cache is. By default it's the .dvc/cache folder in the project
<first 2>The first two characters of the file's md5, from its .dvc file. They name a folder
<other 30>The other 30 characters of the md5. They're the file's name in the cache

DVC's internal files page explains it: "DVC calculates the file hash, a 32 characters long string (usually MD5). The first two characters are used to name the directory inside the cache, and the rest become the file name of the cached file."

The Example​

book-list is a Git repository where dvc init has run. It tracks data/books.csv, a list of two books, and the folder data/covers, with one small text file for each book, with dvc add.

Prerequisites​

  • DVC, in a Git repository where dvc init has run. Run dvc --version to check; this guide uses DVC 3.67.1.

Step 1: Read the File's Hash​

The hash is in the file's .dvc file:

Terminal
cat data/books.csv.dvc
data/books.csv.dvc
outs:
- md5: 8e459b5745608be993ce2cbbc1baf4a5
size: 64
hash: md5
path: books.csv

data/books.csv.dvc in a macOS-style code window: md5 8e459b5745608be993ce2cbbc1baf4a5, size 64, hash md5 and path books.csv.

The md5 is 8e459b5745608be993ce2cbbc1baf4a5, so the cache folder is 8e and the file name is 459b5745608be993ce2cbbc1baf4a5.

Step 2: Find the Cache Folder​

Terminal
dvc cache dir
Output
/Users/you/source/book-list/.dvc/cache

The terminal after dvc cache dir, which prints /Users/you/source/book-list/.dvc/cache.

The dvc cache dir reference says: "If no path value is given, it prints the current path of the cache directory." This project uses the default, .dvc/cache. The command is the same in PowerShell.

Step 3: Find the File​

List the 8e folder:

Terminal
ls .dvc/cache/files/md5/8e
Output
459b5745608be993ce2cbbc1baf4a5

There it is. Open it:

Terminal
cat .dvc/cache/files/md5/8e/459b5745608be993ce2cbbc1baf4a5
Output
title,author,year
Dune,Frank Herbert,1965
Emma,Jane Austen,1815

The terminal after ls .dvc/cache/files/md5/8e, which lists one file, 459b5745608be993ce2cbbc1baf4a5, and cat of that file, which prints the book list.

It's the book list. cat works in PowerShell too.

Step 4: Check It's the Same File​

Compute the MD5 hash of both files:

Terminal
md5 data/books.csv .dvc/cache/files/md5/8e/459b5745608be993ce2cbbc1baf4a5
Output
MD5 (data/books.csv) = 8e459b5745608be993ce2cbbc1baf4a5
MD5 (.dvc/cache/files/md5/8e/459b5745608be993ce2cbbc1baf4a5) = 8e459b5745608be993ce2cbbc1baf4a5

The terminal after md5 of data/books.csv and of the cached file: both print 8e459b5745608be993ce2cbbc1baf4a5, the md5 from the .dvc file.

Both files have the hash from the .dvc file, so they have the same contents. md5 is the macOS command; on Linux it's md5sum. PowerShell prints the same hash in capital letters.

Step 5: See Why You Can't Edit It​

Terminal
ls -l .dvc/cache/files/md5/8e/459b5745608be993ce2cbbc1baf4a5
Output
-r--r--r-- 1 you staff 64 Dec 4 10:48 .dvc/cache/files/md5/8e/459b5745608be993ce2cbbc1baf4a5

-r--r--r-- means read-only for everyone: r is read, and the missing w is write. Try to add a line to it:

Terminal
echo 'Ulysses,James Joyce,1922' >> .dvc/cache/files/md5/8e/459b5745608be993ce2cbbc1baf4a5
Output
zsh: permission denied: .dvc/cache/files/md5/8e/459b5745608be993ce2cbbc1baf4a5

The terminal after ls -l of the cached file, which shows -r--r--r--, and an echo that tries to add a line to it and fails with permission denied.

bash says Permission denied the same way, after the file's path, and the file stays as it was. DVC does this on purpose: in DVC 3.67.1, the dvc-data package sets cache files to mode 0o444, read-only for everyone, so a saved version can't change by accident. Your data/books.csv stays editable. On this Mac, DVC links it to the cache with a reflink, which the large dataset page says doesn't "carry any risk of cache corruption since the file system takes care of copying the file if you try to edit it in place". To change the data, edit data/books.csv and run dvc add again.

A Tracked Folder in the Cache​

A folder's .dvc file has one hash for the whole folder, ending in .dir:

Terminal
cat data/covers.dvc
data/covers.dvc
outs:
- md5: 9bd8f966b70e79956f5deaa229c0d1e3.dir
size: 28
nfiles: 2
hash: md5
path: covers

The same rule finds it: folder 9b, file d8f966b70e79956f5deaa229c0d1e3.dir.

Terminal
cat .dvc/cache/files/md5/9b/d8f966b70e79956f5deaa229c0d1e3.dir
Output
[{"md5": "fe97cabd88502fe2ac1debdaaad4ffc0", "relpath": "dune.txt"}, {"md5": "3d5bbabcc80de836a944057e95df6f0a", "relpath": "emma.txt"}]

The terminal after cat data/covers.dvc, whose md5 ends in .dir, and cat of the .dir file in the cache, a JSON list with the md5 and relpath of dune.txt and emma.txt.

The .dir file is a JSON list with one entry per file in the folder: its md5 and its relpath, the path inside the folder. Each of those files is in the cache under its own hash, in fe/ and 3d/. The internal files page says: "That's how DVC knows that the other two cached files belong in the directory."

List Everything in the Cache​

Terminal
find .dvc/cache -type f
Output
.dvc/cache/files/md5/9b/d8f966b70e79956f5deaa229c0d1e3.dir
.dvc/cache/files/md5/3d/5bbabcc80de836a944057e95df6f0a
.dvc/cache/files/md5/fe/97cabd88502fe2ac1debdaaad4ffc0
.dvc/cache/files/md5/8e/459b5745608be993ce2cbbc1baf4a5

The terminal after find .dvc/cache -type f, which lists four files: the .dir list, the two cover files and the book list.

Four files: the book list, the two cover files, and the folder's .dir list. No file in the cache has its original name.

Common Mistakes​

MistakeWhat happensDo this instead
Searching the cache for a file's nameNothing matches: cache files are named after their hashTake the md5 from the .dvc file
Using all 32 characters as the file nameNo such fileThe first 2 are a folder, the other 30 the file name
Editing a file inside .dvc/cachepermission deniedEdit the file in the project, then dvc add it

Troubleshooting​

SymptomLikely causeFix
permission denied when writing to a cache fileCache files are read-only on purposeLeave them alone; change the project's file and dvc add it
The .dir file is one long lineThat's its format: JSON without line breaksRead it as a list of md5 and relpath pairs

FAQ​

Why are there no file names in the cache?​

Because the cache is organized by contents. The internal files page says: "file hashes are calculated from file contents only. 2 or more files with different names but the same contents can exist in the workspace and be tracked by DVC, but only one copy is stored in the cache."

Is the remote organized the same way?​

Yes. A folder used as a remote has the same files/md5/<first 2>/<other 30> layout, as How to Upload Data to a DVC Remote with dvc push shows.

Conclusion​

data/books.csv.dvc gave the hash 8e459b57…, dvc cache dir gave the cache's place, and the file was at .dvc/cache/files/md5/8e/459b57…, with the same MD5 hash as the project's copy and read-only. A tracked folder has a .dir file in the cache that lists each of its files by hash.

Additional Resources​