0

How can I create a new terminal command that copies a folder (this will always be the same folder) and it's contents to either the current directory (if no path is supplied) or to the path supplied.

This command should take the following parameters:

  1. Name of the new folder to copy the contents to (i.e. mkdir myNewFolder)
  2. The path of the directory in which to create the new folder and paste the contents into.

I'd like to end up with something like this:

$ createsite newFolderName ./Desktop/sites/

I don't really know where to start with this. So any help is appreciated

2
  • Let me get this straight.. You want to copy a folder to another path or you want to create a folder in a certain path?
    – Valinho
    Apr 20, 2015 at 14:05
  • I'd like to copy the folder to the current directory or, if a path is supplied I want to copy the folder to that path.
    – tkiddle
    Apr 20, 2015 at 14:06

3 Answers 3

1

If you don't simply want to use mkdir -p and then a cp, then you may as well create a fairly robust function to copy it wall always be the same folder to the directory name given by dirnm as the first argument (in the current working directory if no second argument given) or to /destpath/dirnm if a path representing destpath is given in argument as the second argument.

In forming the paths involved, you will need to check for (and remove) leading and trailing '/' characters in dirnm and any trailing '/' in destpath. You then attempt to createdestpath/dirnmas given and on success copyit wall always be the same foldertodestpath/dirnm(with the-a` option, or as desired). On failure, throw error and return. The following is one approach to such a function:

mkdircp ()
{
    srcdir="NameOfDirToCopy"    ## the name of the dir you always copy (w/full path)

    [ -d "$srcdir" ] || {       ## validate srcdir exists
        printf "error: source directory '%s' does not exist.\n" "$srcdir"
        return 1
    }

    [ -z $1 ] && {              ## validate that required dirnm given
        printf "usage: mdcp dirnm [destpath (default ./)]\n";
        return 1
    };

    ## trim leading/trailing '/' from dirnm
    [ ${1:0:1} == '/' ] && dirnm="${1:1}" || dirnm="$1"
    [ ${1:(-1)} == '/' ] && dirnm="${dirnm%/}"

    ## if destpath given, trim trailing '/' & set destdir
    if [ -n "$2" ]; then
        [ ${2:(-1)}x == '/x' ] && destpath="${2%/}" || destpath="$2"
        [ -n "$2" ] && destdir="${destpath}/${dirnm}"
    else
        destdir="./$dirnm"    ## default destdir in ./
    fi

    ## create destdir & validate or throw error
    [ -d "$destdir" ] || mkdir -p "$destdir"
    [ -d "$destdir" ] || {
        printf "error: unable to create destdir '%s'. (check permissions)\n" "$destdir"
        return 1
    }

    ## copy (-recursive -archive) "$srcdir" "$destdir"
    printf "copying:  %s -> %s\n" "$srcdir" "$destdir"
    # cp -a "$srcdir" "$destdir"    ## (uncomment for actual copy )
}

I would include the function in your ~/.bashrc (or ~/.profile) or you can manually enter/export it in your current shell. I would also create a convenient alias to cut down typing following the declaration in .bashrc like:

alias mdcp='mkdircp'

With the alias, the use is:

mdcp dirnm [destpath (default: ./)]

To copy it wall always be the same folder to destpath/dirnm. Let me know if you have any questions or need to adjust things a bit.

0

I would use a function. Add these lines to your ~/.profile (or, if not on OSX, to your ~/.bashrc):

createsite(){
    ## Change this to point to the folder you want to copy
    source="/path/to/source/folder"

    ## If no parameters were given, copy to the current directory
    if [ $# -eq 0 ];
    then    
        cp -rv "$source" .
    ## If an argument was given
    elif [ $# -eq 1 ]
    then
        ## Create the directory. The -p suppresses error messages
        ## in case the directory exists.
        mkdir "$1"
        ## Copy
        cp -rv "$source" "$1"/
    ## If more than one was given, something's wrong.
    else
        echo "Usage: $0 [target_directory]";
        exit 1;
    fi
}

Then, open a new terminal and you can run

createsite foo

Which will copy the contents of /path/to/source/folder into the newly created directory foo. Note that this is a very simplistic approach and won't warn you if the directory exists or if files will be overwritten.

Or, just run createsite and it will copy to the current directory.

0

rsync is a command that could help you. It can synchronize 2 folders efficiently by looking at differences between them. It also works over the network.

rsync source destination

Either source or destination can be expressions, like pwd or ${HOME}; relative, like . ; or fixed, like /tmp

For more info read the fine manual pages:

man rsync

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .