How to Track a Folder with dvc add
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
dvc add <folder>
| Part | What it means |
|---|---|
dvc add | Hashes 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 inithas run. Rundvc --versionto check; this guide uses DVC 3.67.1.
Step 1: See What's in the Folder
ls images
cat-1.png cat-2.png dog-1.png
Step 2: Track the Folder
dvc add images
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

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
cat images.dvc
outs:
- md5: 43700be2895cbefc3e92444c80db515f.dir
size: 551
nfiles: 3
hash: md5
path: images

Two things differ from a single file's .dvc file:
md5ends 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 sayssizeis the "sum of all files", here 551 bytes.
Step 4: Commit It
git add images.dvc .gitignore
git commit -m "Track the images folder with DVC"
[main 704407f] Track the images folder with DVC
2 files changed, 7 insertions(+)
create mode 100644 .gitignore
create mode 100644 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:
dvc status
images.dvc:
changed outs:
modified: images
Run dvc add on the folder again, and see what changed in images.dvc:
dvc add images
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
git diff images.dvc
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 .dir hash, the size and nfiles all changed. Commit images.dvc to save this version of the folder:
git commit -am "Add dog-2.png to the images"
[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:
dvc add images/cat-1.png
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
git status --short
M images.dvc
cat images.dvc
outs:
- md5: d22800d63c79581c18ce1d7dd71e7761.dir
nfiles: 4
hash: md5
path: images

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:
dvc add images
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
cat images.dvc
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
| Mistake | What happens | Do this instead |
|---|---|---|
Running dvc add on each file of a big folder | One .dvc file per file, and a long .gitignore | dvc add <folder> once |
Forgetting dvc add after changing the folder | dvc status keeps showing modified: images, and the commit still points at the old version | dvc add images, then commit images.dvc |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
changed outs: / modified: images | A file in the folder was added, changed or removed since the last dvc add | dvc add images and commit images.dvc |
size is missing from the .dvc file | A 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,sizeand the other fields.
