Bash Scripting - How to Set the Group That New Files Will Be Created With

Bash Scripting - How to set the group that new files will be created with?

The newgrp command is used to change the current group ID during a login session.

New directories created in that session will have the group ID set by the command.

newgrp(1)

Set default group for newly created files/folders in linux?

If you are using the mkdir command the ownership is set to the user who creates the folder and the group ownership will be set to the primary group of that user.

You can use the install command to create a folder with a different ownership:

sudo install -o www-data -g www-data -d test

The above command creates the folder test and sets ownership and group ownership to www-data (for example)


Of course you can also use

sudo -u www-data mkdir test

to create a folder owned by USER and group owned by it's primary group. It leads to the same results as the install command above.

Change Default Group in Script

Try the newgrp command, which changes the primary group of a user into another group of which that user is a member:

#!/bin/bash

newgrp groupb << END
touch "myscript_output.txt"
END

shell script to create empty files with all possible permissions

Do a loop:

for ((i=0; i < 512; i++)); do 
mod=$(printf "%03o" "$i");
touch ${mod}.txt; chmod $mod $mod.txt;
done

Rather than trying to construct the names, if you want the names to look like the output of ls -l, just do something like

for ((i=0; i < 512; i++)); do
mod=$(printf "%03o" "$i")
touch ${mod}.txt
chmod $mod $mod.txt
n=$(ls -l $mod.txt | cut -b1-10)
mv -- $mod.txt "$n.txt"
done

Linux - how to set group permissions for new files in advance

There are several mechanisms related to permissions manipulations:

  • umask allows you to disable permission bits per application. It doesn't allow neither enabling permissions nor doing it per directory. It's also not a global setting. It is a per-process attribute that is inherited from parent process. E.g. you can set its default value in init script, but programs (especially daemons) may overwrite their umask.
  • sticky bit restricts directory access so that files in that directory may only be unlinked or renamed by root or the directory owner or the file owner.
  • setgit bit on directory forces any new files created within that directory to have their group set to the same group that's set on the directory. It allows to overwrite group owner, but not permissions.
  • bsdgroups or grpid mount option enables setgit bit behaviour for all directories, even if setgit bit is not set.
  • ACL (access control lists) allows you to set per-user and per-group permissions for a directory or file. They are also inherited from parent directory when new file is created in that directory. However, ACL permissions are masked against traditional POSIX permissions, which are set by file creator and its umask, so you can't inherit more permissions than allowed by file creator.

All of these mechanisms except umask are linux-specific. See this question for some details about setgit bit and ACLs.

I guess non of these is a direct solution for you problem. What you can do:

  • Configure programs that create those files to create them with sufficient permissions. E.g., if they create them (or can be configured so) with permissions 0777, setting umask to 000 should be enough. See @KasunRathnayaka's answer.
  • If it's not an option, you can use bindfs tool with --create-with-perms and related options. It allows to mount a directory to another directory and overwrite permissions and user/group owners when files are created or modified. See manual page.

bash shell scripting: How to group argument list into N items?

See xargs and its -n option.

ls | xargs -n3 command

If your file names contain whitespace, you might need the -0 option and find with -print0 to list the files.



Related Topics



Leave a reply



Submit