How to change file ownership?

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
cobaka
Posts: 521
Joined: Thu Jul 16, 2020 6:04 am
Location: Central Coast, NSW - au
Has thanked: 87 times
Been thanked: 49 times

How to change file ownership?

Post by cobaka »

Hello cobaka here - i.e. 'puppy' but @Grey explained cobaka is a 'dog'. Шенок is a 'puppy'. So I got that wrong too ....
Now, to explain the problem ... errr, my present problem ...

I wanted to change the owner of all the files in a directory from "webuser" to (preferably) spot or alternately the real "me" - #root. That's who 'the real puppy' thinks I am. 'The real puppy' does not know 'webuser' - and I have no idea from whence that name derived.

I used the terminal, typing in few commands to do this. I proceeded, picking all files beginning with 'a', then 'b' and so on.
My scheme was working (see below) You can see (in the snapshop of the screen) that spot owns the files at the top of the listing.
Lower down, starting at "i" the files are still owned by webuser (whoever that is!).
(Incidently - all the files in the directory came by extracting a *tar.gz file I got from github.)

So - here is the ROX window:

zzz_screen-grab_fr._ROX.png
zzz_screen-grab_fr._ROX.png (129.67 KiB) Viewed 548 times

Ain't it lovely? spot owns forthload.asm etc. webuser owns io.c etc. May the fleas from one thousand camels infest the under-wear of web-user!

Of course I'm impatient. I can use a larger wild-card than ./a* or ./b*! (See code, below) And (of course) a 'dot' will signify any character in the alphabet.
Here is a copy of the last few lines of code I ran.

root# chown -c spot ./c*
changed ownership of './cond09.asm' to 502:1002
changed ownership of './cond09.inc' to 502:1002
changed ownership of './core.4' to 502:1002
changed ownership of './cross09.4' to 502:1002
root# chown -c spot ./e*
changed ownership of './engine.c' to 502:1002
changed ownership of './exampl.bas' to 502:1002
changed ownership of './extend09.4' to 502:1002
root# chown -c spot ./f*
changed ownership of './forthload.asm' to 502:1002
root# chown -c spot ./.* <--- this created the problem!
changed ownership of './.' to 502:1002
changed ownership of './..' to 502:0
changed ownership of './.~lock.README.doc#' to 502:1002

Uh-oh! The tilde character is commonly used to denote the root directory.
But I'm working in a directory called sbc09
It appears I changed ownership of files not intended, yes?
If 'yes' then "what have I done?". That's my "Q"!
Help!

Comment: I use bash/terminal somewhat - but I'm well below average competence.
That's why I need to ask.

cobaka

собака --> это Русский --> an old dog
"so-baka" (not "co", as in coast or crib).

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: Help! What have I done?

Post by Burunduk »

cobaka wrote: Mon May 15, 2023 7:48 am

Шенок is a 'puppy'.

Щенок is a 'puppy'.

And (of course) a 'dot' will signify any character in the alphabet.

In a regular expression (regex). You are using the bash globbing (wildcards) here, so ? matches any single character not a dot.

If you want to change the owner (and the group) of all the files in a given directory, you can use this command:

Code: Select all

chown -R spot:spot /my/directory

To change only the owner (not the group):

Code: Select all

chown -R spot /my/directory

-R means 'recurse'.

changed ownership of './.' to 502:1002
changed ownership of './..' to 502:0
changed ownership of './.~lock.README.doc#' to 502:1002

You've changed the owner of the current directory (sbc09), its parent directory (sbc), and one hidden file (it's a libreoffice lock file inside the sbc09 directory, ~ here doesn't stand for root). It's easy to undo this.

User avatar
Grey
Posts: 2003
Joined: Wed Jul 22, 2020 12:33 am
Location: Russia
Has thanked: 75 times
Been thanked: 365 times

Re: Help! What have I done?

Post by Grey »

cobaka wrote: Mon May 15, 2023 7:48 am

Шенок is a 'puppy'.

Yes, Burunduk is right, there is a tail at the letter, a puppy can have such a tail :) Шnot=Щ

Burunduk wrote: Mon May 15, 2023 8:18 am

Щенок is a 'puppy'.

Yeah, now it's clear why Burunduk.
It seems that there are not 3, but 4 Russian-speaking members on the forum.

Fossapup OS, Ryzen 5 3600 CPU, 64 GB RAM, GeForce GTX 1050 Ti 4 GB, Sound Blaster Audigy Rx with amplifier + Yamaha speakers for loud sound, USB Sound Blaster X-Fi Surround 5.1 Pro V3 + headphones for quiet sound.

User avatar
mikewalsh
Moderator
Posts: 5662
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 599 times
Been thanked: 1738 times

Re: How to change file ownership?

Post by mikewalsh »

Yeah, that's what I'd do, too. It's an easy enough 'fix'.

Simply open a terminal, then issue the command

Code: Select all

chown -R spot:spot /my/directory

.....where you'd replace '/my/directory' with the path to the one desired. '-R' in this case being the 'recursive', indicating everything within the named directory, including hidden files & directories. (Don't use a small '-r', because it won't work. This is recognised as something else entirely; I'm not quite sure what).

Or

Code: Select all

chown -R root:root /my/directory

.....if '/root' ownership is required.

Very simple. And much easier to do it from outside the directory in question, than to mess around with globs & wildcards from within. Hope that helps!

Mike. ;)

Puppy "stuff" ~ MORE Puppy "stuff" ~ ....and MORE! :D
_______________________________________________________

Image

Post Reply

Return to “Programming”