Chown Recursively

Last Updated on 2023-02-14

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

[#short-answer]The short answer[#short-answer]

 # 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-r][.inline-code]chown[.inline-code] recursively with [.inline-code]chown -R[.inline-code][#chown-r]

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

 $ sudo chown -R owner:group directory


  • [.inline-code]owner[.inline-code] is the username of the new owner
  • [.inline-code]group[.inline-code] is the name of the new group
  • [.inline-code]directory[.inline-code] is the path to the target directory

For example, given the following folder structure in [.inline-code]/var/www[.inline-code]:

To recursively transfer the ownership of the [.inline-code]/var/www[.inline-code] directory to the [.inline-code]johndoe[.inline-code] user and the [.inline-code]developers[.inline-code] group, you can run the following command:

[#chown-with-find][.inline-code]chown[.inline-code] recursively using the [.inline-code]find[.inline-code] command[#chown-with-find]

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

On its own, the [.inline-code]find[.inline-code] 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 [.inline-code]/var/www[.inline-code] directory:

[#chown-with-exec]Executing [.inline-code]chown[.inline-code] with the [.inline-code]-exec[.inline-code] option flag[#chown-with-exec]

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

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

Where the [.inline-code]{} \;[.inline-code] expression will be replaced at runtime by each file path returned by [.inline-code]find[.inline-code].

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

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

[#chown-with-natural-lang]Remind yourself how to [.inline-code]chown[.inline-code] recursively with natural language[#chown-with-natural-lang]

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 [.inline-code]#[.inline-code] sign, followed by a short sentence describing your command:

You can now press [.inline-code]cmd[.inline-code] + [.inline-code]Enter[.inline-code] to edit and use the suggested command:

[#caution]A word of caution[#caution]

When using [.inline-code]chown -R[.inline-code] in combination with wildcards [.inline-code]*[.inline-code], you should be extra careful with the patterns matched by your command.

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

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 ([.inline-code]/[.inline-code]), as you might otherwise end up with a broken system.

 $ 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 [.inline-code]sudo[.inline-code]? Read more about why the [.inline-code]chown[.inline-code] command requires [.inline-code]sudo[.inline-code] to be executed.