• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Chown Recursively

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Published: 8/3/2023

About Terminus

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

 # 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

Run in Warp

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:

 $ sudo chown -R owner:group directory

Run in Warp

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:

Thumbnail for

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

Thumbnail for

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:

Thumbnail for

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:

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

Run in Warp

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

Thumbnail for

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.

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

Run in Warp

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:

Thumbnail for

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

Thumbnail for

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.

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

Run in Warp

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

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Filed Under

Related Articles

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.

UnixLinux
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuRazvan Ludosanu

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

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.

Linux

Create Groups In Linux

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

Linux

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.

Linux

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.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

Copy Files In Linux

Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null