There is a folder, in it other files and folders (including hidden ones), which belong to different users. I want to assign everything to the current user:

sudo chown user:group * 

However, this command only works for non-hidden files and folders. I guessed that for the hidden you need to (the truth, it also affects . )

 sudo chown user:group .* 

And what to indicate instead of *, so that both categories are at once? We need a solution that is not tied to chown, that is, to work with any other team. I suspect that there is some simple solution with wildcards, but I do not know him.

There is also find, but it is recursive and much more cumbersome. No good.

 find . -exec chown user:group {} \+ 
  • ls -a1 | xargs -n1 'file' for example - Dmitry V.
  • @DmitryV .: it's almost like with find, also a train. But already better. I suspect that there must be some kind of expression, literally in a couple of characters. - Nick Volynkin ♦
  • Not sure .. You have enough specific requirements - so that it works for everyone and not be recursive. This "train" can be called a script with a name in two letters and put in ~ / bin :) - Dmitry V.

4 answers 4

If there are no exotic names like ..x , then absolutely simple: command .[^.]* *

The first expression is all names of two or more characters, starting with . except .. , and the second one everyone knows.

  • And this just solves the problem with “recursive recovery” to / , right? - Nick Volynkin ♦
  • one
    Yes decides. By the way, the “exotic” can obviously be added like this - ..?* - avp
  • and ../* will not fall into the filter ..?* ? - Nick Volynkin ♦
  • Not. There are no such names ( ./tram-pam-pam or ../ ). These are paths that are interpreted by the bash program (as it happens, see, for example, man 3 glob ), and among the names of the current table of contents are not. - avp 5:19
  • thanks, I will study. - Nick Volynkin ♦

The selector * determines access to all non- hidden files and directories. Instead, use the link to the current directory - . (point). So The command should look like this:

 sudo chown user:group . -R 

The -R switch adds recursiveness.

  • sudo chown user:group . -R sudo chown user:group . -R changes the ownership of files in the directory and the directory itself. And the -R key is not in every utility. - Nick Volynkin ♦
  • That is, if I want, for example, to put each file in a separate archive, then this syntax will not work. - Nick Volynkin ♦
 sudo chown user:group $(ls -A) any-command $(ls -A) 

With the -A option, the ls returns all files and folders, including hidden ones, but not including . and ..


Addition from andy.47 :

Do not use .* , Especially with rm , since sudo rm .* sudo rm -rf / equivalent to sudo rm -rf / .

  • It just occurred to me, just bear it in mind, under. * Unfortunately, they still fall . and .. which is easily seen by echo .* sometimes it interferes, for example, the ls that manages to climb a level higher. - Mike
  • I'm afraid this is not treated. Well, you can of course try to deploy the savvy, filter it with gray hair and somehow return it to the command line, but I'm afraid after this find you will find the most elementary solution :) - Mike
  • I put a '-' because sudo rm * .* equivalent to sudo rm -rf / . Do not try. Either as @NickVolynkin advises, or @DmitryV - andy.37
  • @NickVolynkin, ok) Just personally through it. A fresh backup was, fortunately) - andy.37
  • one
    @ andy.37: why is * .* equivalent to -rf / ? Something I do not understand, how here the recursion should work. - Nick Volynkin ♦

For bash there is a mode set by the integrated command

 shopt -s dotglob 

Then * will not ignore names beginning with a dot except . and .. A similar effect is achieved by setting the GLOBIGNORE variable GLOBIGNORE

 GLOBIGNORE='.' 

In quotes, you can put ignored names with a colon,. and .. will be ignored anyway, just something needs to be in order for the variable to take effect.

In some distributions sh refers to a dash or something else, in the script it is important to explicitly specify that bash is running.

To return to the standard mode run:

 shshopt -u dotglob unset GLOBIGNORE 
  • There is a problem: I work on a dozen remote machines, it’s problematic to change the bash settings - some scripts may break. But you can really do it on your own, thanks. - Nick Volynkin ♦
  • @NickVolynkin No need to change global settings. Execute one of the commands inside your script, the mode will not affect other scripts if they are not executed through the command . or source . - sercxjo