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:
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:
$ 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.
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:
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.
# 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
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.
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.
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.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
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.
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 Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in 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.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
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.