This commit is contained in:
parent
59cb5e3f97
commit
581c635916
27
trash.sh
27
trash.sh
@ -1,18 +1,21 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
# Trash Functions
|
# 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"
|
[ ! -d "$TRASH_DIR" ] && mkdir "$TRASH_DIR"
|
||||||
FILE_PATHS_FILE="$TRASH_DIR/.filepaths"
|
|
||||||
[ ! -f "$FILE_PATHS_FILE" ] && touch "$FILE_PATHS_FILE"
|
[ ! -f "$FILE_PATHS_FILE" ] && touch "$FILE_PATHS_FILE"
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
local func_args="$1"
|
local func_args="$1"
|
||||||
|
|
||||||
if [[ $func_args == 0 ]];
|
if [[ $func_args == 0 ]];
|
||||||
then echo -e "At least one argument required:\ncommand FILE1 DIR2 ..."
|
then echo -e "At least one file/directory as argument required:\nExamples: FILE.txt /DIR/FILE.txt SUB_DIR/FILE.* /DIR/* DIR/DIR ..."
|
||||||
exit
|
return 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,14 +24,14 @@ trash_list() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trash_put() {
|
trash_put() {
|
||||||
usage "$#"
|
usage "$#" && {
|
||||||
|
|
||||||
for FILE in $@; do
|
for FILE in $@; do
|
||||||
# Store old filepath for later restore
|
# Store old filepath for later restore
|
||||||
echo "$FILE $(readlink -f "$FILE")" >>"$FILE_PATHS_FILE"
|
echo "$FILE $(readlink -f "$FILE")" >>"$FILE_PATHS_FILE"
|
||||||
done
|
done
|
||||||
mv -v $* "$TRASH_DIR/"
|
mv -v $* "$TRASH_DIR/"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
trash_empty() {
|
trash_empty() {
|
||||||
echo "Delete $(find "$TRASH_DIR" ! -name ".filepaths" | tail -n +2 | wc -l) files in trash"
|
echo "Delete $(find "$TRASH_DIR" ! -name ".filepaths" | tail -n +2 | wc -l) files in trash"
|
||||||
@ -36,8 +39,7 @@ trash_empty() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trash_restore() {
|
trash_restore() {
|
||||||
usage "$#"
|
usage "$#" && {
|
||||||
|
|
||||||
for FILE_TO_RESTORE in "$@"; do
|
for FILE_TO_RESTORE in "$@"; do
|
||||||
# Get old filepath for restore
|
# Get old filepath for restore
|
||||||
OLD_PATH=$(grep "$FILE_TO_RESTORE" "$FILE_PATHS_FILE" | cut -d ' ' -f2)
|
OLD_PATH=$(grep "$FILE_TO_RESTORE" "$FILE_PATHS_FILE" | cut -d ' ' -f2)
|
||||||
@ -49,10 +51,10 @@ trash_restore() {
|
|||||||
sed -i "/${FILE_TO_RESTORE////\\/}/d" "$FILE_PATHS_FILE"
|
sed -i "/${FILE_TO_RESTORE////\\/}/d" "$FILE_PATHS_FILE"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
trash_rm() {
|
trash_rm() {
|
||||||
usage "$#"
|
usage "$#" && {
|
||||||
|
|
||||||
for FILE_TO_REMOVE in "$@"; do
|
for FILE_TO_REMOVE in "$@"; do
|
||||||
if ! echo "$FILE_TO_REMOVE" | grep "$HOME/.trash"
|
if ! echo "$FILE_TO_REMOVE" | grep "$HOME/.trash"
|
||||||
then FILEPATH_TO_REMOVE="$HOME/.trash/$FILE_TO_REMOVE"
|
then FILEPATH_TO_REMOVE="$HOME/.trash/$FILE_TO_REMOVE"
|
||||||
@ -65,6 +67,7 @@ trash_rm() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
alias trash='trash_put'
|
alias trash='trash_put'
|
||||||
alias rm='echo "This is not the command you are looking for. Use \rm instead"; false'
|
alias rm='echo "This is not the command you are looking for. Use \rm instead"; false'
|
||||||
|
Loading…
Reference in New Issue
Block a user