The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
BASH : Find files by date and shred them cbismuth Shell Programming and Scripting 2 06-13-2005 08:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-17-2006
Registered User
 

Join Date: Jul 2006
Posts: 42
Help using shred instead of rm

Hi,

I have this script which runs pretty well bar a couple of problems, I am trying to emulate the rm command and this means I can't use rm directory, I inted to alias my script to rm and then use mv to move files to a trash folder rather than delete forever.

The problem: when I use mv to move a directory into my deleted folder where this directory all ready exists I get an error, I plan to shred all the files and then I want to use rmdir to remove all the empty directories in the tree.

My script is here

Code:

#!/bin/bash
# Ai program to emulate the "rm" command in UNIX.
# Created by Michael Kelly
# Last revison 12/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 verbose () {
if [ "$FLAG_V" = "v" ] ; then
echo "removing \`$1'"
fi
}

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

function writePro () {
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 checkExisting ()
{
if [ -d "$TRASH/$1" ] || [ -f "$TRASH/$1" ]; then


     shred -fp $TRASH/$1 2>/dev/null
     rmdir $TRASH/$1 2>dev/null
     writePro $1

else
     writePro $1

fi
}

function force () {

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


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

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

}

# Getops

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`

if ! [ -d "$HOME/deleted" ] ; then
     mkdir $HOME/delete

else
   for ARG in $*
   do
   errors $ARG
   done
fi
if [ $# -eq $NO_ARGS ] ; then
errors
fi
I have highlighted the problem function in bold, I want to shred files recrusively and then use rmdir the same way for the empty directories.

Someone recommended this command:

Code:
find -type f -exec shred -u '{}' \;
which shreds recursivley but how do I specify which point to start shreding from? when I used this before it wiped out every file in my home directory onwards.

Once I have that sorted I want to use a similar command but replacing rmdir with shred, can anyone advise me away to it?

Regards

Jack
Reply With Quote
Forum Sponsor
  #2  
Old 07-17-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
why not just

find $TRASH/$1 -type f -exec shred -u '{}' \;
Reply With Quote
  #3  
Old 07-17-2006
Registered User
 

Join Date: Jul 2006
Posts: 42
Cheers!

So how would I use rmdir in a similar way to remove the empty directories?

Jack
Reply With Quote
  #4  
Old 07-17-2006
Registered User
 

Join Date: Jul 2006
Posts: 42
Quote:
Originally Posted by Hitori
why not just

find $TRASH/$1 -type f -exec shred -u '{}' \;

Also, I am kind of new to Unix could you explain this command a little better, someone else recommended it and I dont really understand the -exec and '{}' \

Thanks again

Jack
Reply With Quote
  #5  
Old 07-17-2006
Registered User
 

Join Date: Jul 2006
Posts: 42
I ran it in my script and it says missing argument for -exec
Reply With Quote
  #6  
Old 07-17-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
Quote:
Originally Posted by jack1981
I ran it in my script and it says missing argument for -exec
do not forget <space>:

find $TRASH/$1 -type f -exec shred -u '{}'<space>\;

This means search (recursive) in directory $TRASH/$1 for files (-type f) and execute for each file which was found command shred -u '{}' where macro {} will be replaced with the file name

You can use -type d to search for directories

Check out find(1) (man 1 find)
Reply With Quote
  #7  
Old 07-17-2006
Registered User
 

Join Date: Jul 2006
Posts: 42
Hi, thanks again.

I still get the error however, even with a space, my code is like so:

find $TRASH/$1 -type f -exec shred -fu '{}' \ 2>/dev/null

any ideas why?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0