Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article shows you how to use the Azure CLI to create and manage directories and files in storage accounts that have a hierarchical namespace.
All examples in this article assume a storage account that has a hierarchical namespace enabled, and they use Microsoft Entra authorization (--auth-mode login).
Prerequisites
An Azure subscription. For more information, see Get Azure free trial.
A storage account that has hierarchical namespace enabled. Follow these instructions to create one.
Azure CLI version
2.6.0or later.
Ensure that you have the correct version of Azure CLI installed
Open the Azure Cloud Shell, or if you installed the Azure CLI locally, open a command console application such as Windows PowerShell.
Verify that your installed Azure CLI version is
2.6.0or later by using the following command.az --versionIf your Azure CLI version is earlier than
2.6.0, install a later version. For more information, see Install the Azure CLI.
Connect to the account
If you're using Azure CLI locally, run the login command.
az loginIf the CLI can open your default browser, it opens the browser and loads an Azure sign-in page.
Otherwise, open a browser page at https://aka.ms/devicelogin and enter the authorization code displayed in your terminal. Then, sign in with your account credentials in the browser.
To learn more about different authentication methods, see Authorize access to blob or queue data with Azure CLI.
If your identity is associated with more than one subscription, and you're not prompted to select the subscription, set your active subscription to the subscription of the storage account that you want to operate on. In this example, replace the
<subscription-id>placeholder value with the ID of your subscription.az account set --subscription <subscription-id>Replace the
<subscription-id>placeholder value with the ID of your subscription.
Note
The example presented in this article shows Microsoft Entra authorization. To learn more about authorization methods, see Authorize access to blob or queue data with Azure CLI.
Create a container
A container acts as a file system for your files. Create one by using the az storage fs create command.
This example creates a container named my-file-system.
az storage fs create -n my-file-system --account-name mystorageaccount --auth-mode login
To verify that the container exists, run the az storage fs show command.
Show container properties
Use the az storage fs show command to print the properties of a container to the console.
az storage fs show -n my-file-system --account-name mystorageaccount --auth-mode login
List container contents
Use the az storage fs file list command to list the contents of a container.
This example lists the contents of a container named my-file-system.
az storage fs file list -f my-file-system --account-name mystorageaccount --auth-mode login
Delete a container
Use the az storage fs delete command to delete a container.
This example deletes a container named my-file-system.
az storage fs delete -n my-file-system --account-name mystorageaccount --auth-mode login
Create a directory
Use the az storage fs directory create command to create a directory reference.
This example adds a directory named my-directory to a container named my-file-system that is located in an account named mystorageaccount.
az storage fs directory create -n my-directory -f my-file-system --account-name mystorageaccount --auth-mode login
To verify that the directory exists, run the az storage fs directory exists command.
Show directory properties
Print the properties of a directory to the console by using the az storage fs directory show command.
az storage fs directory show -n my-directory -f my-file-system --account-name mystorageaccount --auth-mode login
Rename or move a directory
Rename or move a directory by using the az storage fs directory move command.
This example renames a directory from the name my-directory to the name my-new-directory in the same container.
az storage fs directory move -n my-directory -f my-file-system --new-directory "my-file-system/my-new-directory" --account-name mystorageaccount --auth-mode login
This example moves a directory to a container named my-second-file-system.
az storage fs directory move -n my-directory -f my-file-system --new-directory "my-second-file-system/my-new-directory" --account-name mystorageaccount --auth-mode login
Delete a directory
Delete a directory by using the az storage fs directory delete command.
This example deletes a directory named my-directory.
az storage fs directory delete -n my-directory -f my-file-system --account-name mystorageaccount --auth-mode login
Check if a directory exists
Determine whether a specific directory exists in the container by using the az storage fs directory exists command.
This example reveals whether a directory named my-directory exists in the my-file-system container.
az storage fs directory exists -n my-directory -f my-file-system --account-name mystorageaccount --auth-mode login
Download from a directory
Download a file from a directory by using the az storage fs file download command.
This example downloads a file named upload.txt from a directory named my-directory.
az storage fs file download -p my-directory/upload.txt -f my-file-system -d "C:\myFolder\download.txt" --account-name mystorageaccount --auth-mode login
List directory contents
Use the az storage fs file list command to list the contents of a directory.
This example lists the contents of a directory named my-directory that is located in the my-file-system container of a storage account named mystorageaccount.
az storage fs file list -f my-file-system --path my-directory --account-name mystorageaccount --auth-mode login
Upload a file to a directory
Upload a file to a directory by using the az storage fs file upload command.
This example uploads a file named upload.txt to a directory named my-directory.
az storage fs file upload -s "C:\myFolder\upload.txt" -p my-directory/upload.txt -f my-file-system --account-name mystorageaccount --auth-mode login
To verify that the file uploaded, run the az storage fs file list command.
Show file properties
Print the properties of a file to the console by using the az storage fs file show command.
az storage fs file show -p my-file.txt -f my-file-system --account-name mystorageaccount --auth-mode login
Rename or move a file
Rename or move a file by using the az storage fs file move command.
This example renames a file from the name my-file.txt to the name my-file-renamed.txt.
az storage fs file move -p my-file.txt -f my-file-system --new-path my-file-system/my-file-renamed.txt --account-name mystorageaccount --auth-mode login
Delete a file
Delete a file by using the az storage fs file delete command.
This example deletes a file named my-file.txt.
az storage fs file delete -p my-directory/my-file.txt -f my-file-system --account-name mystorageaccount --auth-mode login