There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Linux and Unix
Tag Cloud
access acer asus bios bsod computer crash driver drivers error ethernet excel freeze gaming google gpu graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard mouse network printer problem ram registry router server slow software sound svchost.exe trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
Shell scripting help

Reply  
Thread Tools
evilmrhenry's Avatar
Member with 106 posts.
 
Join Date: Dec 2001
17-Oct-2003, 05:00 AM #1
Shell scripting help
First, the background.
I'm running on a slackware based system, KDE 3.1.

Whenever I want to delete something, I have a choice of sending it to the trash, or deleting it immediately. I don't like the trash, as I have to keep in mind to empty it regularly. Therefore, I have a *bad* habit of simply deleting the item.

The following code (hopefully) will go through the trash and delete anything that was placed in there over 180 minutes (3 hours) ago. I plan to place it in the cron-hourly directory.

Code:
#! /bin/sh
cd $HOME/Desktop/Trash/
rm | find -amin +180 -not -name .directory -not -name .
exit 0
My questions:
Will this work?
Can bad things happen if I put this in?
Did I forget something?

Normally, I would just put this in, and try it out. However, the presence of rm makes me really want to get advice first.
Squashman's Avatar
Trusted Advisor with 18,705 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
17-Oct-2003, 05:34 AM #2
Well you could always use the rm -i option. Which is interactive and will prompt you if you wan to delete anything. That way you could test it out without destroying anything.
evilmrhenry's Avatar
Member with 106 posts.
 
Join Date: Dec 2001
17-Oct-2003, 01:53 PM #3
Thanks for the tip. This version appears to work better:

Code:
#! /bin/sh
cd $HOME/Desktop/Trash/
find -amin +120 -not -name .directory -not -name . -exec rm -i {} \;
exit 0
Are there any filenames that could trigger bad reactions? Any other issues?
(I'll remove the -i when I put it into action.)
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
18-Oct-2003, 05:39 AM #4
If I understand what you're trying to do, you may be able to simplify your script somewhat by using: find -amin +120 -type f -exec rm (etc.). This specifies that only files (not directories) should be removed. If you intend to remove (sub-) directories in Trash, then you should use rm -r (possibly rm -rf) instead of just rm in your -exec command (e.g., find -amin +120 -exec rm -rf {} \;). Note that this will not remove the current or the parent directory (. or ..).

You might also consider having the script support two modes (readonly and execute); this would allow you to test/debug the script without risking unintentional removal of files. You could, for example, use something like:
Code:
#! /bin/sh
USAGE="`basename $0` [-r]"
READONLY=ON

while [ ! -z "$1" ]
do
    case "$1" in
        "-r")  READONLY=
               shift
               ;;
           *)  echo "$USAGE"
               exit 1
               ;;
    esac
done

if [ -z "$READONLY" ]
then
    COMMAND="find -amin +120 -type f -exec rm {} \;"
else
    COMMAND="find -amin +120 -type f"
fi

cd $HOME/Desktop/Trash
eval "$COMMAND"
This script defaults to "readonly" mode, and produces a list of files that would be removed (but doesn't remove them). Adding the -r option on the command line forces the script to perform the actual removal.

One additional point: if you intend to use the script in a cron job, you should make provisions for handling any output from the script (error messages, etc.). If you do not redirect output, it will be mailed to you (which may be what you want if you are running the script readonly -- this would allow you to watch what the script would do with real data over time, without the risk of deleting an important file).

Hope this helps.
__________________
The slowest component still sits at the keyboard.
evilmrhenry's Avatar
Member with 106 posts.
 
Join Date: Dec 2001
18-Oct-2003, 07:10 AM #5
I'm beginning to have second thoughts about the script as it works now. I'm wondering how this one stands up instead:

Code:
#! /bin/sh
cd $HOME/Desktop/Trash/
rm -ri .countdown/*
find -not -name .directory -not -name . -not -name .countdown -maxdepth 1 -exec mv -i {} .countdown \;
exit 0
(No, I didn't get the two lines switched. The second command moves the files, an hour passes, then the first line removes them.)

Would it be better to move all files to a specific directory, then delete them, or would it be better to go by the access date?


Some points:
It needs to act on both files and dirs.
However, only the files/dirs in the root directory of the Trash need to be looked at.


I'll think about adding a read-only/working switch later. Right now, it's staying in confirmation mode.

I need a bit more help on the output files. I tried
Code:
#! /bin/sh
cd $HOME/Desktop/Trash/
rm -ri .countdown/* > t_log.txt
find -not -name .directory -not -name . -not -name .countdown -maxdepth 1 -exec mv -i {} .countdown \; >> t_log.txt
exit 0
but that just produces a blank file. Is this working the way it should? Is there a way to record the files being moved or deleted?

Thanks for all your help.
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
19-Oct-2003, 01:51 AM #6
You can use your find command to create a list of the files and directories that will be removed. If you capture the output in a temporary file, you can remove each entry individually and email the file to yourself as a record of what happened. Would something like this do what you intend?
Code:
#! /bin/sh
FINDLOG=/tmp/findlog.$$
cd $HOME/Desktop/Trash
find -not -name .directory -not -name . -not -name .countdown -maxdepth 1 > $FINDLOG

for i in `cat $FINDLOG`
do
    rm -rf $i
done

mail -s "list of files removed on `date`" user-name < $FINDLOG
rm -f $FINDLOG
I'd recommend against storing the files to be removed in another directory, and depending on a later execution of the script to remove them.

Hope this helps.
__________________
The slowest component still sits at the keyboard.
evilmrhenry's Avatar
Member with 106 posts.
 
Join Date: Dec 2001
19-Oct-2003, 04:00 AM #7
Quote:
Would something like this do what you intend?
Just about.

I changed a few things, to make it fit in better. Basically I changed the notification from a mail to just a file placed in the Trash, (I don't use the mail feature.) and used the correct find command. (You were using the "directory" method, not the "date" method.)
Code:
#! /bin/sh
FINDLOG=$HOME/Desktop/Trash/.findlog
cd $HOME/Desktop/Trash/
find -not -name .directory -not -name .findlog -not -name . -maxdepth 1 -amin +180 > $FINDLOG

for i in `cat $FINDLOG`
do
    rm -rf $i
done

exit 0
Unless there are any bugs, I'll go with this version. Thanks for your help, everyone!
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:14 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.