私は、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には、非常に堅固な動作パターンが必要です。CPは、コピーしているアイテムが宛先パスにドロップされ、宛先自体(別名、ソースファイル/フォルダーの名前が変更されたコピー)ではないと想定します。意味:
- 「CP / a / b / c / d」は、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モード(--strictスイッチで使用可能)では、コピーされるすべてのファイル/フォルダーが存在する必要があります。存在しない場合、コピーは行われません。リラックスモード(--relaxed)では、リストした項目の少なくとも1つが存在する場合、コピーが続行されます。リラックスモードがデフォルトです。スイッチを使用して一時的にモードを変更することも、スクリプトの先頭に変数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