Run a command as a different group, on demand

User & Group Management

Run a command as a different group, on demand

🧩 The Challenge

Ever needed to create a file or run a script that *absolutely* had to be owned by a specific group, not your default primary one? You `touch` it, then realize it’s got your primary group, and you’re stuck doing a `chown` or `chgrp` right after. I’ve wasted too many keystrokes on this dance, especially when directories don’t have sticky bits set just right.

💡 The Fix

You can skip that extra step entirely. There’s a simple command that lets you temporarily run *just one thing* as if your primary group was something else. Keeps your file permissions pristine without the cleanup.

sg webdev -c "touch /var/www/html/new_project/config.ini"

⚙️ Why It Works

The `sg` command temporarily changes your effective primary group to whatever you specify (e.g., `webdev`) *only* for the execution of the command provided with `-c`. Anything that command creates or interacts with will use that group, respecting your system’s `umask` and directory permissions.

🚀 Pro-Tip: For an entire shell session with a new primary group, check out `newgrp` instead.

Linux Tips & Tricks | © ngelinux.com | 10/27/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted