Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
6f5b5ce47c | |||
2f3c7e3f6a | |||
3d01dba823 | |||
581c635916 | |||
59cb5e3f97 | |||
4d9e394336 |
@ -77,4 +77,4 @@ steps:
|
||||
pull: if-not-exists
|
||||
commands:
|
||||
- . $HOME/.bashrc
|
||||
- trash
|
||||
- trash | grep required
|
||||
|
18
LICENSE
18
LICENSE
@ -2,20 +2,8 @@ MIT License
|
||||
|
||||
Copyright (c) 2021 Kim Oliver Drechsel <kim@drechsel.xyz>
|
||||
|
||||
Permission is hereby granted, without written agreement and without
|
||||
license or royalty fees, to use, copy, modify, and distribute this
|
||||
software and its documentation for any purpose, provided that the
|
||||
above copyright notice and the following two paragraphs appear in
|
||||
all copies of this software.
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
||||
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
||||
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
||||
IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
DAMAGE.
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
||||
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
||||
ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
||||
PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
14
README.md
14
README.md
@ -1,18 +1,18 @@
|
||||
# trash - A trash bin for files and directories instead of deleting them directly
|
||||
# trash.sh - A cli trash bin for files and directories instead of deleting them directly
|
||||
|
||||
[![Build Status](https://drone.pyas.de/api/badges/Kim/trash/status.svg)](https://drone.pyas.de/Kim/trash)
|
||||
[![Build Status](https://drone.pyas.de/api/badges/Kim/trash.sh/status.svg)](https://drone.pyas.de/Kim/trash.sh)
|
||||
|
||||
## Installation:
|
||||
the trash.sh file in your desired path and source it in your $HOME/.bashrc or in /etc/.bashrc:
|
||||
Put the trash.sh file to your desired location (e.g home directory) and source it in your `$HOME/.bashrc` or in `/etc/.bashrc`:
|
||||
|
||||
. /path/to/trash.sh
|
||||
|
||||
- As the Script runs it first checks if $HOME/.trash/ (your trash bin) in your home dir exists and creates it if missing.
|
||||
- When the script runs, it first checks if `$HOME/.trash/` (your trash bin) exists and creates it if missing.
|
||||
|
||||
Add cleanup job to your cron:
|
||||
Add the cleanup job to your crontab:
|
||||
|
||||
# .trash cleanup of all contents (files and dirs) older than 31 days every morning at 06:00
|
||||
0 6 * * 0 find /home/YOURHOME/.trash/ -mtime +31 -delete
|
||||
0 6 * * 0 find $HOME/.trash/ -mtime +31 -delete
|
||||
|
||||
## Usage:
|
||||
|
||||
@ -23,4 +23,4 @@ Add cleanup job to your cron:
|
||||
- `trash_restore FILE1 DIR2 ...` to restore specific files and directories from your trash to it's origin
|
||||
|
||||
## 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,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'
|
||||
|
Loading…
Reference in New Issue
Block a user