建立一個新的 Mac 終端機命令來複製資料夾

建立一個新的 Mac 終端機命令來複製資料夾

如何建立一個新的終端命令來複製資料夾(這將始終是同一資料夾)及其內容到目前目錄(如果未提供路徑)或提供的路徑。

此命令應採用以下參數:

  1. 將內容複製到的新資料夾的名稱(即mkdir myNewFolder
  2. 要在其中建立新資料夾並將內容貼到的目錄的路徑。

我想最終得到這樣的結果:

$ createsite newFolderName ./Desktop/sites/

我真的不知道從哪裡開始。所以任何幫助都是值得讚賞的

答案1

如果您不想簡單地使用mkdir -p然後 a cp,那麼您也可以建立一個相當強大的函數來複製到由it wall always be the same folder給出的目錄名稱dirnm第一個論點(如果沒有給出第二個參數,則在當前工作目錄中)或者如果在參數中給出/destpath/dirnm表示路徑作為destpath第二個參數

在形成涉及的路徑時,您需要檢查(並刪除) destpath/dirnm'/'中的前導和尾隨字符,以及destpath/dirnm中的dirnm任何尾隨字符(它始終是同一資料夾destpath/dirnm -a` 選項,或根據需要)。失敗時,拋出錯誤並返回。以下是實現此類函數的一種方法:'/'destpath. You then attempt to createas given and on success copyto(with the

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 )
}

我會將該函數包含在您的~/.bashrc(或 ~/.profile)中,或者您可以在當前的 shell 中手動輸入/導出它。我也會建立一個方便的方法alias來減少聲明後的輸入,例如.bashrc

alias mdcp='mkdircp'

有了別名,用法就是:

mdcp dirnm [destpath (default: ./)]

要複製it wall always be the same folderdestpath/dirnm.如果您有任何疑問或需要進行一些調整,請告訴我。

答案2

我會使用一個函數。將這些行新增到您的~/.profile(或者,如果不在 OSX 上,請新增到您的~/.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
}

然後,打開一個新終端,您可以運行

createsite foo

這會將 的內容複製/path/to/source/folder到新建立的目錄中foo。請注意,這是一種非常簡單的方法,如果目錄存在或檔案將被覆蓋,則不會警告您。

或者,只需運行createsite它就會複製到當前目錄。

答案3

rsync 是一個可以幫助您的指令。它可以透過查看兩個資料夾之間的差異來有效地同步它們。它也可以透過網路運作。

rsync source destination

來源或目標都可以是表達式,例如pwd或 ${HOME};相對的,比如。 ;或固定,如 /tmp

有關更多信息,請閱讀精美的手冊頁:

man rsync

相關內容