rmmsafe
This commit is contained in:
1
bin/rm-safe
Symbolic link
1
bin/rm-safe
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../files/rm-safe
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import os, argparse, sys
|
import os, argparse, sys
|
||||||
from shutil import copyfile
|
from shutil import copyfile, copytree
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
def get_options():
|
def get_options():
|
||||||
@@ -12,13 +12,13 @@ def get_options():
|
|||||||
action = "store_false",
|
action = "store_false",
|
||||||
dest = "move",
|
dest = "move",
|
||||||
default = True,
|
default = True,
|
||||||
help = "Copy file instead of moving",
|
help = "Copy path instead of moving",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-d',
|
'-d',
|
||||||
action = "store_true",
|
action = "store_true",
|
||||||
dest = "date",
|
dest = "date",
|
||||||
help = "Use file's date+time as version number",
|
help = "Use paths date+time as version number",
|
||||||
default = False
|
default = False
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@@ -105,8 +105,6 @@ def test_existing(name, overwrite):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
opts = get_options()
|
opts = get_options()
|
||||||
if os.path.isdir(opts.file):
|
|
||||||
raise ValueError("Can not handle directories")
|
|
||||||
if opts.date:
|
if opts.date:
|
||||||
new_name = get_date_name(opts.file)
|
new_name = get_date_name(opts.file)
|
||||||
else:
|
else:
|
||||||
@@ -117,4 +115,7 @@ if __name__ == "__main__":
|
|||||||
if opts.move:
|
if opts.move:
|
||||||
os.rename(opts.file, new_name)
|
os.rename(opts.file, new_name)
|
||||||
else:
|
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
36
files/rm-safe
Executable 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
|
||||||
|
|
||||||
Reference in New Issue
Block a user