-
Notifications
You must be signed in to change notification settings - Fork 0
/
divide_into_blocks
executable file
·191 lines (169 loc) · 6.71 KB
/
divide_into_blocks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env bash
#!/bin/bash
#
# @author Zeus Intuivo <[email protected]>
#
THISSCRIPTNAME=`basename "$0"`
if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]]; then # empty value
echo -e "\\033[38;5;123m
\\033[38;5;227mFrom one folder to another using batch in subfolders, you don't get stopped while the computer 'builds' a list
using rsync which is supposed to be faster at the time I did this (2017)
Only tested on Linux Ubuntu now 14,15, and 16
\\033[0m${THISSCRIPTNAME} \\033[38;5;227mfrom_path \\033[38;5;123mto_path \\033[38;5;1m number_per_block \\033[0m
${THISSCRIPTNAME} bash_intuivo_cli2 new_path 3
+ This will copy from \"bash_intuivo_cli\" to \"new_path\" --it will create it if not, into amounts of '3' per folder groups named with numbers 1,2,3,4,5,6 until done
+ It will make a backup folder \"bash_intuivo_cli/original\" just in case
"
exit 0
fi
function load_execute_command {
EXECOMCLI=$(wget --quiet --no-check-certificate https://raw.githubusercontent.com/zeusintuivo/execute_command_intuivo_cli/master/execute_command -O - 2>/dev/null ) # suppress only curl download messages, but keep curl output for variable
eval """${EXECOMCLI}"""
( ( ! command -v passed >/dev/null 2>&1; ) && echo -e "\n \n ERROR! Loading execute_command \n \n " && exit 130; )
} # end function load_execute_command
load_execute_command
function slashes_to_newlines {
sed -e 's/'"\/"'/'"\n"'/g' #splits slashed into enters with newlines divides slashes
}
# TEST: echo "/_/work/savedo/projects/savedo-cms-wagon-de/app/assets/fonts/bootstrap/" | slashes_to_newlines
# EXPECTATION:
# _
# work
# savedo
# projects
# savedo-cms-wagon-de
# app
# assets
# fonts
# bootstrap
#
function remove_double_slashes { #removes double slashes in path and converts ./ to PWD
# usage
# 1. FOLDER=$(remove_double_slashes <<< "$@")
# 2. remove_double_slashes ".//_/work/savedo/projects/savedo-cms-wagon-de///app/assets///fonts/bootstrap///"
local incoming_string="${@}"
local PARTS=$(slashes_to_newlines <<< "${incoming_string}")
local ONE_PART
local result=""
local COUNTER=0
local using_string=""
while read -r ONE_PART; do
# for ONE_PART in ${PARTS}; do
{
if [ ! -z "${ONE_PART}" ] ; then # if not empty
{
using_string="${ONE_PART}"
if [ $COUNTER -eq 0 ] && ( [ "${using_string}" == "." ] || [ -d "${using_string}" ] ) ; then
{
result="$(pwd)"
[ -d "${using_string}" ] && result="$(pwd)/${using_string}"
} else {
if [ $COUNTER -eq 0 ] ; then
{
result="${using_string}"
[ -d "${using_string}" ] && [[ ${incoming_string:0:1} != "/" ]] && result="$(pwd)/${using_string}"
} else {
result="${result}/${using_string}"
}
fi
}
fi
(( COUNTER++ ))
}
fi
}
done <<< "${PARTS}"
if [[ ${incoming_string:0:1} == "/" ]] ; then # get first char from string
{
[[ ${result:0:1} != "/" ]] && result="/${result}"
}
fi
echo "${result}"
return 0
}
# TEST: remove_double_slashes "/_/work/savedo/projects/savedo-cms-wagon-de///app/assets///fonts/bootstrap///"
# EXPECTATION: /_/work/savedo/projects/savedo-cms-wagon-de/app/assets/fonts/bootstrap
# TEST: remove_double_slashes "./work/savedo/projects/savedo-cms-wagon-de///app/assets///fonts/bootstrap///"
# EXPECTATION: ${PWD}/work/savedo/projects/savedo-cms-wagon-de/app/assets/fonts/bootstrap
function remove_dot_slash_from_begining {
sed 's/\.\///g'
}
function remove_lonely_dot {
sed '/^\.$/d'
}
function remove_lonely_double_dots {
sed '/^\.\.$/d'
}
function get_all_files {
local folder="${1}"
[ "${folder}" == "." ] && folder=$(pwd)
local all_files=$(find "${folder}" -maxdepth 1 | remove_dot_slash_from_begining | remove_lonely_dot | remove_lonely_double_dots | sed s@"${folder}\/"@@g)
#anounce Found:"${all_files}"
#exit
echo "${all_files}"
} #end get_all_files
function remove_last_slash {
sed 's/\/$//g' # remove last slash
}
function check_path {
local path=$(echo "${2}" | remove_last_slash)
local cwd=$(pwd)
local result
# Find paths
if [ -e "${2}" ] ; then
result="${2}"
elif [ -e "${cwd}/${2}" ] ; then
result="${cwd}/${2}"
else
echo -e "\\033[38;5;1m ${1} \\033[01;33m PATH Not found: \\033[38;5;1m ${2}\\033[0m"
return 1
fi
echo "${result}"
return 0
} # end check_path
function divide_using_rsync() {
local from_path=$(remove_double_slashes "${1}")
local base_path=$(remove_double_slashes "${2}")
local backup_path="${from_path}/original"
local GOAL=${3}
local to_path
local all_files=$(get_all_files "${from_path}")
local counter=1
local blocks=1
anounce Copy Batch
anounce Found files:"${all_files}"
anounce Found from_path:"${from_path}"
anounce Found to_path:"${base_path}/#${GOAL}#"
anounce Found base_path:"${base_path}"
anounce Found backup_path:"${backup_path}"
anounce Found GOAL:"${GOAL}"
[ ! -d "${backup_path}/" ] && mkdir -p "${backup_path}/"
while read -r one_item; do
{
if [ ! -z "${one_item}" ] && [[ "${from_path}/${one_item}" != "${from_path}/${from_path}" ]]; then
{
to_path="${base_path}/${blocks}"
[ ! -d "${to_path}" ] && mkdir -p "${to_path}"
anounce base_path:"${to_path}"
local filename=$(escape_filename <<< "${one_item}")
# if source file is a directory and target file directory does not exists then create
[ -d "${from_path}/${filename}/" ] && [ ! -d "${to_path}/${filename}/" ] && mkdir -p "${to_path}/${filename}" # make directory if it does not exist
# [ -d "${from_path}/${filename}/" ] && [ ! -d "${to_path}" ] && mkdir -p "${to_path}"
anounce "\\033[38;5;30m${filename} \\033[38;5;242mmoving to \\033[38;5;30m${to_path}/"
rsync -av "${from_path}/${filename}" "${to_path}/"
rsync -av "${from_path}/${filename}" "${backup_path}/"
# DEBUG anounce rsync -av "${from_path}/${filename}" "${to_path}/"
# DEBUG anounce rm "${from_path}/${filename}"
[ -e "${to_path}/${filename}" ] && [ -e "${backup_path}/${filename}" ] && anounce_command rm "${from_path}/${filename}"
# DEBUG echo "$GOAL...$blocks:$counter"
(( counter++ ))
if [ $counter -gt $GOAL ] ; then
counter=1
(( blocks++ ))
fi
}
fi
}
done <<< "${all_files}"
} # end divide_using_rsync
divide_using_rsync "${1}" "${2}" "${3}"