Re: [Help-bash] How to safely work on files/dirs with .
From:
Bob Proulx
Subject:
Re: [Help-bash] How to safely work on files/dirs with .
Date:
Sat, 12 Mar 2016 18:48:37 -0700
User-agent:
Mutt/1.5.24 (2015-08-30)
David Niklas wrote:
> The other day I tried to change the perms of the files in a temporary home
> dir as root with
> % chown -R test:test /home/test/.*
Only the dot files? Not all of the files? Why not all of the files?
> After noting that it was taking more then a few seconds I killed the
> command
Since "*" matches "." that ".*" also matched "..". Having matched
".." it was recursing down the entire /home directory. I am assuming
you have repaired that problem already by setting the other directory
ownerships back to what they needed to be already.
> and then used the path that I gave chown to ls and got the
> following output:
> % ls /home/test/.*
Better would have been:
ls -d /home/test/.* | less
> This is not what I wanted to access. I only want to change the file owner
> in my test user's home dir.
> How do I do this safely?
Use find for this. It is perfect.
find /home/test/ -exec chown test:test {} +
But if you really want to use 'chown -R' then just using it by itself
is fine. Just don't use any shell wildcards.
chown -R test:test /home/test/
If you want the bash way for matching characters that are not a dot:
chown -R test:test /home/test/.[!.]*
You can test the match out using 'ls' before running the chown.
ls -d /home/test/.[!.]* | less
Bob