In a previous post, we shared a Bash shell script for creating local user accounts in GNU/Linux. As is often the case, the process of drafting this script yielded a number of useful code snippets, one of which we'll detail in this post.
By default, the useradd
command will not create the directory structure that GUI users of GNU/Linux distributions may be familiar with (e.g.: Desktop
, Music
, and the like). While this isn't necessarily a deal-breaker, there may be use cases where this structure is desirable.
To create the default directory structure, we need to call on the xdg-user-dirs
tool (for reference: https://wiki.archlinux.org/index.php/XDG_user_directories). Here's one way to do so:
#!/usr/bin/env bash
username='sjobs'
create_default_dirs () {
if [[ -n $(command -v xdg-user-dirs-update) ]]
then
su ${username} -c xdg-user-dirs-update
$username -i xdg-user-dirs-update
fi
}
In our snippet, we ask Bash
to print the path of the xdg-users-dirs-update
command (command -v
), checking that the length of this check is not zero (0). Once confirmed, we then run the xdg-...
command as the user (sjobs
, in this case) of the account our script created (su ${username} -c
), such that the default directory structure is placed in this user's home
folder.
Cheers.