DBUTILS in Databricks

In this post, we are going to learn about the dbutils and its’ command available DBFS Databricks File System.

Overview

The dbutils contain file-related commands. It used to contain all these utilities in dbutils.fs. It makes it easy to work with files available at databricks file system.

dbutils.fs Commands

Below are the listed command:

COMMANDDESCRIPTIONUSE
lsUsed to list files under a directory

%python or %scala

dbutils.fs.ls(“/directory_name”)

                        OR

%fs
ls /directory_name

mkdirsUsed to create directory

%python or %scala

dbutils.fs.mkdirs(“/directory_name”)

           OR

%fs

mkdirs /directory_name

putUsed to overwrite file with provided value

%python or %scala

dbutils.fs.put(“file_name.txt”, “Input String”)

                        OR

%fs
put -f “file_name.txt” “Input String”

headUsed to read the top content of the file

%python or %scala

dbutils.fs.head(“file_name.txt”)

                        OR

%fs
head “file_name.txt”

rmRemove file

%python or %scala

dbutils.fs.rm(“file_name.txt”)

                        OR

%fs
rm “file_name.txt”


You can prefix with dbfs:/ (eg. dbfs:/file_name.txt) with the path to access the file/directory available at the databricks file system.

For deleting the files of a folder recursively, use the below command:

 %fs rm -f <folder_name>

Wrapping

We can use these dbutils commands on the databricks notebook to access the files available in the databricks file system to perform any action listed above.

Sharing is caring!

Subscribe to our newsletter
Loading

Leave a Reply