Compare commits
No commits in common. "581c63591644aefc87f5d491bcbbf1262b96bb4b" and "4d9e394336489fc028912e0fbf94e29d4be21164" have entirely different histories.
581c635916
...
4d9e394336
14
README.md
14
README.md
@ -1,18 +1,18 @@
|
|||||||
# trash.sh - A trash bin for files and directories instead of deleting them directly
|
# trash - A trash bin for files and directories instead of deleting them directly
|
||||||
|
|
||||||
[![Build Status](https://drone.pyas.de/api/badges/Kim/trash.sh/status.svg)](https://drone.pyas.de/Kim/trash.sh)
|
[![Build Status](https://drone.pyas.de/api/badges/Kim/trash/status.svg)](https://drone.pyas.de/Kim/trash)
|
||||||
|
|
||||||
## Installation:
|
## Installation:
|
||||||
Put the trash.sh file to your desired location (e.g home directory) and source it in your $HOME/.bashrc or in /etc/.bashrc:
|
the trash.sh file in your desired path and source it in your $HOME/.bashrc or in /etc/.bashrc:
|
||||||
|
|
||||||
. /path/to/trash.sh
|
. /path/to/trash.sh
|
||||||
|
|
||||||
- When the script runs, it first checks if `$HOME/.trash/` (your trash bin) exists and creates it if missing.
|
- As the Script runs it first checks if $HOME/.trash/ (your trash bin) in your home dir exists and creates it if missing.
|
||||||
|
|
||||||
Add the cleanup job to your crontab:
|
Add cleanup job to your cron:
|
||||||
|
|
||||||
# .trash cleanup of all contents (files and dirs) older than 31 days every morning at 06:00
|
# .trash cleanup of all contents (files and dirs) older than 31 days every morning at 06:00
|
||||||
0 6 * * 0 find $HOME/.trash/ -mtime +31 -delete
|
0 6 * * 0 find /home/YOURHOME/.trash/ -mtime +31 -delete
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
@ -23,4 +23,4 @@ Add the cleanup job to your crontab:
|
|||||||
- `trash_restore FILE1 DIR2 ...` to restore specific files and directories from your trash to it's origin
|
- `trash_restore FILE1 DIR2 ...` to restore specific files and directories from your trash to it's origin
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
Repo Icon made by [Freepik](https://www.freepik.com "Freepik") from [www.flaticon.com](https://www.flaticon.com/ "Flaticon")
|
Repo Icon made by [Freepik](https://www.freepik.com "Freepik") from [www.flaticon.com](https://www.flaticon.com/ "Flaticon")
|
75
trash.sh
75
trash.sh
@ -1,21 +1,18 @@
|
|||||||
#!/usr/bin/env bash
|
#!/bin/bash
|
||||||
# Trash Bin Functionss
|
# Trash Functions
|
||||||
# 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 file/directory as argument required:\nExamples: FILE.txt /DIR/FILE.txt SUB_DIR/FILE.* /DIR/* DIR/DIR ..."
|
then echo -e "At least one argument required:\ncommand FILE1 DIR2 ..."
|
||||||
return 1
|
exit
|
||||||
else
|
|
||||||
return 0
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,13 +21,13 @@ trash_list() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trash_put() {
|
trash_put() {
|
||||||
usage "$#" && {
|
usage "$#"
|
||||||
for FILE in $@; do
|
|
||||||
# Store old filepath for later restore
|
for FILE in $@; do
|
||||||
echo "$FILE $(readlink -f "$FILE")" >>"$FILE_PATHS_FILE"
|
# Store old filepath for later restore
|
||||||
done
|
echo "$FILE $(readlink -f "$FILE")" >>"$FILE_PATHS_FILE"
|
||||||
mv -v $* "$TRASH_DIR/"
|
done
|
||||||
}
|
mv -v $* "$TRASH_DIR/"
|
||||||
}
|
}
|
||||||
|
|
||||||
trash_empty() {
|
trash_empty() {
|
||||||
@ -39,34 +36,34 @@ trash_empty() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trash_restore() {
|
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)
|
|
||||||
|
|
||||||
# Move file to old filepath
|
for FILE_TO_RESTORE in "$@"; do
|
||||||
mv -v "${TRASH_DIR}/${FILE_TO_RESTORE}" "$OLD_PATH"
|
# Get old filepath for restore
|
||||||
|
OLD_PATH=$(grep "$FILE_TO_RESTORE" "$FILE_PATHS_FILE" | cut -d ' ' -f2)
|
||||||
|
|
||||||
# Replace / with \/ from string and remove filepath from file after move was successful
|
# Move file to old filepath
|
||||||
sed -i "/${FILE_TO_RESTORE////\\/}/d" "$FILE_PATHS_FILE"
|
mv -v "${TRASH_DIR}/${FILE_TO_RESTORE}" "$OLD_PATH"
|
||||||
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() {
|
trash_rm() {
|
||||||
usage "$#" && {
|
usage "$#"
|
||||||
for FILE_TO_REMOVE in "$@"; do
|
|
||||||
if ! echo "$FILE_TO_REMOVE" | grep "$HOME/.trash"
|
for FILE_TO_REMOVE in "$@"; do
|
||||||
then FILEPATH_TO_REMOVE="$HOME/.trash/$FILE_TO_REMOVE"
|
if ! echo "$FILE_TO_REMOVE" | grep "$HOME/.trash"
|
||||||
else FILEPATH_TO_REMOVE="$FILE_TO_REMOVE"
|
then FILEPATH_TO_REMOVE="$HOME/.trash/$FILE_TO_REMOVE"
|
||||||
fi
|
else FILEPATH_TO_REMOVE="$FILE_TO_REMOVE"
|
||||||
if [ -f "$FILEPATH_TO_REMOVE" ]; then
|
fi
|
||||||
\rm -irf "$FILEPATH_TO_REMOVE" && sed -i "/$FILE_TO_REMOVE/d" "$FILE_PATHS_FILE"
|
if [ -f "$FILEPATH_TO_REMOVE" ]; then
|
||||||
else
|
\rm -irf "$FILEPATH_TO_REMOVE" && sed -i "/$FILE_TO_REMOVE/d" "$FILE_PATHS_FILE"
|
||||||
echo "$FILE_TO_REMOVE is not in Trash." && false
|
else
|
||||||
fi
|
echo "$FILE_TO_REMOVE is not in Trash." && false
|
||||||
done
|
fi
|
||||||
}
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
alias trash='trash_put'
|
alias trash='trash_put'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user