나는 정확히 이것을하기위한 CP (참고 대문자)라는 cp에 대한 지원 스크립트를 작성했습니다. 스크립트는 경로에 넣은 경로에서 오류를 확인하고 (대상인 마지막 경로는 제외) 모든 것이 정상이면 복사를 시작하기 전에 대상 경로를 만들기 위해 mkdir -p 단계를 수행합니다. 이 시점에서 일반 cp 유틸리티가 대신 사용하고 CP와 함께 사용하는 모든 스위치 (예 : -r, -p, -rpL은 cp로 직접 파이프 됨) 내 스크립트를 사용하기 전에 몇 가지 이해해야 할 것이 있습니다.
- 여기에있는 모든 정보는 CP --help를 수행하여 액세스 할 수 있습니다. CP --help-all include의 cp 스위치.
- 일반 cp는 대상 경로를 찾지 못하면 복사를 수행하지 않습니다. CP를 사용한 오타 용 안전망이 없습니다. 목적지가 만들어 지므로 / usrr / share / icons 또는 / usr / share / icon로 목적지의 철자를 잘못 입력하면 바로 이것이 만들어 질 것입니다.
- 일반 cp는 기존 경로에서 동작을 모델링하는 경향이 있습니다. cp / a / b / c / d는 d의 존재 여부에 따라 다릅니다. d가 기존 폴더 인 경우 cp는 b를 복사하여 / c / d / b를 만듭니다. d가 존재하지 않으면 b는 c로 복사되고 d로 이름이 바뀝니다. d가 존재하지만 파일이고 b가 파일 인 경우 b의 사본으로 덮어 씁니다. c가 존재하지 않으면 cp는 복사를 수행하지 않고 종료합니다.
CP는 기존 경로에서 신호를 얻는 데 사치가 없으므로 매우 확실한 행동 패턴을 가져야합니다. CP는 복사중인 항목이 대상 경로에서 삭제되고 대상 자체가 아니라고 가정합니다 (일명 소스 파일 / 폴더의 이름이 바뀐 사본). 의미:
- d가 폴더 인 경우 "CP / a / b / c / d"는 / c / d / b가됩니다.
- / c / b의 b가 폴더 인 경우 "CP / a / b / c / b"는 / c / b / b가됩니다.
- b와 d가 모두 파일 인 경우 : CP / a / b / c / d는 / c / d가됩니다 (여기서 d는 b의 사본 임). 동일한 상황에서 CP / a / b / c / b와 동일합니다.
이 기본 CP 동작은 "--rename"스위치를 사용하여 변경할 수 있습니다. 이 경우에는
- "CP --rename / a / b / c / d"는 b를 / c에 복사하고 사본의 이름을 d로 바꾸는 중입니다.
몇 가지 참고 사항 : cp와 마찬가지로 CP는 마지막 경로가 대상으로 가정하여 한 번에 여러 항목을 복사 할 수 있습니다. 따옴표를 사용하는 한 공백이있는 경로를 처리 할 수도 있습니다.
CP는 복사 한 경로를 확인하고 복사하기 전에 경로가 존재하는지 확인합니다. 엄격 모드 (--strict 스위치를 통해 사용 가능)에서는 복사중인 모든 파일 / 폴더가 존재해야합니다. 완화 모드 (-휴식)에서 나열된 항목 중 하나 이상이 존재하면 복사가 계속됩니다. 완화 모드가 기본값이며 스위치를 통해 모드를 일시적으로 변경하거나 스크립트 시작 부분에 easy_going 변수를 설정하여 영구적으로 모드를 변경할 수 있습니다.
설치 방법은 다음과 같습니다.
루트가 아닌 터미널에서 다음을 수행하십시오.
sudo echo > /usr/bin/CP; sudo chmod +x /usr/bin/CP; sudo touch /usr/bin/CP
gedit admin:///usr/bin/CP
gedit에서 CP 유틸리티를 붙여넣고 저장하십시오.
#!/bin/bash
#Regular cp works with the assumption that the destination path exists and if it doesn't, it will verify that it's parent directory does.
#eg: cp /a/b /c/d will give /c/d/b if folder path /c/d already exists but will give /c/d (where d is renamed copy of b) if /c/d doesn't exists but /c does.
#CP works differently, provided that d in /c/d isn't an existing file, it assumes that you're copying item into a folder path called /c/d and will create it if it doesn't exist. so CP /a/b /c/d will always give /c/d/b unless d is an existing file. If you put the --rename switch, it will assume that you're copying into /c and renaming the singl item you're copying from b to d at the destination. Again, if /c doesn't exist, it will be created. So CP --rename /a/b /c/d will give a /c/d and if there already a folder called /c/d, contents of b will be merged into d.
#cp+ $source $destination
#mkdir -p /foo/bar && cp myfile "$_"
err=0 # error count
i=0 #item counter, doesn't include destination (starts at 1, ex. item1, item2 etc)
m=0 #cp switch counter (starts at 1, switch 1, switch2, etc)
n=1 # argument counter (aka the arguments inputed into script, those include both switches and items, aka: $1 $2 $3 $4 $5)
count_s=0
count_i=0
easy_going=true #determines how you deal with bad pathes in your copy, true will allow copy to continue provided one of the items being copied exists, false will exit script for one bad path. this setting can also be changed via the custom switches: --strict and --not-strict
verbal="-v"
help="===============================================================================\
\n CREATIVE COPY SCRIPT (CP) -- written by thebunnyrules\
\n===============================================================================\n
\n This script (CP, note capital letters) is intended to supplement \
\n your system's regular cp command (note uncapped letters). \n
\n Script's function is to check if the destination path exists \
\n before starting the copy. If it doesn't it will be created.\n
\n To make this happen, CP assumes that the item you're copying is \
\n being dropped in the destination path and is not the destination\
\n itself (aka, a renamed copy of the source file/folder). Meaning:\n
\n * \"CP /a/b /c/d\" will result in /c/d/b \
\n * even if you write \"CP /a/b /c/b\", CP will create the path /a/b, \
\n resulting in /c/b/b. \n
\n Of course, if /c/b or /c/d are existing files and /a/b is also a\
\n file, the existing destination file will simply be overwritten. \
\n This behavior can be changed with the \"--rename\" switch. In this\
\n case, it's assumed that \"CP --rename /a/b /c/d\" is copying b into /c \
\n and renaming the copy to d.\n
\n===============================================================================\
\n CP specific help: Switches and their Usages \
\n===============================================================================\n
\
\n --rename\tSee above. Ignored if copying more than one item. \n
\n --quiet\tCP is verbose by default. This quiets it.\n
\n --strict\tIf one+ of your files was not found, CP exits if\
\n\t\tyou use --rename switch with multiple items, CP \
\n\t\texits.\n
\n --relaxed\tIgnores bad paths unless they're all bad but warns\
\n\t\tyou about them. Ignores in-appropriate rename switch\
\n\t\twithout exiting. This is default behavior. You can \
\n\t\tmake strict the default behavior by editing the \
\n\t\tCP script and setting: \n
\n\t\teasy_going=false.\n
\n --help-all\tShows help specific to cp (in addition to CP)."
cp_hlp="\n\nRegular cp command's switches will still work when using CP.\
\nHere is the help out of the original cp command... \
\n\n===============================================================================\
\n cp specific help: \
\n===============================================================================\n"
outro1="\n******************************************************************************\
\n******************************************************************************\
\n******************************************************************************\
\n USE THIS SCRIPT WITH CARE, TYPOS WILL GIVE YOU PROBLEMS...\
\n******************************************************************************\
\n******************************* HIT q TO EXIT ********************************\
\n******************************************************************************"
#count and classify arguments that were inputed into script, output help message if needed
while true; do
eval input="\$$n"
in_=${input::1}
if [ -z "$input" -a $n = 1 ]; then input="--help"; fi
if [ "$input" = "-h" -o "$input" = "--help" -o "$input" = "-?" -o "$input" = "--help-all" ]; then
if [ "$input" = "--help-all" ]; then
echo -e "$help"$cp_hlp > /tmp/cp.hlp
cp --help >> /tmp/cp.hlp
echo -e "$outro1" >> /tmp/cp.hlp
cat /tmp/cp.hlp|less
cat /tmp/cp.hlp
rm /tmp/cp.hlp
else
echo -e "$help" "$outro1"|less
echo -e "$help" "$outro1"
fi
exit
fi
if [ -z "$input" ]; then
count_i=$(expr $count_i - 1 ) # remember, last item is destination and it's not included in cound
break
elif [ "$in_" = "-" ]; then
count_s=$(expr $count_s + 1 )
else
count_i=$(expr $count_i + 1 )
fi
n=$(expr $n + 1)
done
#error condition: no items to copy or no destination
if [ $count_i -lt 0 ]; then
echo "Error: You haven't listed any items for copying. Exiting." # you didn't put any items for copying
elif [ $count_i -lt 1 ]; then
echo "Error: Copying usually involves a destination. Exiting." # you put one item and no destination
fi
#reset the counter and grab content of arguments, aka: switches and item paths
n=1
while true; do
eval input="\$$n" #input=$1,$2,$3,etc...
in_=${input::1} #first letter of $input
if [ "$in_" = "-" ]; then
if [ "$input" = "--rename" ]; then
rename=true #my custom switches
elif [ "$input" = "--strict" ]; then
easy_going=false #exit script if even one of the non-destinations item is not found
elif [ "$input" = "--relaxed" ]; then
easy_going=true #continue script if at least one of the non-destination items is found
elif [ "$input" = "--quiet" ]; then
verbal=""
else
#m=$(expr $m + 1);eval switch$m="$input" #input is a switch, if it's not one of the above, assume it belongs to cp.
switch_list="$switch_list \"$input\""
fi
elif ! [ -z "$input" ]; then #if it's not a switch and input is not empty, it's a path
i=$(expr $i + 1)
if [ ! -f "$input" -a ! -d "$input" -a "$i" -le "$count_i" ]; then
err=$(expr $err + 1 ); error_list="$error_list\npath does not exit: \"b\""
else
if [ "$i" -le "$count_i" ]; then
eval item$i="$input"
item_list="$item_list \"$input\""
else
destination="$input" #destination is last items entered
fi
fi
else
i=0
m=0
n=1
break
fi
n=$(expr $n + 1)
done
#error condition: some or all item(s) being copied don't exist. easy_going: continue if at least one item exists, warn about rest, not easy_going: exit.
#echo "err=$err count_i=$count_i"
if [ "$easy_going" != true -a $err -gt 0 -a $err != $count_i ]; then
echo "Some of the paths you entered are incorrect. Script is running in strict mode and will therefore exit."
echo -e "Bad Paths: $err $error_list"
exit
fi
if [ $err = $count_i ]; then
echo "ALL THE PATHS you have entered are incorrect! Exiting."
echo -e "Bad Paths: $err $error_list"
fi
#one item to one destination:
#------------------------------
#assumes that destination is folder, it does't exist, it will create it. (so copying /a/b/c/d/firefox to /e/f/firefox will result in /e/f/firefox/firefox
#if -rename switch is given, will assume that the top element of destination path is the new name for the the item being given.
#multi-item to single destination:
#------------------------------
#assumes destination is a folder, gives error if it exists and it's a file. -rename switch will be ignored.
#ERROR CONDITIONS:
# - multiple items being sent to a destination and it's a file.
# - if -rename switch was given and multiple items are being copied, rename switch will be ignored (easy_going). if not easy_going, exit.
# - rename option but source is folder, destination is file, exit.
# - rename option but source is file and destination is folder. easy_going: option ignored.
if [ -f "$destination" ]; then
if [ $count_i -gt 1 ]; then
echo "Error: You've selected a single file as a destination and are copying multiple items to it. Exiting."; exit
elif [ -d "$item1" ]; then
echo "Error: Your destination is a file but your source is a folder. Exiting."; exit
fi
fi
if [ "$rename" = true ]; then
if [ $count_i -gt 1 ]; then
if [ $easy_going = true ]; then
echo "Warning: you choose the rename option but are copying multiple items. Ignoring Rename option. Continuing."
else
echo "Error: you choose the rename option but are copying multiple items. Script running in strict mode. Exiting."; exit
fi
elif [ -d "$destination" -a -f "$item1" ]; then
echo -n "Warning: you choose the rename option but source is a file and destination is a folder with the same name. "
if [ $easy_going = true ]; then
echo "Ignoring Rename option. Continuing."
else
echo "Script running in strict mode. Exiting."; exit
fi
else
dest_jr=$(dirname "$destination")
if [ -d "$destination" ]; then item_list="$item1/*";fi
mkdir -p "$dest_jr"
fi
else
mkdir -p "$destination"
fi
eval cp $switch_list $verbal $item_list "$destination"
cp_err="$?"
if [ "$cp_err" != 0 ]; then
echo -e "Something went wrong with the copy operation. \nExit Status: $cp_err"
else
echo "Copy operation exited with no errors."
fi
exit