LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2005, 03:16 AM   #1
matthewa
Member
 
Registered: Mar 2005
Distribution: Slackware 10.1
Posts: 123

Rep: Reputation: 15
My remove script


I wrote up a little script that I use to remove large amounts of files with. It's not much now but I was hoping to get some feedback on this thing. Maybe some ways to make it better?

#!/bin/bash

rm -rf \
<path to file you want removed> \
<path to file you want removed? \



One thing that is really a pain is that you actually have to paste in a list of the files you want removed and then append a "\" to the end of each one of them. I would really like to find a way to just paste in the files and run the script to remove the files, without all the work! Another thing that I thought about was adding "updatedb" to update slocates database after it clears all the files.

Any feedback would be appreciated. thanks
 
Old 07-12-2005, 04:10 AM   #2
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
you don't need the backslash after every line, write the script the same as you would write it on the command line, you put a different command on a new line, if you want more than one command on a line then separate those commands with a semi-colon

Search google for tutorials to write bash scripts, there are millions lying around
 
Old 07-12-2005, 04:29 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
I made a little script here that you can use if you want:

Here is from when I tried it out:
Code:
$ touch /tmp/file1
$ touch "/tmp/file with space"
$ mkdir "/tmp/directory1"
$ ./removal.sh
Usage: ./removal.sh [option] <removelist>
Try ./removal.sh -h for more info.
$ ./removal.sh -v
Removal Tool v0.1
$ ./removal.sh -h
Removal Tool usage:
        -h, --help              Show this
        -v, --version           Show version
        -t, --trash             Use trashcan, do not remove
        -e, --empty             Empty trashcan

$ cat removelist
/tmp/file1
/tmp/directory1
/tmp/file with space
$ ./removal.sh --trash removelist
Trashcan set to: /home/mezzymeat/trashcan
Moving "/tmp/file1" to trashcan ...
Moving "/tmp/directory1" to trashcan ...
Moving "/tmp/file with space" to trashcan ...
$ ls ~/trashcan/
directory1/  file with space  file1
$ ./removal.sh -e
Cleaning up in /home/mezzymeat/trashcan ...
Done!
$ ls -l ~/trashcan/
total 0
And here is the code. It is simple and not optimized, so you can make a lot of improvements to it:
Code:
#!/bin/sh

if [ $# -lt 1 ] || [ $# -gt 2 ]; then
        echo "Usage: $0 [option] <removelist>"
        echo "Try $0 -h for more info."
        exit
fi;

if [ $1 == "-h" ] || [ $1 == "--help" ]; then
        echo "Removal Tool usage:"
        echo -e "\t-h, --help\t\tShow this"
        echo -e "\t-v, --version\t\tShow version"
        echo -e "\t-t, --trash\t\tUse trashcan, do not remove"
        echo -e "\t-e, --empty\t\tEmpty trashcan"
        echo ""
        exit
fi;

if [ $1 == "-v" ] || [ $1 == "--version" ]; then
        echo "Removal Tool v0.1"
        exit
fi;

if [ $1 == "-e" ] || [ $1 == "--empty" ]; then
        if [ -z $TRASHCAN ]; then
                TRASHCAN="$HOME/trashcan"
        fi;

        if [ -d $TRASHCAN ]; then
                if [ -w $TRASHCAN ]; then
                        echo "Cleaning up in $TRASHCAN ..."
                        rm -r $TRASHCAN/*
                        echo "Done!"
                        exit
                else
                        echo "! You do not write permission for $TRASHCAN"
                        exit
                fi;

        else
                echo "! $TRASHCAN does not exist."
                exit
        fi;

        echo "Cleaning up in $TRASHCAN ..."
        exit
fi;

trash=0
if [ $1 == "-t" ] || [ $1 == "--trash" ]; then
        if [ -z $TRASHCAN ]; then
                TRASHCAN="$HOME/trashcan"
        fi;

        if [ ! -d $TRASHCAN ]; then
                echo "! $TRASHCAN does not exist."
                exit
        fi;
        echo "Trashcan set to: $TRASHCAN"

        trash=1
fi;

cat removelist | while read entry; do

        if [ -w "$entry" ]; then
                if [ $trash == 1 ]; then
                        echo -e "Moving \"$entry\" to trashcan ..."
                        mv "$entry" $TRASHCAN
                else
                        echo -e "Removing \"$entry\" ..."
                        rm -r "$entry"
                fi;
        else
                if [ -e "$entry" ]; then
                        echo "Cannot access: $entry"
                else
                        echo "No such file or directory: $entry"
                fi;
        fi;
done;
Regads.
 
Old 07-12-2005, 05:39 AM   #4
matthewa
Member
 
Registered: Mar 2005
Distribution: Slackware 10.1
Posts: 123

Original Poster
Rep: Reputation: 15
ephracis, now thats a neat script! I have no clue as to how to use it, but believe me I'm studying it right now. Thanks for sharing!
 
Old 07-12-2005, 06:24 AM   #5
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
It is very simple. I added the trashcan functionality and put the remove-list you wanted in a seperate file. Now you just need to have a file with a list of all files/dirs you want to remove and then run something like "removal -t <file>" and it should remove the files listed.

You can improve some things:
  • The switches (-h, -t, -e) works now but if you add some more and want to have two switches (eg you add -V for verbose and want to run "-t -V" or "-Vt" the script should be changed a little bit.
  • You may add so if the trashcan-dir does not exist the script asks the user if he wants it to be created.

Have fun!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
remove text from file with script paul_mat Linux - Software 3 11-17-2005 12:21 PM
need help with script to remove all metachars from filenames BrianK Programming 5 08-20-2005 11:10 PM
Help to remove script from booting sequence sasha_baranov Linux - Newbie 1 11-18-2004 09:34 AM
How to remove a device script (permissions) michapma Linux - Newbie 3 10-24-2003 06:32 PM
Script to remove old files. cmfarley19 Linux - General 2 03-24-2003 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration