0

I need to write a backup script. I need to be able to run the script from any computer and have it pull the username and date as the directory name. Each computer has an admin account and a user account. How can I pull the user account from a script and use it to name the backed up directory?

3

folder name example: joe--2014-01-31

folder_name="${USER}--$(date +%Y-%m-%d)"
mkdir "$folder_name"
  • wow. Your answer bears uncanny similarity to mine. – tylerl Jan 31 '14 at 2:31
  • @tylerl That happens if the solution is obvious and several people write an answer simultaneously... – Hauke Laging Jan 31 '14 at 2:38
  • 1
    Note that $USER is not always set and not always to the value you'd wish. See also $(logname), $(id -run) and $(id -un) (and $(whoami)) depending on which user you actually mean. – Stéphane Chazelas Jan 31 '14 at 7:16
2

The current logged in user is typically stored in the USER environment variable. The current date is easily retrieved using the date command. So, just put them together.

echo "$USER--$(date +%Y-%m-%d)"
  • Can I use the UID and get the same effect? – user58197 Jan 31 '14 at 2:48
  • UID is distinct from the username, as it is a numerical designation. – DopeGhoti Jan 31 '14 at 2:52
  • If I know that each user will be a particular, in this case the same UID on a different machine, can I use it to find the name of the user and use that name in the script? – user58197 Jan 31 '14 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.