The adduser
command can run a site-specific script to do any setup like removing files. As long as it is acceptable to start with a full copy and then delete some files afterwards, then this approach could work for you.
From the adduser(8) man page:
If the file /usr/local/sbin/adduser.local
exists, it will be executed after the user account has been set
up in order to do any local setup. The arguments passed to
adduser.local
are:
username uid gid home-directory
So all you need to do is write a script that takes four parameters and use it remove any files you need. Save it as /usr/local/sbin/adduser.local
and make sure it's marked executable (chmod a+x
).
Here's something to get you started:
#!/bin/bash
## Site-specific setup for newly-created users.
## adduser(8) will call this script after setting up a new user.
set -euo pipefail
if [[ "$#" != 4 ]]; then
echo "usage: $0 username uid gid home" > /dev/stderr
fi
NEW_USERNAME="${1:?}"
NEW_UID="${2:?}"
NEW_GID="${3:?}"
NEW_HOME="${4:?}"
# The groups command outputs a space-separated list of group names
IFS=' '
for group in $(groups "${NEW_USERNAME}"); do
case "${group}" in
a)
[[ "${VERBOSE}" > 0 ]] && echo Removing file for a
rm "${NEW_HOME}/not_for_a.txt"
;;
b)
[[ "${VERBOSE}" > 0 ]] && echo Removing dir for b
rm -r "${NEW_HOME}/not_for_b/"
;;
*)
[[ "${VERBOSE}" > 1 ]] && echo No special setup required for $group
;;
esac
done
The interesting part, which you'll want to edit, are the lines that look like this one:
a)
[[ "${VERBOSE}" > 0 ]] && echo Removing file for a
rm "${NEW_HOME}/not_for_a.txt"
;;
You can fill in the actual group name and behaviour you'd like to see instead of a)
and rm not_for_a.txt
.