Help using shred instead of rm


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help using shred instead of rm
# 15  
Old 07-18-2006
Quote:
Originally Posted by reborg
look very closely at what is already quoted.

Hi thanks again reborg and Hitori, I have the command workign now, its
Code:
find "$TRASH/$1" -type f -exec shred -fu {} \;
find "$TRASH/$1" -type d -exec rmdir -p {} \;

I was using the semi-colon in the wrong place and had quotes I didnt need

but I don't think rmdir works anyway as it dosent delete the directories as when they contain any other directories, is this still possible? can I reverese find to delete from the one at the bottom rather than the top of the hierarchy?


Thanks again Mike
# 16  
Old 07-18-2006
Hey man, look closely at rmdir(1)
rmdir -p solves your problem while if you have for instance directories a/b/c, then rmdir a, rmdir a/b will be failed but rmdir a/b/c will remove a, b and c directories
# 17  
Old 07-18-2006
Cheers,

My code is about done, I have trouble with a for loop in the highlighted statement, it works fine for directories with contents but it I am moving an empty directory with options -ri and specified it fails, in fact my whole program fails because of this, say if I enter nothing but a file agrument then it wont delete, the problem lies within the highlighted function.

Code:
#!/bin/bash
#A program to emulate the "rm" command in UNIX.
# Created by Michael Kelly
# Last revison 18/07/2006

# INITIALIZE VARIABLES
NO_ARGS=0
FLAG_R=""
FLAG_F_I=""
FLAG_V=""
TRASH=$HOME/deleted

# FUNCTIONS

function errors() {
if [ "$#" -eq  "$NO_ARGS" ] ; then
      echo "rm: too few arguments"
      echo "Try \`rm --help' for more information."
      exit 0
elif [[  ! -f "$1"  && ! -d "$1"   ]] ; then
      echo "rm: cannot remove $ARG : no such file or directory"
      exit 0
elif [[ -d $ARG  &&  "$FLAG_R" = ""  ]] ; then
      echo "rm: \`$ARG' is a directory"
      exit 0
else
     checkExisting $1
fi
}


function checkExisting (){
if [ -d $TRASH/$1 ]; then

     find "$TRASH/$1" -type f -exec shred -fu {} \; 2>/dev/null
     find "$TRASH/$1" -type d -exec rmdir -p  {} \; 2>/dev/null
     directoryDelete $1
else
     directoryDelete $1

fi

}

function directoryDelete () {
if [[ -d "$1"  &&  "$FLAG_R" == "R" && "$FLAG_F_I" == "i" ]]; then
echo -n "rm: descend into directory \`$1'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
echo  "removing all entries of directory \`$1'?"

for FILE in "$1/*"
do
     delete $FILE
done
     echo -n "rm: remove directory \`$1'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     mv  $1 $TRASH 2>/dev/null
echo "rm: removing directory itself: \`$1'"

else
writePro $1

fi
fi
fi
}

function writePro () {
echo "Hello"
if ! [ -w  "$1" ] ; then
echo -n "rm: remove write-protected file \`$*'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     delete $1
     fi
else
     delete $1
fi

}

function delete() {
if [ "$FLAG_F_I" = "i" ] &&  [ -w  "$1" ] ; then
   interactive $1
elif [ "$FLAG_F_I" = "f"  ] ; then
   force $1
elif [ "$FLAG_R" = "R"  ] ; then
   remove $1
else
   remove $1
fi

}

function force () {

mv -f $1 $TRASH 2>/dev/null
verbose $1
}


function remove () {
mv  $1 $TRASH 2>/dev/null
verbose $1
}

function interactive () {
echo -n "rm: remove $1 ?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     remove $1
     else
     remove $1
     else
     exit 0
fi
}
function verbose () {
if [ "$FLAG_V" = "v" ] ; then
echo "removing \`$1'"
fi
}


# PARSE OPTIONS WITH GETOPTS

while getopts :rRfvi o
do    case $o in
           r|R) FLAG_R=R
             ;;
             f) FLAG_F_I=f
             ;;
             v) FLAG_V=v
             ;;
             i) FLAG_F_I=i
             ;;
             *) echo "rm: invalid option -$1"
                echo "try \`rm --help' for more information"
                exit 0
      esac
done
shift `expr $OPTIND - 1`

# START OF FLOW

if ! [ -d "$HOME/deleted" ] ; then
     mkdir $HOME/delete
elif  [ $# -eq $NO_ARGS ] ; then
errors
else
   for ARG in $*
   do
   errors $ARG
   done
fi

can you advise me further on why its behaving like this?

Thanks for all the previous help too!

Jack

Last edited by jack1981; 07-18-2006 at 05:44 PM..
# 18  
Old 07-18-2006
Hi,

I have my problems fixed, the only problem that still remains is that if the file isnt a directory and options r and i are not set then its not passing through the function to the else statement which should then pass to writePro function, it seems to be just existing.

Any ideas?
# 19  
Old 07-18-2006
Sorted it!

damn, had my "fi's" in the wrong place.

I am done apart from testing, I would just like to say a big thank you for any help you guys provided!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. BSD

Wipe out userland caches with shred, bleach bit is out of reach

Another topic that bothers me on bsd is the missing of bleach-bit. Looking at what bleach-bit can clean, the new version, it wipes out all the caches of the user. May someone can give me a hint how to clean the userland in bsd and all the hidden caches for the user. shred all rubbish or LSA cookies... (0 Replies)
Discussion started by: 1in10
0 Replies

2. UNIX for Advanced & Expert Users

shred() not working on ext* file systems

When vfat format my128kb flash drive, shred works fine. But, when I format it using ext2 or ext3, shred() exits with this error: shred: /dev/sdb1: pass 1/1 (random)... shred: /dev/sdb1: error writing at offset 12288: Invalid argumentAnyone know what is going on? (0 Replies)
Discussion started by: codecellar
0 Replies

3. UNIX for Advanced & Expert Users

shred multiple options

I've created a wxpython gui for the shred command. I can successfully mix and match all the shred options except two: -size and --random-source. (Man page definitions below). -size and --random-source seem to only work when they are used as the sole option passed. For example, I can zero a... (0 Replies)
Discussion started by: codecellar
0 Replies

4. UNIX for Dummies Questions & Answers

Shred utility

Hi experts. We have a HP-Unix server, we deleted some files and folders from the server. Is there any utilities that we can use to ensure those deleted files and folders are not recoverable? We used rm command to delete these files. Cheers, RT:confused: (1 Reply)
Discussion started by: robtseng
1 Replies

5. Shell Programming and Scripting

BASH : Find files by date and shred them

Good afternoon everyone, For security reasons, I need to delete files on a HDD I have to send after the ending of a demo. I need to find all the files which not end by ".log" and which have been created for 45 days. After getting the file list, I would like to use the shred command. It... (2 Replies)
Discussion started by: cbismuth
2 Replies
Login or Register to Ask a Question