-
Notifications
You must be signed in to change notification settings - Fork 0
/
loeschen_seo_nur_datei
executable file
·221 lines (174 loc) · 9.07 KB
/
loeschen_seo_nur_datei
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env bash
#
# @author Zeus Intuivo <[email protected]>
#
# CURRENT SCRIPT EXECUTING
THISSCRIPTNAME=`basename "$0"`
if [ -z "$1" ] ;
then
echo "
Missing 1st argument
Sample Usage: - expects one arguments *required
${THISSCRIPTNAME} filenames-terms-to-find
sample:
${THISSCRIPTNAME} TREEHOUSE.jpg CAT.jpg google.shit.js
term-to-find - is in concept for the filename or filenameSSS like TREEHOUSE.jpg and CAT.jpg and google.shit.js
• Remove references in all files .... it will look for the term TREEHOUSE.jpg and replace with '' <---- empty string ...aka erase
• Remove file .... it will remove the filename TREEHOUSE.jpg
• Remove references in all files .... it will look for the term CAT.jpg and replace with '' <---- empty string ...aka erase
• Remove file .... it will remove the filename CAT.jpg
• Remove references in all files .... it will look for the term google.shit.js and replace with '' <---- empty string ...aka erase
• Remove file .... it will remove the filename google.shit.js
"
exit
fi
fail_if_file_already_exists(){
if [ -f "${1}" ] ; then
{
echo "${THISSCRIPTNAME}: cannot rename to target '${1}': File exists"
exit 130;
}
fi
if [ -L "${1}" ] ; then
{
echo "${THISSCRIPTNAME}: cannot rename to target '${1}': Softlink exists"
exit 130;
}
fi
}
fail_if_target_already_exists(){
fail_if_file_already_exists "${1}"
if [ -d "${1}/" ] ; then
{
echo "${THISSCRIPTNAME}: cannot rename to target '${1}': Directory exists"
exit 130;
}
fi
if [ -e "${1}" ] ; then
{
echo "${THISSCRIPTNAME}: cannot rename to target '${1}': Target exists in some form, not a softlink, not a directory, and not a regular file."
exit 130;
}
fi
}
delete_if_folder_empty() {
local CURR_DIR=$(pwd)
cd "${1}"
local TEMP_NAMES=$(ö .)
(( $THINKING_MACHINE )) && [[ -z "${TEMP_NAMES}" ]] && echo " +-- delete dir : ${1} : because is empty"
(( ! $THINKING_MACHINE ))&& [[ -z "${TEMP_NAMES}" ]] && cd "${CURR_DIR}" && rm -rf "${1}"
cd "${CURR_DIR}"
}
remove_all_found_files() {
local SEEKING
local IMPLACING="";
for SEEKING in ${@}; do
{
# Getting the source directory of a Bash script from within REF: https://stackoverflow.com/questions/3362920/get-just-the-filename-from-a-path-in-a-bash-script
# Get just the filename from a path in a Bash script [duplicate] REF: https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
local NEW_IMPLACING_FOLDER=$(echo $(dirname "${IMPLACING}") | sed '/\./d') # ${IMPLACING##*/} # get name of directory name without full path in bash,, or last string after last slash / # REF http://stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-bash-script
local NEW_IMPLACING_FILE=$(basename "${IMPLACING}") # ${IMPLACING%/*} # get only directoy, --or everything before the last slash / # REF http://stackoverflow.com/questions/6121091/get-file-directory-path-from-file-path
local OLD_SEEKING_FOLDER=$(echo $(dirname "${SEEKING}") | sed '/\./d') # ${SEEKING##*/} # get name of directory name without full path in bash,, or last string after last slash / # REF http://stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-bash-script
local OLD_SEEKING_FILE=$(basename "${SEEKING}") # ${SEEKING%/*} # get only directoy, --or everything before the last slash / # REF http://stackoverflow.com/questions/6121091/get-file-directory-path-from-file-path
(( $THINKING_MACHINE )) && echo "Provided information: .......................................... - START"
(( $THINKING_MACHINE )) && echo "NEW FOLDER: ${NEW_IMPLACING_FOLDER}"
(( $THINKING_MACHINE )) && echo " NEW FILE: ${NEW_IMPLACING_FILE}"
(( $THINKING_MACHINE )) && echo "OLD_FOLDER: ${OLD_SEEKING_FOLDER}"
(( $THINKING_MACHINE )) && echo " OLD FILE: ${OLD_SEEKING_FILE}"
(( $THINKING_MACHINE )) && echo "Provided information: .......................................... - END "
local FIND_FILENAMES=$(ö "${SEEKING}")
# 1 Rename file
# Determine if the new file has SUBDIRECTORIES
# ö "${SEEKING}" | xargs -I {} | mv "${SEEKING}" "${IMPLACING}" # Deprecated - Simple file replace
local ONELINE_FOLDER
local ONELINE_FILE
(( $THINKING_MACHINE )) && [[ -z "${FIND_FILENAMES}" ]] && echo "# 1 Rename file - mmm no Files Found named : ${SEEKING} :"
for ONELINE in ${FIND_FILENAMES}; do
{
if [ ! -z "${ONELINE}" ] ; then # not empty
{
ONELINE_FOLDER=$(echo $(dirname "${ONELINE}") | sed '/\./d') # ${ONELINE##*/} # get name of directory name without full path in bash,, or last string after last slash / # REF http://stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-bash-script
ONELINE_FILE=$(basename "${ONELINE}") # ${ONELINE%/*} # get only directoy, --or everything before the last slash / # REF http://stackoverflow.com/questions/6121091/get-file-directory-path-from-file-path
(( $THINKING_MACHINE )) && echo " "
(( $THINKING_MACHINE )) && echo " Deleting : .............................. ${ONELINE_FILE} ............ - START "
(( $THINKING_MACHINE )) && echo " "
(( $THINKING_MACHINE )) && echo rm "${ONELINE}"
(( ! $THINKING_MACHINE )) && rm "${ONELINE}"
delete_if_folder_empty "${OLD_SEEKING_FOLDER}"
replace_in_all_files "${SEEKING}" "${IMPLACING}"
(( $THINKING_MACHINE )) && echo " "
(( $THINKING_MACHINE )) && echo " Deleting : .............................. ${ONELINE_FILE} ............ - END "
(( $THINKING_MACHINE )) && echo " "
}
fi
}
done
}
done
}
find_files_that_have_the_term() {
local SEEKING="$1";
sift -nQ --blocksize 50M "${SEEKING}" -l --output-sep="\x00" | sed '/.log$/d' # exclude log files
}
get_line_number_containing_seeking() {
local file_replacing="$1";
local SEEKING="$2";
#ag \<script --nocolor "${file_replacing}" | nur "${SEEKING}" | cut -d':' -f1
#ag \<link --nocolor "${file_replacing}" | nur "${SEEKING}" | cut -d':' -f1
ag "${SEEKING}" --nocolor "${file_replacing}" | nur "${SEEKING}" | cut -d':' -f1
}
remove_found_lines() {
local file_replacing="$1";
local delete_lines="$2";
sed -i ''${delete_lines}'d' "${file_replacing}"
}
seek_and_remove_found_complete_lines() {
local file_replacing="$1";
local SEEKING="$2";
local delete_lines=""
local line_number
for line_number in $(get_line_number_containing_seeking "${file_replacing}" "${SEEKING}"); do
{
if [[ -z "${delete_lines}" ]]; then
delete_lines="${line_number}"
else
delete_lines="${delete_lines};${line_number}"
fi
remove_found_lines "${file_replacing}" "${delete_lines}"
}
done
}
replace_in_all_files() {
local SEEKING="$1";
local IMPLACING="$2";
# Replace in all files
# 2 Replace word :SEEKING: inside all files
(( $THINKING_MACHINE )) && echo "# 2 Replace word : $SEEKING : inside all files to : $IMPLACING :"
(( $THINKING_MACHINE )) && echo "# 2.1 nur results:---- start "
(( $THINKING_MACHINE )) && nur "${SEEKING}"
(( $THINKING_MACHINE )) && echo "# 2.1 nur results:---- end "
#(( $THINKING_MACHINE )) && sift -nQ --blocksize 50M "${SEEKING}" -l --output-sep="\x00"
local file_replacing
local line_number
for file_replacing in $(find_files_that_have_the_term "${SEEKING}"); do
{
seek_and_remove_found_complete_lines "${file_replacing}" "${SEEKING}"
}
done
(( ! $THINKING_MACHINE )) && äö "${SEEKING}" "${IMPLACING}"
#(( ! $THINKING_MACHINE )) && find .* * *.* *. -type f 2> /dev/null | sift -nQ --blocksize 50M "${SEEKING}" -l --output-sep="\x00" | xargs -I {} sed -i -e s"${SEEKING}""${IMPLACING}"g "{}" # 2> /dev/null suppress stderr messages in a bash script REF: https://unix.stackexchange.com/questions/184804/suppress-stderr-messages-in-a-bash-script
}
log_this_action() {
(( ! $THINKING_MACHINE )) && [ ! -f deleting.log ] && touch deleting.log
(( ! $THINKING_MACHINE )) && echo "${*}" >> deleting.log
(( ! $THINKING_MACHINE )) && [ ! -f actions.log ] && touch actions.log
(( ! $THINKING_MACHINE )) && echo "${*}" >> actions.log
}
THINKING_MACHINE=0
[[ "${@}" == *"--thinking-machine"* ]] && THINKING_MACHINE=1
[[ "${@}" == *"--t"* ]] && THINKING_MACHINE=1
remove_all_found_files "${@}"
replace_in_all_files "${@}"
(( $THINKING_MACHINE )) && echo "# 3 Log action inside a file for repetition purposes "
log_this_action "${THISSCRIPTNAME} ${*}"
(( $THINKING_MACHINE )) && echo "...end "