You are not logged in.

#1 2008-04-30 00:07:48

B-Con
Member
From: USA
Registered: 2007-12-17
Posts: 554
Website

An amusing account

Just a helpful tip to would-be script writers out there: It is advisable to examine your script before you run it. I learned this lesson the hard way recently.

Before the upgrade to v2.22, the Gnome desktop environment kept each user's "trash can" in the folder ~/.Trash. Performing a "soft" delete of a file merely moved the file to the trash can, where it would await permanent deletion.

Being a security-conscious person, I made a habit of shredding my deleted files when I emptied the trash. Since there exists no built-in feature to do so, I wrote my own simple script to do it for me, as follows.

#!/bin/bash

# Empty the "trash"
cd ~/.Trash
shred -n 3 *
sync
rm -rf *

It performed its elementary task perfectly.

However, when Gnome upgraded to v2.22, the location of the trash can changed (for better compliance with the FreeDesktop.org standards) to ~/.local/share/Trash. In this folder exist two folders, "files" and "info", which contain the actual deleted files and info for their undeletion, respectively.

So I had to upgrade my script to shred the new trash can. At the same time I decided to add an option to empty the trash without shredding the files because sometimes I would have large non-sensitive files (such as Linux distro ISOs) that I didn't care to spend time shredding. So I rewrote my script in less than a minute to this:

#!/bin/bash

# Empty the "trash"
if [ "$1" = "shred" ]; then
    cd ~/.local/share/Trash
    shred -n 3 files info
    sync
fi
rm -rf files info
mkdir files info
chown b-con:users files info

Since I was working on various other school/personal projects, I made the changes without thinking too much about it and quickly moved on to other tasks.

If you have any experience with programing, a quick glance through that revised script should raise a serious red flag.

That's right, my script only changed into the trash directory if I was performing the shred operation. If I was not performing the shred operation, it did not change directories and merely deleted "files" and "info" from my current directory. I had placed one line of code just one line too low.

I suppose it goes without saying that in my home directory I keep a folder called "files". This is where I store dozens of miscellaneous files, such as half-finished writings for this website. The first time I executed the script without the "shred" option while in my home directory, which was that very day, the contents of my "files" folder disappeared.

When I noticed what had happened several days later, I nearly had a heart attack. I spend a long time looking through various logs and my bash history, trying to determine what had happened. I was mad that something had the audacity to touch files in my home folder. I was ready to strace every binary on my system. However, seeing the presence of the new "info" folder eventually sparked my memory and I realized what had happened. Needless to say, I felt more than just a tad embarrassed.

I recovered just about everything from a lucky three-week-old backup I had made by chance, so all ended well. But still, that's one lesson I'll not soon forget: Always proof-read your bloody code, even just briefly.

Offline

#2 2008-04-30 00:37:46

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: An amusing account

It should be:  cd wherever || exit 1

Offline

#3 2008-04-30 02:19:14

B-Con
Member
From: USA
Registered: 2007-12-17
Posts: 554
Website

Re: An amusing account

Indeed. And it is in my local script, I've since added to it.

Offline

#4 2008-05-10 11:16:59

sujoy
Member
From: Finland
Registered: 2008-02-08
Posts: 95

Re: An amusing account

quite a lesson.

Offline

#5 2008-05-10 11:37:03

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

Re: An amusing account

I am very paranoid about scripts involving the command "rm", so I usually execute them in a sterile environment, a folder with fake (touched) files, and only after testing thoroughly I use them.

I have deleted too many important files because of my mindlessness, I think I have learnt my lesson tongue


Have you Syued today?
Free music for free people! | Earthlings

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery

Offline

#6 2008-05-10 13:41:04

11010010110
Member
Registered: 2008-01-14
Posts: 284

Re: An amusing account

When I started reading I thought of another failure mode : running the 1 st script (./Trash) after the Gnome upgrade

cd ~/.Trash fails since the folder does not exist
shred -n 3 * we are in the home folder
sync
rm -rf * we are in the home folder

This can be prevented by use of rm -rf ~/.Trash/* instead of cd and then rm -rf *

Check if the new scipt may fail like this if the folder does not exist

Offline

#7 2008-05-10 22:51:50

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: An amusing account

11010010110 wrote:

Check if the new scipt may fail like this if the folder does not exist

Brebs already brought that up, although you walk through why it's such a big deal, which may help people understand the issue. smile

brebs wrote:

It should be:  cd wherever || exit 1

Offline

#8 2008-05-11 01:00:34

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: An amusing account

Doesn't shift + Del work in Gnome's file manager? It does in Thunar, and it does in Windows wink.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#9 2008-05-11 19:48:45

11010010110
Member
Registered: 2008-01-14
Posts: 284

Re: An amusing account

Its like rm but not like shred

Offline

#10 2008-05-13 10:49:17

B-Con
Member
From: USA
Registered: 2007-12-17
Posts: 554
Website

Re: An amusing account

While we're on the topic...

It's something like 2:30am and it's been a bit of a long day. I'm doing a much needed -Syu ("much needed" here meaning "it's been a couple days") and I decided to take a quick look in /etc/ to see what .pacnew or .pacsave files I've forgotten to cleanse out as of late.

However, upon cd'ing to /etc, I remember that I took care of all the .pacnew's just a day or two ago, so I can delete any existing ones.
$ sudo rm *.pacnew

In my defense, I have to say it was 2:30am and I would never in my right mind issue a "sudo rm *" for any reason other than to wipe out a folder.

Those of you familiar with the concept of "irony" have no doubt already guessed that I hit the spacebar a wee miss-timed, and actually issued a
$ sudo rm * .pacnew
instead. Say bye-bye /etc/ . Needless to say, I got very much of nowhere without my passwd.

Thankfully I didn't touch the folders, which is where the real important stuff (xorg, etc) is.

I'm in the process of (successfully) recovering/rebuilding my /etc. I keep my laptop with virtually an identical install as on my desktop, so I've transported the /etc from my laptop to my desktop and edited various files (fstab, passwd) till it all works. Thankfully, a relatively small, small chore.

(Although now I'm wondering if some more important files couldn't have been recovered by using the "hard-line to inode" file recovery trick for processes with open files. Although I can't think of anything that keeps something like passwd or fstab constantly open.)

Gaaaaah, shoot me, please, somebody... before I do something *bad*...

(P.S. Old backup hdd failed a while ago and I never got around to replacing it. New one(s) will be ordered tomorrow. I hope I still have data to backup by the time they arrive.)

Offline

#11 2008-05-13 12:42:50

schivmeister
Developer/TU
From: Singapore
Registered: 2007-05-17
Posts: 971
Website

Re: An amusing account

finferflu wrote:

I have deleted too many important files because of my mindlessness, I think I have learnt my lesson tongue

Same here sad Now I just live with interactive rm and mv, and move stuff to a folder first before deleting at the end of the week - if i remember.

Last edited by schivmeister (2008-05-13 12:43:48)


I need real, proper pen and paper for this.

Offline

#12 2008-05-13 12:59:33

Bison
Member
From: Jacksonville, FL
Registered: 2006-04-12
Posts: 158
Website

Re: An amusing account

it would be safer to use find

find ~/.local/trash -type f | xargs rm

Offline

Board footer

Powered by FluxBB