Here’s a breakdown of all the ways you can interact with Databricks File System (DBFS)—whether from a notebook, via the Databricks CLI or the REST API.
1. Notebook “%fs” Magic Commands
(available in Python/Scala/R/SQL cells)
Command Description Example %fs ls <path> List files & folders in a directory %fs ls /dbfs/tmp/ %fs head <file> Show first 1 KB of a file %fs head /dbfs/tmp/data.csv %fs cp <src> <dst> Copy file or directory (no recursive flag!) %fs cp /dbfs/a /dbfs/b %fs mv <src> <dst> Move/rename file or directory %fs mv /dbfs/a.csv /dbfs/b.csv %fs rm <path> Remove file or empty dir (use -r for trees) %fs rm -r /dbfs/tmp/my_folder/ %fs mkdirs <path> Create a directory (and parents) %fs mkdirs /dbfs/tmp/new_folder %fs mount <src> <mnt> Mount external storage (S3, ADLS, etc.) see docs %fs unmount <mnt> Unmount a previously mounted path see docs %fs mounts List all mounts %fs mounts %fs refreshMounts Refresh mount table (rarely needed) %fs refreshMounts
2. dbutils.fs APIs
(same ops as above, callable from Python/Scala/R)
python Copy code
dbutils.fs.ls(path) # list
dbutils.fs.head(path, maxBytes=1024) # read head
dbutils.fs.cp(src, dst) # copy
dbutils.fs.mv(src, dst) # move
dbutils.fs.rm(path, recurse=False) # delete
dbutils.fs.mkdirs(path) # make dirs
# mount-related (for external mounts)
dbutils.fs.mount(...)
dbutils.fs.unmount(...)
dbutils.fs.mounts()
dbutils.fs.refreshMounts()
3. Databricks CLI (“databricks fs” group)
(must install & configure the Databricks CLI)
code Copy code
# list
databricks fs ls dbfs:/path/
# view head (1 KB)
databricks fs head dbfs:/file.csv
# copy (no recursive flag)
databricks fs cp dbfs:/src dbfs:/dst
# move
databricks fs mv dbfs:/old.dbfs:/new
# delete (add --recursive for trees)
databricks fs rm dbfs:/path --recursive
# mkdirs
databricks fs mkdirs dbfs:/new_folder
# upload local → DBFS
databricks fs put local.txt dbfs:/folder/remote.txt --overwrite
# download DBFS → local
databricks fs get dbfs:/folder/remote.txt local.txt --overwrite
4. REST API Endpoints
(HTTP POST to https:///api/2.0/dbfs/ )*
Endpoint Description Payload (JSON) POST /dbfs/list List directory contents { "path":"/dbfs/path" }POST /dbfs/get-status Get metadata (size, type) { "path":"/dbfs/file_or_dir" }POST /dbfs/mkdirs Create directory { "path":"/dbfs/new_folder" }POST /dbfs/delete Delete file or dir { "path":"/dbfs/target","recursive":true}POST /dbfs/move Move/rename file or directory { "source":"/dbfs/a","destination":".." }POST /dbfs/put Upload file (base64-encoded) { "path":"/dbfs/f","contents":"…","overwrite":true}POST /dbfs/get Download file (returns base64) { "path":"/dbfs/f","offset":0,"length":1048576}
That covers every official way to list, copy, move, delete, read and write data in DBFS—choose whichever interface best fits your workflow! 🚀