Linux Bash Worklet to Copy Files to Users' Home Folder

Linux Worklet: Copy Files to User Home Directories

This article provides Bash scripts designed for Automox Worklets to automate the distribution of files to all "normal" (non-system) user home directories on Linux endpoints.

Overview

The scripts perform the following actions:

  1. Identify all users with a UID of 500 or greater (standard for human users).

  2. Verify the existence of the user's home directory.

  3. Copy the specified file(s) to the directory.

  4. Correct the file ownership to match the specific user and their primary group.


Script 1: Single File Distribution

Use this script when distributing a single configuration file, script, or asset.

Requirements

  • UHOME: Set this to the base home directory (default is /home).

  • FILE: The full source path of the file on the local machine (e.g., /tmp/config.conf).

  • FILENAME: The name of the file used for permission assignment.

Bash

 
#!/bin/bash

# Base home folder
UHOME="/home"

# Path to file to be copied
FILE="/tmp/test_file"

# Filename variable for chown command
FILENAME="test_file"

# Get a list of all normal users (UID >= 500)
_USERS="$(awk -F':' '{ if ( $3 >=500 ) print $1 }' /etc/passwd)"

# Iterate through user list and copy file
for u in $_USERS
do
    _dir="${UHOME}/${u}"
    # Check that the directory exists
    if [ -d "$_dir" ]
    then
        /bin/cp "$FILE" "$_dir"
        # Set ownership to the specific user and their group
        chown $(id -un $u):$(id -gn $u) "$_dir/${FILENAME}"
    fi
done

Script 2: Multiple File Distribution

Use this script to distribute several files simultaneously.

Requirements

  • UHOME: Set this to the base home directory (default is /home).

  • _FILES: A space-separated list of full paths to the source files.

Bash

 
#!/bin/bash

# Base home folder
UHOME="/home"

# Path to files to be copied (Space separated)
_FILES="/tmp/test_file /tmp/test_file2"

# Get a list of all normal users (UID >= 500)
_USERS="$(awk -F':' '{ if ( $3 >=500 ) print $1 }' /etc/passwd)"

# Iterate through user list and file list and copy files
for u in $_USERS
do
    for f in $_FILES
    do
        _dir="${UHOME}/${u}"
        # Check that the directory exists
        if [ -d "$_dir" ]
        then
            /bin/cp -f "$f" "$_dir"
            file_name=`basename ${f}`
            # Set ownership to the specific user and their group
            chown $(id -un $u):$(id -gn $u) "$_dir/${file_name}"
        fi
    done
done

Technical Notes

User Filtering

Both scripts use the following command to filter accounts: _USERS="$(awk -F':' '{ if ( $3 >=500 ) print $1 }' /etc/passwd)"

  • Why UID 500? Most Linux distributions reserve UIDs 0–499 for system services and root. Starting at 500 (or 1000 on newer systems) ensures the script only affects actual user accounts.

  • Permissions: These scripts must be run with root or sudo privileges to modify files within other users' home directories and change file ownership (chown).

Best Practices

  • Upload Files via Automox: Before running the execution script, ensure the files you intend to copy are uploaded as Payloads in the Automox Worklet interface so they are available in the local /tmp or working directory.

  • Testing: Always test on a single non-production device to verify directory paths and ownership results before deploying globally.

Was this article helpful?
0 out of 0 found this helpful