bash回收站

WindSpirit發表於2018-05-15

通過替換rm命令使被刪除的檔案或資料夾被移動到~/.delete/日期資料夾,方便恢復

可以定期手動刪除.delete資料夾或通過額外配置定期刪除減少空間佔用

# for rm {{

del_time=$(date +%H%M%S)
bak_date=$(date +%Y%m%d)

checkDelDir ()
{
    if [ ! -d $HOME/.delete ]
    then
        mkdir $HOME/.delete
    fi

    if [ ! -d ~/.delete/${bak_date} ]
    then
        mkdir ~/.delete/${bak_date}
    fi
}

toBackup()
{
    checkDelDir
    for thing in $@
    do
        echo $thing | grep '^-' > /dev/null
        if [ ! $? = 0 ]
        then
            mv $thing $HOME/.delete/${bak_date}/${del_time}-$thing
            echo rename $thing to ${del_time}-$thing and mv to ~/.delete/${bak_date}
        fi
    done
}

cleanDelete()
{
    echo 'Clear all backup files?[y/N]'
    read confirm
    [ $confirm = 'y' ] || [ $confirm = 'Y' ]  && /bin/rm -rf $HOME/.delete/*
}

listDelete()
{
    echo 'ls $HOME/.delete/${bak_date}'
    echo 'Only list what you delete today'
    echo
    ls $HOME/.delete/${bak_date}
}

alias rm=toBackup
alias lsdel=listDelete
alias cleandel=cleanDelete

# }}