1

I am working on mimicing rm behaviour with added functionality similar to recycle bin. This is what my code looks like so far:

#!/bin/bash

# tests if bin does not exist and if true, create it
binName=$HOME/deleted

if [ ! -d $binName ]
then
        mkdir $binName
else
        echo "recycle bin exists"
fi

if [ $# -eq 0 ] ; then
        echo "No argument passed"
elif [ -d $1 ] ; then
        echo "is dir"
elif [ ! -e $1 ] ; then
        echo "does not exist"
else
        mv $1 $binName
fi

I am struggling with is adding an under score and inode number to the end of the moved file to avoid duplicate file errors in the bin directory. I tried using brace expansion from this link but it causes errors.

Community
  • 1
  • 1
Singh
  • 545
  • 2
  • 5
  • 24
  • What OS are you using? – Mark Setchell Apr 11 '15 at 13:23
  • 1
    Out of curiosity, what will you do if the user "recycles" two hardlinks to the same underlying file with the same basename? E.g. `mkdir foo; mkdir bar; touch foo/a; ln foo/a bar/; recycle foo/a; recycle bar/a`? – pilcrow Apr 11 '15 at 13:38
  • I am using putty on windows 7 to ssh into my unix environment – Singh Apr 11 '15 at 13:48
  • @pilcrow I only started leaning unix this week - i am basically building two scripts, one that will move file to a recycle bin directory and another that will restore the file to its original location facilitated by a hidden file which will contain the source info. I dont understand what hardlinks are to be honest – Singh Apr 11 '15 at 13:52
  • @Singh, welcome to Unix! It's a nice place. We talk about "hardlinks" when we mean that several files point to the same inode. In this case, the files would have the same inode number: your script will want to "recycle" `foo/a` to `$HOME/deleted/a_12345` (or whatever) and `bar/a` to `$HOME/deleted/a_12345` – pilcrow Apr 11 '15 at 14:06
  • @pilcrow cheers! I have heard its really cool as well. I used to think each file had a unique inode but you learm something new everyday :) – Singh Apr 11 '15 at 14:16

2 Answers2

2

Try this with ls:

inode=$(ls -id "${1}" | cut -d " " -f 1)
mv "$1" "${binName}/${1}_${inode}"

or with stat:

inode=$(stat --printf %i "${1}")
mv "$1" "${binName}/${1}_${inode}"

or with GNU find:

inode=$(find -maxdepth 1 -name "${1}" -printf "%i")
mv "$1" "${binName}/${1}_${inode}"
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

In the accepted answer to the question you linked, brace expansions are used to avoid repeating the original file name. (It'd appear once as the source and once as part of the target name.) This would be rather pointless in your case, as you already have the name in a variable with the short name $1.

So you can just

mv $1 $binName/${1}_$(${command that gives the inode of} $1)

e.g. as @Cyrus suggested

mv $1 "${binName}/${1}_"$(find . -maxdepth 1 -name "${1}" -printf "%i")

It'd still be possible to use brace expansion to avoid one of the repetitions of "$1", but that'd just make the command harder to read.

A note about creating directories:

Instead of

if [ ! -d $binName ]
then
        mkdir $binName
fi

you might simply use

mkdir -p $binName

This will leave the directory unaltered if it already exists. And if any parent directories are also missing, it'll create those too as needed. However, it will of course not be able to output "recycle bin exists", but I guess you only had that for debugging/demonstration reasons, anyway.

Community
  • 1
  • 1
das-g
  • 9,718
  • 4
  • 38
  • 80
  • Hi das-g, thanks a lot for your comments - I will keep them in mind when writing the scripts - thanks for your help – Singh Apr 11 '15 at 13:45