This commit is contained in:
ville rantanen
2019-04-11 18:47:42 +03:00
parent f2e9ed2b76
commit 8bbe668c46
3 changed files with 44 additions and 6 deletions

1
bin/rm-safe Symbolic link
View File

@@ -0,0 +1 @@
../files/rm-safe

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env python
import os, argparse, sys
from shutil import copyfile
from shutil import copyfile, copytree
from datetime import datetime
def get_options():
@@ -12,13 +12,13 @@ def get_options():
action = "store_false",
dest = "move",
default = True,
help = "Copy file instead of moving",
help = "Copy path instead of moving",
)
parser.add_argument(
'-d',
action = "store_true",
dest = "date",
help = "Use file's date+time as version number",
help = "Use paths date+time as version number",
default = False
)
parser.add_argument(
@@ -105,8 +105,6 @@ def test_existing(name, overwrite):
if __name__ == "__main__":
opts = get_options()
if os.path.isdir(opts.file):
raise ValueError("Can not handle directories")
if opts.date:
new_name = get_date_name(opts.file)
else:
@@ -117,4 +115,7 @@ if __name__ == "__main__":
if opts.move:
os.rename(opts.file, new_name)
else:
copyfile(opts.file, new_name)
if os.path.isdir(opts.file):
copytree(opts.file, new_name)
else:
copyfile(opts.file, new_name)

36
files/rm-safe Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/bash
set -e
function helpexit() {
echo Delete files by moving to a safe location in your ~/.cache/rm-safe
echo This command is always recursive!
echo '-v verbose, --clear delete cache permanently'
exit
}
function clearsafe() {
test -d "$SAFELOC" && rm-progress -f "$SAFELOC"
exit
}
SAFELOC=~/.cache/rm-safe
FOLDERS=()
[[ -z "$1" ]] && helpexit
for ((i=1; i<=${#@}; i++)) {
[[ "${!i}" = "-h" ]] && helpexit
[[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-v" ]] && { VERBOSE=1; continue; }
[[ "${!i}" = "--clear" ]] && { clearsafe; continue; }
[[ "${!i}" = "-"* ]] && helpexit
FOLDERS+=( "${!i}" )
}
mkdir -p "$SAFELOC"
now=$( date -Idate )
for d in "${FOLDERS[@]}"; do
if [[ ! -e "$d" ]]; then continue; fi
path=$( readlink -f "$d" )
dir=$( dirname "$path" )
if [[ "$VERBOSE" -eq 1 ]]; then echo "$path"; fi
if [[ -e "$SAFELOC/$now/$path" ]]; then file-version -q "$SAFELOC/$now/$path"; fi
mkdir -p "$SAFELOC/$now/$dir"
mv "$d" "$SAFELOC/$now/$dir"
done