Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to set group:owner to all directories created with "mkdir -p" (or other command(s))?

24 views
Skip to first unread message

Bengt T

unread,
Feb 16, 2024, 9:04:06 AMFeb 16
to
Suppose that tmp_dir_1, tmp_dir_2 and tmp_dir_3 do not exist. With "mkdir -p tmp_dir_1/tmp_dir_2/tmp_dir_3" the following is created:

!-- tmp_dir_1
....!-- tmp_dir_2
........!-- tmp_dir_3

The group:owner of all tmp_dir_...-s are set the group:owner of the user generating the "mkdir..." the command.

How to set group:owner to others, for all tmp_dir_...-s, directly in a script with command(s), without having to manually
do so afterwards?

J.O. Aho

unread,
Feb 16, 2024, 4:13:53 PMFeb 16
to
one liner:

sudo find /path/to/where/the/temp/dirs/are/located -type d -name
"tmp_dir_*" -exec chown user:group "{}" \;


example:

mkdir -p root_dir/tmp_dir_1/{not_temp,tmp_dir_2,tmp_dir_3}/{dir_4,tmp_dir_5}
tree root_dir
### output of tree ###
root_dir/
└── tmp_dir_1
├── not_temp
│   ├── dir_4
│   └── tmp_dir_5
├── tmp_dir_2
│   ├── dir_4
│   └── tmp_dir_5
└── tmp_dir_3
├── dir_4
└── tmp_dir_5

11 directories, 0 files
### end of tree output ###
sudo find root_dir -type d -name "tmp_dir_*" -exec chown user:group "{}" \;

If you want all directories regardless name, then remove the '-name
"tmp_dir_*"' part.
If you want to change all directories and files, then the simple one is just

sudo chown -R user:group root_dir/*

or if you want the root dir to change too

sudo chown -R user:group root_dir



Or you can do:

sudo -u user mkdir -p
root_dir/tmp_dir_1/{not_temp,tmp_dir_2,tmp_dir_3}/{dir_4,tmp_dir_5}

and all the directories are already owned by the user, if the user can
write to the directory where the command is run.

--
//Aho

--
//Aho

Paul

unread,
Feb 16, 2024, 6:42:04 PMFeb 16
to
J.O. Aho has given you the answer.

Here are some one-liner commands from my notes file.

find /media/sample -type d -exec ls -al -1 -d {} + > /mnt/temp/directories.txt
find /media/sample -type f -exec ls -al -1 {} + > /mnt/temp/filelist.txt

Just be careful when crafting material like this, that you do not
accidentally lose access to the area you are working in. Sometimes,
the order you execute a series of one-liners, has consequences.

For an amateur administrator, creating a "sample tree" for practicing
the commands, is a good idea. Then, only the small sample area is
damaged by your mistake. Rather than a large tree of files that
will take a lot of time to correct.

Even how a partition is mounted, matters. It's possible for example,
for a partition to be mounted in such a way, that gratuitous usage
of "sudo" does not work. So the very first thing you do as an
amateur admin, is check how a partition is mounted, the exact command used.
To see if the mount happens to be a "tricky one". An obvious case of a
tricky mount, is a read-only mount, where you won't be changing any
permissions or ownership, any time soon. You can remount rw to fix that.

Paul

Bengt T

unread,
Feb 17, 2024, 2:18:14 AMFeb 17
to
Many thanks for helpful assistance!

It seen to me that the "procedure"
sudo -u user mkdir -p root_dir/tmp_dir_1/{not_temp,tmp_dir_2,tmp_dir_3}/{dir_4,tmp_dir_5}
may suit me.

/Bengt
0 new messages