Skip to content

Instantly share code, notes, and snippets.

@theAkito
Last active May 27, 2020 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theAkito/30b807b3c7ef735754be99aa184027dc to your computer and use it in GitHub Desktop.
Save theAkito/30b807b3c7ef735754be99aa184027dc to your computer and use it in GitHub Desktop.
#!/bin/bash
#########################################################################
# Copyright (C) 2020 Akito <the@akito.ooo> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
#########################################################################
## Moves/copies all MOBI files to external storage medium.
## Run script with first argument as the destination path,
## where all the MOBI files should be moved/copied to.
################################# Boilerplate of the Boilerplate ####################################################
# Coloured Echoes #
function red_echo { echo -e "\033[31m$@\033[0m"; } #
function green_echo { echo -e "\033[32m$@\033[0m"; } #
function yellow_echo { echo -e "\033[33m$@\033[0m"; } #
function white_echo { echo -e "\033[1;37m$@\033[0m"; } #
# Coloured Printfs #
function red_printf { printf "\033[31m$@\033[0m"; } #
function green_printf { printf "\033[32m$@\033[0m"; } #
function yellow_printf { printf "\033[33m$@\033[0m"; } #
function white_printf { printf "\033[1;37m$@\033[0m"; } #
# Debugging Outputs #
function white_brackets { local args="$@"; white_printf "["; printf "${args}"; white_printf "]"; } #
function echoDebug { local args="$@"; if [[ ${debug_flag} == true ]]; then #
white_brackets "$(white_printf "DEBUG")" && echo " ${args}"; fi; } #
function echoInfo { local args="$@"; white_brackets "$(green_printf "INFO" )" && echo " ${args}"; } #
function echoWarn { local args="$@"; white_brackets "$(yellow_printf "WARN" )" && echo " ${args}"; 1>&2; } #
function echoError { local args="$@"; white_brackets "$(red_printf "ERROR")" && echo " ${args}"; 1>&2; } #
# Silences commands' STDOUT as well as STDERR. #
function silence { local args="$@"; ${args} &>/dev/null; } #
# Check your privilege. #
function checkPriv { if [[ "$EUID" != 0 ]]; then echoError "Please run me as root."; exit 1; fi; } #
# Returns 0 if script is sourced, returns 1 if script is run in a subshell. #
function checkSrc { (return 0 2>/dev/null); if [[ "$?" == 0 ]]; then return 0; else return 1; fi; } #
# Prints directory the script is run from. Useful for local imports of BASH modules. #
# This only works if this function is defined in the actual script. So copy pasting is needed. #
function whereAmI { printf "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"; } #
# Alternatively, this alias works in the sourcing script, but you need to enable alias expansion. #
alias whereIsMe='printf "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"' #
debug_flag=false #
#########################################################################################################################
current_time="$(date +"%Y%m%d%H%M%S")"
# If the script was interrupted in its process of transporting files
# then you can change the following value to the filename of the
# file that was created on the first run.
# That way, you don't need to build the ebook_list again.
filename="ebook_list_${current_time}.txt"
err_log="ebook_list_errors_${current_time}.log"
# Change following value to the actual destination path.
external_drive_dest="$1"
# Change value to "mv" if you want to move files.
# Change value to "cp" if you want to copy files.
action="mv"
echoInfo "Getting all MOBI files and writing them to ${filename}."
echoInfo "This will take a very long time......"
# Comment out the following line, if you set a non-empty ebook_list file, previously.
ls -R1 | xargs -L 1 readlink -f | grep -Ei '^[[:print:]]*\.mobi$' > ${filename}
if ! [[ -d "${external_drive_dest}" && -w "${external_drive_dest}" ]]; then
echoError "Destination is not writable. Please provide a writable destination path."
exit 1
fi
while read -r line; do
"${action}" "${line}" "${external_drive_dest}" 2>${err_log} && \
echoInfo "Transporting file ${line##*/} succeeded!" || \
echoError "Transporting file ${line##*/} failed."
done <${filename}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment