
폴더(항상 동일한 폴더임)와 그 내용을 현재 디렉터리(경로가 제공되지 않은 경우) 또는 제공된 경로에 복사하는 새 터미널 명령을 만들려면 어떻게 해야 합니까?
이 명령은 다음 매개변수를 사용해야 합니다.
- 내용을 복사할 새 폴더의 이름(예:
mkdir myNewFolder
) - 새 폴더를 만들고 내용을 붙여넣을 디렉터리의 경로입니다.
나는 다음과 같이 끝내고 싶습니다 :
$ createsite newFolderName ./Desktop/sites/
어디서부터 시작해야할지 모르겠습니다. 그래서 어떤 도움이라도 감사하겠습니다
답변1
mkdir -p
단순히 와 a를 사용하고 싶지 않다면 다음과 같이 주어진 디렉토리 이름에 cp
복사하는 상당히 강력한 함수를 생성할 수도 있습니다.it wall always be the same folder
dirnm
첫 번째 인수(두 번째 인수가 제공되지 않은 경우 현재 작업 디렉토리에서) 또는 /destpath/dirnm
나타내는 경로가 destpath
인수로 제공되는 경우두 번째 인수.
'/'
관련된 경로를 형성할 때 선행 및 후행 문자를 확인(및 제거)해야 하며 destpath/dirnm 의 dirnm
후행은 항상 동일한 폴더 destpath/dirnm -a` 옵션이거나 원하는 대로 됩니다. 실패하면 오류를 발생시키고 반환합니다. 다음은 이러한 기능에 대한 한 가지 접근 방식입니다.'/'
destpath. You then attempt to create
as given and on success copy
to
(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 )
}
귀하의 (또는 ~/.profile)에 함수를 포함시키 ~/.bashrc
거나 현재 쉘에 수동으로 입력/내보낼 수 있습니다. 또한 다음과 같은 alias
선언에 따라 입력을 줄이는 편리한 방법을 만들 것입니다 .bashrc
.
alias mdcp='mkdircp'
별칭의 용도는 다음과 같습니다.
mdcp dirnm [destpath (default: ./)]
it wall always be the same folder
에 복사하려면 destpath/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는 도움이 될 수 있는 명령입니다. 2개의 폴더 사이의 차이점을 확인하여 효율적으로 동기화할 수 있습니다. 네트워크를 통해서도 작동합니다.
rsync source destination
pwd
소스 또는 대상은 또는 ${HOME}과 같은 표현식일 수 있습니다 . 친척 같은. ; 또는 고정됨(예: /tmp)
더 많은 정보를 원하시면 훌륭한 매뉴얼 페이지를 읽어보세요:
man rsync