Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Also, is there a way to remove non-empty directories without using rm? I want to alias my script to rm when its done and if I use rm it will simply call my script.
To remove non-empty directories, you could still use rm, but by calling /bin/rm instead of directly rm (this will not call your alias).
Concerning your first post, is writePro a function you have written?
If so, please post it.
To remove non-empty directories, you could still use rm, but by calling /bin/rm instead of directly rm (this will not call your alias).
Concerning your first post, is writePro a function you have written?
If so, please post it.
HTH.
Hi here is my full script:
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
writePro $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
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 am trying to emulate the "rm" command and then set this script as an alias to rm and simply move files rather than delete then.
I am struggling with the directories, I can handle files fine.
function checkExisting ()
{
if [ -d "$TRASH/$1" ] || [ -f "$TRASH/$1" ]; then
shred -fu $TRASH/$1/* 2>/dev/null
rmdir $TRASH/$1
writePro $1
else
writePro $1
fi
}
but that will only shed for one directory, if there is a directory with a directory and so on it only shreds the contents of the first directory, can I improve it to do this recursively? if so how to I do it?
Are you using shred to avoid the problem you stated in your second post ?
Quote:
Also, is there a way to remove non-empty directories without using rm? I want to alias my script to rm when its done and if I use rm it will simply call my script.
If so, I said in my earlier post that you could still use rm (and not shred), but by calling /bin/rm instead of rm. By calling /bin/rm instead of rm, the command that will be executed is really /bin/rm, whereas if you call rm after you aliased it to your script, your script will be executed instead of rm.
Anyway, if you really intend to use shred, you can delete all files recursively using this command :
Quote:
find -type f -exec shred -u '{}' \;
and then remove the tree of empty directories by calling /bin/rm -rf *
Are you using shred to avoid the problem you stated in your second post ?
If so, I said in my earlier post that you could still use rm (and not shred), but by calling /bin/rm instead of rm. By calling /bin/rm instead of rm, the command that will be executed is really /bin/rm, whereas if you call rm after you aliased it to your script, your script will be executed instead of rm.
Anyway, if you really intend to use shred, you can delete all files recursively using this command :
and then remove the tree of empty directories by calling /bin/rm -rf *
HTH.
Hi, thanks for the info, much appreciated.
Can I use the second commands to remove directories with rmdir? I tried
find -type d -exec rmdir -u '{}' \deleted;
but it didn't work, any idea if this is possible (without /bin/rm)?
anyone help with the above post? can I use find and rmdir to remove empty directories within empty directories after using shred in one of the previous posts to delete the files?
I managed to fix that, instead of using $@ I am now using a for loop to processs each argument
individually, can anyone help with the recursive removal of empty directories?
Also in this code, kindly recommended by hfawzy where do I specify the folder?
Code:
find -type f -exec shred -u '{}' \;
do I do it like this:
Code:
find -type f -exec shred -u '{}' $TRASH;
And does anyone know more about using rmdir as above like shred is being used to remove recursivley?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.