rm question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers rm question
# 1  
Old 12-02-2006
rm question

I want to use the rm command to delete files in a directory. But I want to be able to set a variable that says how many arguments it will delete. So for instance, if I set a variable to null,.. (i.e. MAXFILES="") then it will just delete all the arguments. But if I set MAXFILES=2 for instance, then it will delete only 2 arguments. I'm writing a program to do this, and there are other specifications too,.. but I think I have most of it figured out but I can't seem to get past this one hurdle. Any thoughts? Great thanks in advance to any advice on this. Smilie

Btw,... before I start writing the program, I want to be able to practice setting the variable and then using that variable just using the command at the command prompt. In other words, I want to be able to just do something like...

$ MAXFILES=""
$ rm arg1 arg2 arg3
$

or

$ MAXFILES=2
$ rm somethinggoeshere (and it deletes the files per the number of arguments defined in the variable above.)

This way it will hopefully sink in through my thick cranium on how this is working. Smilie
# 2  
Old 12-02-2006
Let me get this straight.
You want to set a variable that will limit the number of files you can delete in the command line, ex:
if you set MAXFILES to 3, and you type something like
Code:
 delete file1 file2 file3 file4 file5

only file1 file2, and file3 will be deleted.

Is that what you are looking for?
# 3  
Old 12-02-2006
Quote:
Originally Posted by System Shock
Let me get this straight.
You want to set a variable that will limit the number of files you can delete in the command line, ex:
if you set MAXFILES to 3, and you type something like
Code:
 delete file1 file2 file3 file4 file5

only file1 file2, and file3 will be deleted.

Is that what you are looking for?
Yeah. Something like that. Here is the code I have so far for the program. It kinda sorta works but either I'm doing something wrong or it's missing something or I dunno.


Code:
#!/bin/bash
# Program: myrm

if [ "$#" -le 0 ]
then
        echo "Not enough arguments"
        echo "Usage: myrm [arg1] [arg2] etc..."
        exit 1
fi

MAXFILES=""
FILECOUNT=$(ls | wc -l)

if [ -n "$MAXFILES" ]
then

        if [ "$FILECOUNT" = "$MAXFILES" ]
        then
                for i
                do
                        rm -rf $1
                        shift
                done

        elif [ "$MAXFILES" > "$FILECOUNT" ]
        then
                echo "Would you like to delete these files?"
                read choice

                case "$choice"
                in
                        y)      for i; do rm -rf $1; shift; done;;
                        n)      echo "Thank you, have a nice day";;
                        *)      echo "Invalid choice"
                                exit 2;;
                esac
        fi

elif [ -z "$MAXFILES" ]
then
        for i in $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
        do
                rm -rf $1
                shift
        done
        exit 0


fi

# 4  
Old 12-02-2006
two comments.

1. Give the full path to the rm command, it may be aliased.
2. Test for wildcards, if you pass in a quoted wildcard, it may not behave as expected.
# 5  
Old 12-02-2006
why do you want to do this ?

rm file1 file2 will delete file1 and file2 , rm * will delete everything. I can not undestand your requirement or this is a stupid requirement.

Regds,

Kaps
# 6  
Old 12-03-2006
Quote:
Originally Posted by kapilraj
why do you want to do this ?

rm file1 file2 will delete file1 and file2 , rm * will delete everything. I can not undestand your requirement or this is a stupid requirement.

Regds,

Kaps
Yeah,.. I guess when we are first learning something people give us stupid questions to figure out. LOL. Believe me,.. I have way better things I'd rather spend my Saturday night doing but I have to get this finished. Also,.. just remember,.. this is probably really easy for you because you are probably an expert with this but I am just a n00b so there's a lot that doesn't make sense to me yet. But I'm getting better and better at it everday. Smilie

Well, anyway,.. I think I have it all figured out for the most part. Now I just have to make it so that if the number of arguments exceed the number specified in MAXFILES, then it will ask me to confirm the deletion. That shouldn't be too hard. Anyway,.. thanks all for the great help. Smilie

Here's my revised version which works the way I need it to for the most part.

Code:
#!/bin/bash
# Program: myrm

if [ -z "$MAXFILES" -a "$#" -ge 1 ]
then
        for MAXFILES in $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
        do
                echo "deleted$MAXFILES"
                shift
        done
        exit 1
fi

if [ -n "$MAXFILES" -a "$#" -ge 1 ]
then
        MAXFILES="$*"
        rm -rf $MAXFILES
