开发者

Recycle bin in bash problem

开发者 https://www.devze.com 2023-03-12 22:57 出处:网络
I need to make a recycle bin code using bash. Here is what I have done so far. My problem is that when I move a file with the same name into the trash folder it just overwrites the previous file. Can

I need to make a recycle bin code using bash. Here is what I have done so far. My problem is that when I move a file with the same name into the trash folder it just overwrites the previous file. Can you give me any suggestions on how to开发者_如何学Go approach this problem?

#!/bin/bash

mkdir -p "$HOME/Trash"
if [ $1 = -restore ]; then
    while read file; do
    mv $HOME/Trash/$2 /$file
    done < try.txt
else
    if [ $1 = -restoreall ]; then
        mv $HOME/Trash/* /$PWD
    else
        if [ $1 = -empty ]; then
            rm -rfv /$HOME/Trash/*
        else
            mv $PWD/"$1"/$HOME/Trash
            echo -n "$PWD" >> /$HOME/Bash/try
        fi
    fi
fi


You could append the timestamp of the time of deletion to the filename in your Trash folder. Upon restore, you could strip this off again.


To add a timestamp to your file, use something like this:

DT=$(date +'%Y%m%d-%H%M%S')
mv $PWD/"$1" "/$HOME/Trash/${1}.${DT}"

This will, e.g., create a file like initrd.img-2.6.28-11-generic.20110615-140159 when moving initrd.img-2.6.28-11-generic.

To get the original filename, strip everything starting from the last dot, like with:

NAME_WITHOUT_TIMESTAMP=${file%.*-*}

The pattern is on the right side after the percentage char. (.* would also be enough to match.)


Take a look how trash-cli does it. It's written in Python and uses the same trash bin as desktop environments. Trash-cli is available at least in the big Linux distributions.

http://code.google.com/p/trash-cli/


Probably the easiest thing to do is simply add -i to the invocation of mv. That will prompt the user whether or not to replace. If you happen to have access to gnu cp (eg, on Linux), you could use cp --backup instead of mv.

0

精彩评论

暂无评论...
验证码 换一张
取 消