Chown Recursively

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: August 3, 2023

On Unix-like and Linux operating systems such as Ubuntu, the  chown command is used to change the ownership of files and directories.

The short answer

Bash
# using -R
 $ sudo chown -R owner:group directory
 # using find
 $ find . -type d -exec sudo chown user:group {} \;
 # using natural language with Warp AI Command Search
 $ # chown recursive just files

chown recursively with chown -R

The first option for recursively changing the ownership of the files and subdirectories contained in a directory, is to use the -R option flag as follows:

Bash
$ sudo chown -R owner:group directory

Where:

  • owner is the username of the new owner
  • group is the name of the new group
  • directory is the path to the target directory

For example, given the following folder structure in /var/www:

To recursively transfer the ownership of the /var/www directory to the johndoe user and the developers group, you can run the following command:

chown recursively using the find command

Another option for recursively changing the ownership of files based on their type or name, is to use a combination of the find and chown commands.

On its own, the find command is used to recursively search for files and directories based on a certain criteria.

For example, the following command will list all the subdirectories present in the /var/www directory:

Executing chown with the -exec option flag

To change the ownership of files returned by the find command, you can use the -exec option flag followed by the chown command:

Bash
$ find . -type d -exec sudo chown user:group {} \;

Where the {} \; expression will be replaced at runtime by each file path returned by find.

Alternatively, you can speed up the process using the {} + expression, which will be replaced at runtime by as many file paths as possible for each execution of chown, instead of one by one.

Bash
$ find . -type d -exec sudo chown user:group {} +

Remind yourself how to chown recursively with natural language

Warp has a pretty handy feature called Artificial Intelligence Command Search (AICS) that helps generate shell commands with natural language.

For example, to retrieve the previous command using the AICS, you can start by typing a # sign, followed by a short sentence describing your command:

You can now press cmd + Enter to edit and use the suggested command:

A word of caution

When using chown -R in combination with wildcards \, you should be extra careful with the patterns matched by your command.

For instance, patterns such as .\ will match both hidden files beginning with a dot character (e.g. .env) as well as the hard link to the parent directory (i.e. ..).

You should also be careful not to insert any undesirable spaces or typos in the path of the target directory, especially if your path starts at the root directory (/), as you might otherwise end up with a broken system.

Bash
# DO NOT RUN THIS COMMAND
 $ sudo chown -R / var/www

As a rule of thumb, it is usually discouraged to change the ownership of files that belong to the system or the root user.

Curious about why we keep using sudo? Read more about why the chown command requires sudo to be executed.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.