2

I am trying to create a basic recycle bin concept in a VM using bash scripting. It will need to delete files that have been entered and place them into a directory that is created and save the path(origin) to a log file to be later used in a restore function.

I will start off with my delete/recycle code which I believe works just fine but seems kind of untidy/contains redundant code:

#!/bin/sh  

if [ ! -d ~/recycle ]
then mkdir ~/recycle
fi

if [ ! -d ~/recycle/recycle_log ]
then mkdir ~/recycle/recycle_log
fi

if [ ! -d ~/recycle/recycle_bin ]
then mkdir ~/recycle/recycle_bin
fi

if [ -d ~/recycle ]
then
echo "$(readlink -f "$1")" >> "$HOME/recycle/recycle_log/log_file" && mv "$1" "$HOME/recycle/recycle_bin"
echo "$(readlink -f "$2")" >> "$HOME/recycle/recycle_log/log_file" && mv "$2" "$HOME/recycle/recycle_bin"
echo "$(readlink -f "$3")" >> "$HOME/recycle/recycle_log/log_file" && mv "$3" "$HOME/recycle/recycle_bin"
echo "$(readlink -f "$4")" >> "$HOME/recycle/recycle_log/log_file" && mv "$4" "$HOME/recycle/recycle_bin"
fi

#end

Thereafter what I have for my restore script is as follows:

#!/bin/sh

cd "$HOME/recycle/recycle_bin" || exit 1
mv -i "$(grep "$1" "$HOME/recycle/recycle_log")"

I imagine this is somewhat close to what I need to return any deleted file stored in the log/recycle bin to be restored to its origin but the error I am getting is:

mv: missing destination file operand after `'

Any thoughts on where I'm going wrong?

  • You only give `mv` one argument. Maybe your thinking was `mv -i "$1" "$(grep "$1" "$HOME/recycle/recycle_log")"` to move the file to its log entry. – that other guy Nov 29 '16 at 18:36
  • @thatotherguy other guy That changes the error to `mv: cannot move 'example.txt' to ': No such file or directory` –  Nov 29 '16 at 18:59
  • I didn't notice that you write your log to `$HOME/recycle/recycle_log/log_file` but ask your grep to read from `$HOME/recycle/recycle_log`. You can use `mv -i "$1" "$(grep "$1" "$HOME/recycle/recycle_log/log_file")"` to fix that – that other guy Nov 29 '16 at 19:07
  • @thatotherguy I suspected that I was being a bit blind but didn't think I was that bad. On another note, since this is now working, how would I implement it so that the log file itself is cleared of that specific row when it restores it? –  Nov 29 '16 at 19:10
  • [Here's a question](http://stackoverflow.com/questions/5410757/delete-lines-in-a-text-file-that-containing-a-specific-string) about that. – that other guy Nov 29 '16 at 19:12
  • @thatotherguy Nice one, I will have a read over that, I take it that it won't be too hard to attach '.bak' to the end of a file should it already be at the destination? –  Nov 29 '16 at 19:20
  • Check out `mv --backup=numbered` – that other guy Nov 29 '16 at 19:28

1 Answers1

1

Try this:

recycle.sh

#!/bin/sh  

set -e

check_dir() {
    [ ! -d $1 ] || return 0
    mkdir --parents $1
}

check_dir "${HOME}/recycle/recycle_bin"
touch "${HOME}/recycle/recycle_log"

for file in "$@"; do
    echo "$(readlink -f "$file")" >> "${HOME}/recycle/recycle_log"
    mv "$file" "${HOME}/recycle/recycle_bin"
done

#end

restore.sh

#!/bin/sh

set -e

cd "${HOME}/recycle/recycle_bin" || exit 1

for name in "$@"; do
    file=$(grep "\/${name}\$" "${HOME}/recycle/recycle_log")
    mv -i $name "$file"
    sed -i "/\/${name}\$/ d" "${HOME}/recycle/recycle_log"
done

Some insights:

set -e: Abort on any error, to avoid some if's

$@: The array of arguments ($1, $2...)

[ ! -d $1 ] || return 0: Since we are using set -e, do not fail if the directory exists

grep "\/${name}\$" ...: Only matches the name at the end of the path

sed -i: sed in-place editing to remove the line

Vinicius
  • 1,060
  • 1
  • 12
  • 21