New Folder Script

jjimenez10
New Contributor II

Hello!

Very new at writing scripts and would like some help. Trying to create a script so I can use a policy to create a folder in specific location in dropbox.

For example: Users/userprofile/dropbox/username/"new folder"

I tried doing this through Composer and building it as a DMG with the right folder location and selecting the FEU in the policies. When testing in my computer, it shows up just fine, but when testing other computers the folder does not appear. So now I'm trying to do this through a script and see if that would do it. a1a5def508994659b91a4c62d75e347a
(package was created in DMG but deleted the file)

8 REPLIES 8

Hugonaut
Valued Contributor II

i believe you want to use the 'mkdir' command...

ie;

mkdir Users/userprofile/dropbox/username/FOLDERNAMEHERE

via terminal - if you do a 'man mkdir' here is what you get

MKDIR(1) BSD General Commands Manual MKDIR(1) NAME mkdir -- make directories SYNOPSIS mkdir [-pv] [-m mode] directory_name ... DESCRIPTION The mkdir utility creates the directories named as operands, in the order specified, using mode rwxrwxrwx (0777) as modi- fied by the current umask(2). The options are as follows: -m mode Set the file permission bits of the final created directory to the specified mode. The mode argument can be in any of the formats specified to the chmod(1) command. If a symbolic mode is specified, the operation characters +'' and-'' are interpreted relative to an initial mode of ``a=rwx''. -p Create intermediate directories as required. If this option is not specified, the full path prefix of each oper- and must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists. Intermediate directories are created with permission bits of rwxrwxrwx (0777) as modified by the current umask, plus write and search permission for the owner. -v Be verbose when creating directories, listing them as they are created. The user must have write permission in the parent directory. DIAGNOSTICS The mkdir utility exits 0 on success, and >0 if an error occurs. SEE ALSO rmdir(1) COMPATIBILITY The -v option is non-standard and its use in scripts is not recommended.
________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

mickgrant
Contributor III

I'm also kinda new to scripting so i'm trying to stretch myself by trying to write scripts to complete jobs people ask about on here
give this one a try and see if it does what you need - seems to work for me

#!/bin/bash
##############################################################################
##  Script for creating nested directory structure                                                                        ##
##  Script created by Mickgrant                                                                           ##
##  Date Created - 5/4/19                                                                                 ##
##  Date Updated - -------                                                                                                                                    ##
## NO liability accepted for improper use of this script or damage caused                                                 ##
##############################################################################

# Get username(short) of current logged in user
USERNAME=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')

# get the username(long) of current logged in user
FULLNAME=$(dscl . -read "/Users/$(who am i | awk '{print $1}')" RealName | sed -n 's/^ //g;2p')

### Check if a directory does not exist ###
if [[ ! -d "Users/"$USERNAME"/dropbox/"$FULLNAME"/FOLDERNAMEHERE" ]] 
then
    echo "Directory ~/Users/"$USERNAME"/dropbox/"$FULLNAME"/FOLDERNAMEHERE DOES NOT exists - CREATING NOW." 
    #create folder structure
    mkdir -p Users/"$USERNAME"/dropbox/"$FULLNAME"/FOLDERNAMEHERE
    # Writes in logs when folders created
    logger "folder structure created"
else
    echo "Directory Users/"$USERNAME"/dropbox/"$FULLNAME"/FOLDERNAMEHERE already exists"
    # Writes in logs that folders already exist
    logger "Directory already exists"
fi

exit 0

jjimenez10
New Contributor II

Cool, thank you so much!! I'll give those a try.

ryan_ball
Valued Contributor

@jjimenez10 You are going to need the leading / in all references to the "Users" directory like so:

if [[ ! -d "/Users/$USERNAME/dropbox/$FULLNAME/FOLDERNAMEHERE" ]]
# AND
mkdir -p "/Users/$USERNAME/dropbox/$FULLNAME/FOLDERNAMEHERE"
# and anywhere else "Users" is mentioned, you should replace that with "/Users"

ryan_ball
Valued Contributor

I would just do this:

#!/bin/bash

loggedInUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
realName=$(/usr/bin/dscl . read "/Users/$loggedInUser" RealName | tail -n 1 | /usr/bin/xargs)
folder="This is the name of your folder"

mkdir -p "/Users/$loggedInUser/Dropbox/$realName/$folder"

mkdir -p will not complain if the directory exists already, so the logic to determine if it exists or not is just fluff.

jjimenez10
New Contributor II

cool, I'll be doing some testing. Thanks again, you guys are awesome!

mickgrant
Contributor III

@ryan.ball thanks for catching the leading / i should have caught that

mkdir -p will not complain if the directory exists already

don't know why your ragging on my mkdir -p usage when its the same command your using.

so the logic to determine if it exists or not is just fluff.

it might not be python, but it DOES works...

ryan_ball
Valued Contributor

@mickgrant I'm not ragging on you, I just wanted to make sure that the person who asked the question had something that was fully functional and I prefer to shorten all my scripts to the lowest number of lines. I enjoy helping people out on this forum, so I will continue to do so.

By the way, another portion of your script that might cause some issues is this one:

# get the username(long) of current logged in user
FULLNAME=$(dscl . -read "/Users/$(who am i | awk '{print $1}')" RealName | sed -n 's/^ //g;2p')

That will not do what you want. That is looking for the real name of the user who is actually running the script, not the logged in user. When running this script with Jamf Pro, the user who is running the script is root, not the logged in user.