fi

Then I just do something like this:

Code:
$ MAXFILES=56 ./myrm f1 f2 f3 f4
# then it deletes the 4 files because they are less than the amount specified in MAXFILES.
or
$ MAXFILES= ./myrm f1 f2 f3 f4

blah blah blah

Also,.. I have just an echo command in the top section just to make things easier. I'll go back and put the correct commands in there later. I just mainly needed to figure out HOW to do it and I think I got it now.
# 7  
Old 12-03-2006
Whoo hoo! Finally after working on this all day I finished it. I feel so accomplished now. Smilie

Code:
#!/bin/bash
# Program: myrm

if [ "$#" -le 0 ]
then
        echo "Not enough arguments"
        echo "Usage: myrm [arg1] [myarg2] etc.."
fi

if [ -z "$MAXFILES" ]
then
        for i in $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
        do
                rm -rf $i
                shift
        done
        exit 1
fi

x=$(ls | wc -l)

if [ -n "$MAXFILES" -a "$#" -lt "$MAXFILES" ]
then
        rm -rf $*
else
        echo "Remove $# files? (y/n)"
        read choice

        case "$choice"
        in
                y)      rm -rf $*;;
                n)      echo "Ok, I will not delete any files then";;
                *)      echo "Invalid option"
                        exit;;
        esac
fi

And here is how it works:

Code:
$ ls
f1  f10  f11  f12  f13  f14  f15  f2  f3  f4  f5  f6  f7  f8  f9  myrm
$ MAXFILES= ./myrm f*
$ ls
f5  f6  f7  f8  f9  myrm
# MAXFILES is set to null and deletes up to 10 files even though I specified more.

$ MAXFILES=2 ./myrm f1 f2 f3 f4 f5
Remove 5 files? (y/n)
y
$ ls
f10  f11  f12  f13  f14  f15  f6  f7  f8  f9  myrm
# MAXFILES is set to 2 but I specified 5 files to delete. So it asks me y/n to confirm.

$ MAXFILES=5 ./myrm f1 f2 f3
$ ls
f10  f11  f12  f13  f14  f15  f4  f5  f6  f7  f8  f9  myrm
$ 
# MAXFILES is set to 5 and since I have specifed less than 5 arguments, it just does it without question.

Anyway,... case closed. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. AIX

df question

Hi, Can anyone please explain a little about df command. I have following question: Following example is showing % used as 4 where as total free blocks are 15.46 out of 16.00 MB blocks. df -m /test Filesystem MBblocks Free %Used Iused %Iused ... (5 Replies)
Discussion started by: itsabhi9
5 Replies

2. Hardware

question

How to add 3 moniters to a pc set up? (2 Replies)
Discussion started by: clicstic
2 Replies

3. Shell Programming and Scripting

question about wc

Hey my friend was asking me if i knew a way to cout how many different words in a file. I told him no not off hand, but i was thinking about it, and i started to wonder also. I imagine this is probably pretty simple im just missing something, I keep confusing my self with how you would compair and... (16 Replies)
Discussion started by: yodadbl07
16 Replies

4. UNIX for Dummies Questions & Answers

Question

hallo, ik heb hier een vraagje. hoeveel gebruikers kunnen er op 1 unix systeem. hopelijk antwoorden golle nu want ik moet da vinde voor school en die leerkracht zaagt. :p groetjes eu wacht wa was mijne nick ah ja vraagje groetjes vraagje ik kan geen engels dus antwoord liever in het... (1 Reply)
Discussion started by: vraagje
1 Replies

5. UNIX for Dummies Questions & Answers

mv question

Hello if I like to move file from defined directories system to new directory that not contained any directories system structure . But I like to create the same file system structure as source directory for example : I have 2 directories: foo1 and foo2 foo1 have directories and foo2 have... (2 Replies)
Discussion started by: umen
2 Replies

6. Solaris

vi question

Im trying to edit a 113 meg file in VI and i get the error TMP FILE TOO LARGE. Does someone know how to get around this? Thanks! (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

7. Programming

Yet Another Question

Now that I have getch() to work, I have yet another problem. BTW, thank you for answering these questions, I do ask a lot, only because I am eager to know, what is a board used for anyways :) Ok, he's the problem... #include iostream.h #include conio.h int main() { char movement; ... (2 Replies)
Discussion started by: mbolthouse
2 Replies
Login or Register to Ask a Question