When running chown -R user:group directory/
, you might have noticed it doesn't affect hidden files (those starting with dot). This behavior occurs because some shell implementations don't expand wildcards to include dotfiles by default.
The most reliable method uses find with xargs:
find /path/to/directory -exec chown user:group {} +
For Bash users, you can enable dotglob temporarily:
shopt -s dotglob
chown -R user:group directory/
shopt -u dotglob
Or using zsh's recursive globbing:
chown -R user:group directory/**/*
The find+xargs method is generally fastest for large directory structures:
find /path -type d -exec chown user:group {} \;
find /path -type f -exec chown user:group {} \;
Changing ownership of a web directory including .htaccess files:
find /var/www/html/ -exec chown www-data:www-data {} +
When managing Linux systems, you've probably encountered this frustrating scenario: running chown -R
appears to work, but hidden files and directories (those starting with a dot) mysteriously remain unchanged. This isn't actually a bug in chown
- it's a fundamental behavior in how shell globbing works.
The issue occurs because the shell expands the *
wildcard before chown
sees it. By default, most shells won't include dotfiles in this expansion. For example:
# This won't affect hidden files
chown -R user:group *
Here are three robust methods to handle hidden files:
Method 1: Using find with -exec
find /path/to/directory -exec chown user:group {} +
Method 2: Explicitly including dotfiles
chown -R user:group /path/to/directory/.[!.]* /path/to/directory/*
Method 3: Using shopt in bash
shopt -s dotglob
chown -R user:group /path/to/directory/*
shopt -u dotglob
For system administrators managing web servers, here's a practical example that changes ownership while preserving permissions:
find /var/www/html -type d -exec chown www-data:www-data {} \;
find /var/www/html -type f -exec chown www-data:www-data {} \;
The find
approach is generally most efficient for large directory trees. When dealing with millions of files, consider adding -xdev
to prevent crossing filesystem boundaries:
find /path -xdev -exec chown user:group {} +
Be cautious with special directories like .
and ..
. The .[!.]*
pattern helps avoid these, but when in doubt, the find
method is safest.