Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
i have a boot up vscanner i have been scripting and one of the commands i use is a find and remove command, but i want to know if there ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90

    script for find command

    i have a boot up vscanner i have been scripting and one of the commands i use is a find and remove command, but i want to know if there is a way to make a script that can be called like /script/command -var that would then remove -var with: find / -iname "var" -exec rm -rv {} \; but the -var could change just by changing the name w/o editing the script. any ideas?

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Something like:

    Code:
    #!/bin/bash
    
    if [ $# −ne 1 ]
    then
      echo "Usage: `basename $0` file-name"
      exit 1
    fi
    
    find / -iname "$1" -exec rm -rv {} \;
    Regards

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    thanks for the help!

  4. #4
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    hey, it said it is expecting a binary on line 3. any ideas why and how to fix?

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    When I copied the script to test it myself, the "-" from "-ne" vanished. Add it back.

    "-ne" is a binary operator that means "is numerical argument 1 not equal to numerical argument 2?" (as opposed to string arguments).
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    ok i got it, works beautiful. bye bye windows viruses . thanks!


    (heh, id get rid of windows too if the customer would allow it!)

  7. #7
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    ok what if i need to give it a starting location too? like:

    "/run/script location file", because i am getting a weird error like the drive is failing trying to start off of /

    device i/o error
    bad bread(block 51 failed
    or something similar.

    the computer has two ntfs partitions on ntfs-3g but one is in an extended partition if any of this matters.

  8. #8
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The first argument to find is the starting directory. It will take the subtree that starts there and traverse it.

    You could specify that directory by using another commandline parameter to the script. This one, not surprisingly, would be called $2.

    This is the best resource for Bash scripting that I have ever found:
    http://www.tldp.org/LDP/abs/html/

    You might read this section, under "positional parameters":
    http://www.tldp.org/LDP/abs/html/othertypesv.html
    DISTRO=Arch
    Registered Linux User #388732

  9. #9
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    um... learning, but still really not good and i only have internet @ work. show me an example with this one so i can learn based on what changes?

  10. #10
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Alrighty. Well, as you will see in find's man page, the first argument to find is the subtree to start at. So the question here is "how are we going to specify this?" This is done using commandline arguments: we can pass the script arguments, and it will use them. We are already doing this: $1 is the first parameter. $2 is the second. So the new script would be:
    Code:
    #!/bin/bash
    
    if [ $# −ne 2 ]
    then
      echo "Usage: `basename $0` file-name starting-directory"
      exit 1
    fi
    
    find "$2" -iname "$1" -exec rm -rv {} \;
    What have we done? Well $# is the number of commandline arguments. We are requiring 2. We have also replaced '/' with '$2' in the call to find. This is because $2 will have the starting directory stored in it.

    Does this all make sense?
    DISTRO=Arch
    Registered Linux User #388732

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...