From 581c63591644aefc87f5d491bcbbf1262b96bb4b Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 28 Oct 2021 02:02:36 +0200 Subject: [PATCH] minor changes --- trash.sh | 75 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/trash.sh b/trash.sh index cb7f9b4..d5ea30e 100644 --- a/trash.sh +++ b/trash.sh @@ -1,18 +1,21 @@ -#!/bin/bash -# Trash Functions +#!/usr/bin/env bash +# Trash Bin Functionss +# On Github: https://github.com/kimdre/trash.sh -TRASH_DIR="$HOME"/.trash +TRASH_DIR="$HOME/.trash" +FILE_PATHS_FILE="$TRASH_DIR/.filepaths" [ ! -d "$TRASH_DIR" ] && mkdir "$TRASH_DIR" -FILE_PATHS_FILE="$TRASH_DIR/.filepaths" [ ! -f "$FILE_PATHS_FILE" ] && touch "$FILE_PATHS_FILE" usage() { local func_args="$1" if [[ $func_args == 0 ]]; - then echo -e "At least one argument required:\ncommand FILE1 DIR2 ..." - exit + then echo -e "At least one file/directory as argument required:\nExamples: FILE.txt /DIR/FILE.txt SUB_DIR/FILE.* /DIR/* DIR/DIR ..." + return 1 + else + return 0 fi } @@ -21,13 +24,13 @@ trash_list() { } trash_put() { - usage "$#" - - for FILE in $@; do - # Store old filepath for later restore - echo "$FILE $(readlink -f "$FILE")" >>"$FILE_PATHS_FILE" - done - mv -v $* "$TRASH_DIR/" + usage "$#" && { + for FILE in $@; do + # Store old filepath for later restore + echo "$FILE $(readlink -f "$FILE")" >>"$FILE_PATHS_FILE" + done + mv -v $* "$TRASH_DIR/" + } } trash_empty() { @@ -36,34 +39,34 @@ trash_empty() { } trash_restore() { - usage "$#" + usage "$#" && { + for FILE_TO_RESTORE in "$@"; do + # Get old filepath for restore + OLD_PATH=$(grep "$FILE_TO_RESTORE" "$FILE_PATHS_FILE" | cut -d ' ' -f2) - for FILE_TO_RESTORE in "$@"; do - # Get old filepath for restore - OLD_PATH=$(grep "$FILE_TO_RESTORE" "$FILE_PATHS_FILE" | cut -d ' ' -f2) + # Move file to old filepath + mv -v "${TRASH_DIR}/${FILE_TO_RESTORE}" "$OLD_PATH" - # Move file to old filepath - mv -v "${TRASH_DIR}/${FILE_TO_RESTORE}" "$OLD_PATH" - - # Replace / with \/ from string and remove filepath from file after move was successful - sed -i "/${FILE_TO_RESTORE////\\/}/d" "$FILE_PATHS_FILE" - done + # Replace / with \/ from string and remove filepath from file after move was successful + sed -i "/${FILE_TO_RESTORE////\\/}/d" "$FILE_PATHS_FILE" + done + } } trash_rm() { - usage "$#" - - for FILE_TO_REMOVE in "$@"; do - if ! echo "$FILE_TO_REMOVE" | grep "$HOME/.trash" - then FILEPATH_TO_REMOVE="$HOME/.trash/$FILE_TO_REMOVE" - else FILEPATH_TO_REMOVE="$FILE_TO_REMOVE" - fi - if [ -f "$FILEPATH_TO_REMOVE" ]; then - \rm -irf "$FILEPATH_TO_REMOVE" && sed -i "/$FILE_TO_REMOVE/d" "$FILE_PATHS_FILE" - else - echo "$FILE_TO_REMOVE is not in Trash." && false - fi - done + usage "$#" && { + for FILE_TO_REMOVE in "$@"; do + if ! echo "$FILE_TO_REMOVE" | grep "$HOME/.trash" + then FILEPATH_TO_REMOVE="$HOME/.trash/$FILE_TO_REMOVE" + else FILEPATH_TO_REMOVE="$FILE_TO_REMOVE" + fi + if [ -f "$FILEPATH_TO_REMOVE" ]; then + \rm -irf "$FILEPATH_TO_REMOVE" && sed -i "/$FILE_TO_REMOVE/d" "$FILE_PATHS_FILE" + else + echo "$FILE_TO_REMOVE is not in Trash." && false + fi + done + } } alias trash='trash_put'