Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am just curious how are the others deleting files on command line. After a (well known) mistake trying to remove all junk files by
$ rm foo*
but instead typing
$ rm foo *
in my working directory, I wrote two scripts: the first one moves all requested files into ~/.Trash, and the second one, invoced by cron, purges trash for files more than a week old. Now it seems to me such a natural way that I would like to have something similar as default on every computer. I think delayed removing is the only way to make rm safe, different confirmations you just get used to answer "y" every time.
Well, my opinions is that it is only marginally safer to use 'rm -i', because one gets used always answering "y". At least this was what happened to me using mc's "safe delete".
I have a great way to remove files.
I use rm, but instead of tapping Enter after I write the command, I always breathe in and out before pressing Enter.
It has never failed me and saved my a** a couple of times.
I usually do my file manager through KDE's Konqueror, but you do have a point on making the rm command safer by aliasing it with a "move" script of some sort. I've lost my arse a couple times from rm'ing what I thought was one folder, when it was another.
Here are my scripts. I have never thought they can be of interest for the others, so here are not many comments and some texts are in Estonian. The first one, for deleting (called del):
Code:
#!/bin/bash
KQNTS=$HOME/.Trash
case $# in
0)
echo "Viga: põle meskit kaotada..."
exit 1
;;
esac
test -d $KQNTS || mkdir $KQNTS
echo "Niisugused fail(id) tqstetud kataloogi $KQNTS:"
for fail; do
if [ -e "$fail" ]; then
touch "$fail"
PATHNAME=${fail%/*}
FNAME=${fail#$PATHNAME/}
if [ "x$FNAME" == "x" ]; then
# $file ends with '/'. Need to do differently:
PATHNAME=${fail%/*/}
FNAME=${fail#$PATHNAME/}
fi
# FNAME is the filename (or the dir name to be removed) w/o path
if [ -d "$KQNTS/$FNAME" ]; then
echo "removing $KQNTS/$FNAME"
rm -rf "$KQNTS/$FNAME"
fi
mv "$fail" $KQNTS
echo " $fail"
else
echo " $fail is missing"
fi
done
There script is long because otherwise you just cannot mv a directory into trash if there already exists one with the same name. You have to rm -rf it before. And you have to check whether it was executed as
$ del path/dir
or
$del path/dir/
The second one, for purging trash:
Code:
#!/bin/sh
kodo=$HOME
kodojuur=${kodo%/*}
KQNTS=.Trash
if test "x$1" = x; then
kodojuur="/home"
kasutajad=$(ls $kodojuur)
else
kasutajad=$1
fi
for kasutaja in $kasutajad
do
# Kui kaua hoida rampsu prugikastis
aeg="+7"
if [ "$kasutaja" = "tiina" ]; then
aeg="+20"
fi
if test -d $kodojuur/$kasutaja/$KQNTS; then
find $kodojuur/$kasutaja/$KQNTS -maxdepth 1 -ctime $aeg -exec rm -rf {} \;
fi
done
it checks whether it is invoked for a specific user of for all users, gives 20 day expiration delay for my wife and only 7 for the others ;-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.