Skip to main content

How to Track a Folder with dvc add

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

A dataset is often a folder of many files, such as thousands of images. You don't need a .dvc file for each one: dvc add on the folder tracks all of them with a single .dvc file. This guide tracks a folder of photos, reads its .dvc file, adds a new photo, and updates one file inside the folder, with DVC 3.67.1.

Syntax​

Syntax
dvc add <folder>
PartWhat it means
dvc addHashes every file in the folder, copies them into DVC's cache, and writes <folder>.dvc next to the folder
<folder>The folder to track, from the current folder

The dvc add reference says DVC "does not produce individual .dvc files for each one. Instead, the single .dvc file references a special JSON file in the cache (with .dir extension), that in turn points to the added files."

The Example​

pet-photos is a Git repository where dvc init has run. Its images folder holds three photos for a cat-and-dog classifier: cat-1.png, cat-2.png and dog-1.png. More photos will arrive, so the folder is tracked as a whole.

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: See What's in the Folder​

Terminal
ls images
Output
cat-1.png cat-2.png dog-1.png

Step 2: Track the Folder​

Terminal
dvc add images
Output
100% Adding...|███████████████████████████████████████████████████████████|1/1 [00:00, 36.04file/s]

To track the changes with git, run:

git add images.dvc .gitignore

To enable auto staging, run:

dvc config core.autostage true

The terminal after ls images, which lists three photos, and dvc add images, which ends with the hint to run git add images.dvc .gitignore.

DVC made one images.dvc file next to the folder, and added /images to the .gitignore in the same place.

Step 3: Read the .dvc File​

Terminal
cat images.dvc
images.dvc
outs:
- md5: 43700be2895cbefc3e92444c80db515f.dir
size: 551
nfiles: 3
hash: md5
path: images

images.dvc in a macOS-style code window: an outs list with md5 43700be2895cbefc3e92444c80db515f.dir, size 551, nfiles 3, hash md5 and path images.

Two things differ from a single file's .dvc file:

  • md5 ends in .dir: it's the hash of the folder's list of files, the special JSON file in the cache. Change, add or remove any file in the folder, and this hash changes.
  • nfiles: 3: the number of files in the folder. The .dvc files page says size is the "sum of all files", here 551 bytes.

Step 4: Commit It​

Terminal
git add images.dvc .gitignore
git commit -m "Track the images folder with DVC"
Output
[main 704407f] Track the images folder with DVC
2 files changed, 7 insertions(+)
create mode 100644 .gitignore
create mode 100644 images.dvc

The terminal after the commit: commit 704407f creates .gitignore and images.dvc.

Git now tracks two small text files, however many photos the folder holds. Every command in this guide works unchanged in PowerShell.

Step 5: Add a File to the Folder​

A fourth photo, dog-2.png, is copied into images. DVC notices the folder changed:

Terminal
dvc status
Output
images.dvc:
changed outs:
modified: images

Run dvc add on the folder again, and see what changed in images.dvc:

Terminal
dvc add images
Output
100% Adding...|███████████████████████████████████████████████████████████|1/1 [00:00, 66.47file/s]

To track the changes with git, run:

git add images.dvc

To enable auto staging, run:

dvc config core.autostage true
Terminal
git diff images.dvc
Output
diff --git a/images.dvc b/images.dvc
index 085cca0..e8b7bb2 100644
--- a/images.dvc
+++ b/images.dvc
@@ -1,6 +1,6 @@
outs:
-- md5: 43700be2895cbefc3e92444c80db515f.dir
- size: 551
- nfiles: 3
+- md5: 06097114df9ce90b17554d2577a0f67c.dir
+ size: 737
+ nfiles: 4
hash: md5
path: images

The terminal after dvc status shows images as modified, then dvc add images and git diff images.dvc: the .dir hash changed, size went from 551 to 737 and nfiles from 3 to 4.

The .dir hash, the size and nfiles all changed. Commit images.dvc to save this version of the folder:

Terminal
git commit -am "Add dog-2.png to the images"
Output
[main 62bb332] Add dog-2.png to the images
1 file changed, 3 insertions(+), 3 deletions(-)

Update One File Inside the Folder​

You can also give dvc add the path of one file inside a tracked folder. The reference says: "If the target is a part of an existing dataset, only that portion of the dataset is updated, and associated .dvc file is updated to reflect the changes." Here cat-1.png was replaced with a brighter version:

Terminal
dvc add images/cat-1.png
Output
100% Adding...|███████████████████████████████████████████████████████████|1/1 [00:00, 54.41file/s]

To track the changes with git, run:

git add images.dvc

To enable auto staging, run:

dvc config core.autostage true
Terminal
git status --short
Output
M images.dvc
Terminal
cat images.dvc
Output
outs:
- md5: d22800d63c79581c18ce1d7dd71e7761.dir
nfiles: 4
hash: md5
path: images

The terminal after dvc add images/cat-1.png: DVC updated images.dvc, which now has a new .dir hash and nfiles 4 but no size line.

DVC updated images.dvc, not a new .dvc file, but the size line is gone. In DVC 3.67.1's output.py, a partial update keeps the total size only when files were just added or nothing changed; for a replaced file it can't know the old file's size, so it leaves size out. Adding the whole folder again puts it back:

Terminal
dvc add images
Output
100% Adding...|███████████████████████████████████████████████████████████|1/1 [00:00, 65.38file/s]

To track the changes with git, run:

git add images.dvc

To enable auto staging, run:

dvc config core.autostage true
Terminal
cat images.dvc
Output
outs:
- md5: d22800d63c79581c18ce1d7dd71e7761.dir
nfiles: 4
hash: md5
path: images
size: 736

Same .dir hash, and size: 736 is back, at the end.

Common Mistakes​

MistakeWhat happensDo this instead
Running dvc add on each file of a big folderOne .dvc file per file, and a long .gitignoredvc add <folder> once
Forgetting dvc add after changing the folderdvc status keeps showing modified: images, and the commit still points at the old versiondvc add images, then commit images.dvc

Troubleshooting​

SymptomLikely causeFix
changed outs: / modified: imagesA file in the folder was added, changed or removed since the last dvc adddvc add images and commit images.dvc
size is missing from the .dvc fileA file inside the folder was updated with dvc add <folder>/<file>dvc add <folder> writes it back

FAQ​

How do I see which files the .dir hash stands for?​

The .dir file in the cache is a JSON list of every file's name and MD5 hash; the cache guide below shows how to open it.

Does DVC copy every file twice?​

Not where the disk supports reflinks, links that share the data until one copy changes. The reference says: "By default, DVC tries to use reflinks (see File link types) to avoid copying any file contents". On this Mac, with Apple's APFS, dvc doctor lists reflink first among its cache types.

Conclusion​

dvc add images tracked three photos with one images.dvc file whose .dir hash stands for the whole folder. Adding a photo changed that hash, the size and nfiles, and dvc add images/cat-1.png updated one file inside the folder, at the price of the size line until the next full dvc add images.

Additional Resources​

  • dvc add — "Adding entire directories" and granular targets.
  • .dvc files — nfiles, size and the other fields.