Skip to content

Commit

Permalink
Update scripts description
Browse files Browse the repository at this point in the history
  • Loading branch information
satk0 committed Feb 20, 2024
1 parent 0692d6e commit 5adb0e6
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ Tools available:

## Installation

Clone the repo:

git clone git@github.com:satk0/usbdrivetools.git

Symlink scripts to `~/.local/bin/` (run from project directory):

ln -s "$PWD"/scripts/* ~/.local/bin/

Made with Love ❤️ by [@satk0](https://github.com/satk0)
6 changes: 5 additions & 1 deletion usbcp.sh → scripts/usbcp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ NC='\033[0m' # No Color

function main() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo ' Usage: usbcp SRC [SRC] DEST'
echo ' Usage: usbcp SRC[/] [SRC[/]] DEST'
echo 'Copy SRC to (USB mounted) DEST.'
echo ''
echo 'Key notes if SRC is directory:'
echo ' SRC/ - copies SRC directory into DEST.'
echo ' SRC - copies SRC directory contents into DEST.'
return
fi

Expand Down
83 changes: 83 additions & 0 deletions scripts/usbmv
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash

# Colors for 'echo' command
GREEN='\033[1;32m'
RED='\033[1;31m'
NC='\033[0m' # No Color

function main() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo ' Usage: usbmv SRC[/] [SRC[/]] DEST'
echo 'Copy SRC to (USB mounted) DEST.'
echo ''
echo 'Key notes if SRC is directory:'
echo ' SRC/ - copies SRC directory into DEST.'
echo ' SRC - copies SRC directory contents into DEST.'
return
fi

args=( "$@" )
pathsFrom=( "${args[@]:: ${#args[@]}-1}" )
pathTo=${args[-1]}

echo "${pathsFrom[@]}"
echo "${pathTo}"

startTime=$SECONDS

echo '1. Running rsync...'
# For NTFS, ExFAT, etc. support:
# check: https://unix.stackexchange.com/a/532192
# and: https://stackoverflow.com/a/668049
#
# moving files with rsync: https://unix.stackexchange.com/a/290276
rsync -rlDvP --remove-source-files "${pathsFrom[@]}" "$pathTo" 2>rsync.err

# && find "${pathsFrom[@]}" -type d -empty -delete

# check any errors
if [ -s rsync.err ]; then
echo -e "${RED}\"rsync\" execution error:${NC}" >&2
cat rsync.err >&2
rm -f ./rsync.err
echo -e "${RED}Quiting...${NC}" >&2
return
fi

rm -f ./rsync.err

echo '2. Syncing...'

sync &
pID=$!;
killErr=""

while [ "$killErr" = "" ]; do
killErr=$(kill -0 "$pID" 2>&1 > /dev/null)
cacheInfo=$(grep -E '(Dirty|Writeback):' /proc/meminfo)

echo "$cacheInfo"
sleep 0.5

clearLastLines 2
done

echo "$cacheInfo"

elapsedTime=$(( SECONDS - startTime ))
echo 'Finished in:'
date -ud "@$elapsedTime" +'%H hr %M min %S sec'
echo -e "${GREEN}Copied successfully!${NC}"
}

# https://stackoverflow.com/a/78015981
function clearLastLines() {
local linesToClear=$1

for (( i=0; i<linesToClear; i++ )); do
tput cuu 1
tput el
done
}

main "$@"; exit

0 comments on commit 5adb0e6

Please sign in to comment.