Using chown command.

I am working on a test machine.
I just discovered that I have misunderstood the way the following command is run.

chown -Rv some_user:users /some_folder/*

This command do exactly what I want. Change the owner of every things from the named folder and in all child folders.
But of course it leave untouched all hidden object like '.some_dir' and '.some_file'
So I run the following command ( and problem arise )

chown -Rv some_user:users /some_folder/.*

I can see on the screen that the command is climbing up one directory level doing something like

..........
..........
..........
chown /some_folder/../some_other_folder/.......
..........
..........
..........

What is the correct command to change every things without climbing up one level.

Do you have access rights to the hidden files that you are trying to change the owner of?

Often hidden files are configuration/shell/user files created by the system manager and the user cannot change them.

If you are the system manager then try using your root account to change the owner.

chown -Rv some_user:users /some_folder

should change the owner of the folder /some_folder and all its files and sub-directories.

If you don't want to change the ownership of /some_folder the following will work for all hidden files with the second character being lower-case letters:

chown -Rv some_user:users /some_folder/.[a-z]*

so it will pick up files such as .bashrc but not .SciTEUser.properties .

Obviously you can extend the pattern to allow upper-case and numerals if necessary.

Andrew

You could also set the dotglob control setting to have the shell include filenames starting with a . in the results of pathname expansion:

$ shopt -s dotglob
$ ls -d *
.bash_profile    .bashrc    .exrc    bin
$ shopt -u dotglob
$ ls -d *
bin

Attention: the SysV chown changes the target of symbolic links; normally you want to change the owner of the symbolic links so you need the -h option
(old Linux chown was different, like BSD chown).
In a shell glob the [!.] is any character but a dot. (A few shells have [^.] in addition, that is not portable).

chown -hRv some_user:users /some_folder/* /some_folder/.[!.]*

Perhaps you can include the parent directory? Then it becomes simple:

chown -hRv some_user:users /some_folder

chown does not ignore hidden files. Only ls does that, as a user convenience.

Those files must have been preserved for some other reason, probably different ownership than you expected, and that won't change until you fix that.

1 Like

@Corona688.....Thanks for the concise explanation. I tried to say that in post#2 without success.

Hello.

My question was :
When using this command with elevated privileges ( after su or sudo ) :

chown -Rv some_user:users /some_folder/.*

Following the message on screen, the system goes on level higher as shown

..........
..........
chown /some_folder/../some_other_folder/.......
..........
..........

As "/some_folder/.." is root level, I mess up the system.

What is the good way to change owner for every things including hidden objects starting from /some_folder including all sub-folder.

Any help is welcome

If I understand you correctly.......

 
 # cd /some_folder
 # chown <new owner> *
 

would change all files (including hidden IF you have access rights to them) at that level.

Then adding -R switch to that command line would cause the whole tree below that point to also change ownership IF you have access rights to all of that.

Ok noted.

But if I remember correctly ( but I may wrong ), I did that command on command line from terminal, on user level for a particular user, ( /home/another_user ).

sudo chown another_user:users /home/another_user/*

And it seems to me that files/directories which names start with a dot ( hidden object ) was not all modified.
So I retype the command like that :

sudo chown another_user:users /home/another_user/.*

Then the system was messed up.
I did not imagine that "/home/some_user/.*" could climb up to "/"

Thank you for helping

I can only think that you accidentally put a space between / and home when entering the command.

No, it is because .* matches .. and .
Test the glob expansion with echo!
This is for the arguments = start files.
If one of the start files is a directory, chown -R will recurse into it and find ALL files.

2 Likes

The problem with your guess is that UNIX doesn't have hidden files.

When you use readdir() , it does not hide dot files. Hiding dot files is not a UNIX filesystem feature, UNIX kernel feature, or UNIX library feature.

Its a shell feature. And ls, grandfathered in from 45 years ago.

Meaning, there are two and only two ways to implicitly exclude dot-files:

  1. Getting the list of files from the shell a la chown -R user:pass path/*
  2. Getting the list of files from ls a la ls | xargs chown -R user:path

The command you listed will never exclude dot-files. Either you did something like the above and omitted it not thinking it relevant, or the dot files have a different owner than you expected, or some filesystem magic like immutable bits is preventing you from changing them.

Just doing ls -l /path/to/.dotfile and posting the result would have solved this argument for good a week ago.

That was my big mistake.

---------- Post updated at 19:36 ---------- Previous update was at 19:34 ----------

Thank you everybody for helping