Results 1 to 6 of 6

Thread: Help with find | grep | rm -r

  1. #1
    Join Date
    Apr 2012
    Beans
    6

    Angry Help with find | grep | rm -r

    Hi guys,

    I've got a large project which I need to wipe out a certain subset of folders and all of their contents.

    I'm trying to use:

    sudo find | grep test- | rm -r

    I get the files I'm looking for but I get a permission denied error when the rm attempts to occur. As I understand this it's because the rm is not actually being executed as root.

    I've seen some stuff with :

    sudo "echo 'find | grep | rm|' " >> sudo, and variations there of but they aren't working.

    I'm hoping to get a work around?

    Thanks everyone!

  2. #2
    hakermania's Avatar
    hakermania is offline Τώρα ξέρεις τι γράφω εδώ!
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    1,705
    Distro
    Ubuntu Development Release

    Re: Help with find | grep | rm -r

    Welcome to the forums!

    The command 'find' can accept as argument another command to execute on the found files. An example in your case would be:
    Code:
    find . -name "*test*" -exec rm -r {} \;
    This will delete all the files&folders that contain the word 'test' inside their filenames. But, be extremely careful with this, it will delete your files permanently.

    So, use with caution.

    If you need super user permissions in order to delete these files, then run the above 'find' command with sudo.

  3. #3
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Help with find | grep | rm -r

    Quote Originally Posted by hakermania View Post
    Welcome to the forums!

    The command 'find' can accept as argument another command to execute on the found files. An example in your case would be:
    Code:
    find . -name "*test*" -exec rm -r {} \;
    This will delete all the files&folders that contain the word 'test' inside their filenames. But, be extremely careful with this, it will delete your files permanently.

    So, use with caution.

    If you need super user permissions in order to delete these files, then run the above 'find' command with sudo.
    Dang! Beat me to the punch!

    I'll just add if there are not too many files and you want to check each as it's being deleted, you can add the interactive option to rm:

    Code:
    find . -iname "test*.txt" -exec rm -ri {} \;
    If you want to do a "dry run" to see what would be deleted, then you can use the 'echo' that you had suggested earlier:

    Code:
    find . -iname "test*.txt" -exec echo rm -r {} \;

  4. #4
    Join Date
    Dec 2008
    Location
    Research Triangle, NC
    Beans
    Hidden!
    Distro
    Xubuntu

    Re: Help with find | grep | rm -r

    What I always use, since I can test before hand what files its actually finding is something like the following:

    find . -name '*mysearch*' > out

    I review out, if it looks okay, I then use:

    find . -name '*mysearch*' | awk '{ print $1; system("sudo rm -f " $1); }'

    This will print and then remove each file that is piped into the awk command.

    USE with extreme caution.

    There are many other ways of doing this, I find piping it into awk gives me more control.
    from nowhere, and yes it hurts
    GIGABYTE z390 AORUS PRO, Intel i7-8700K (6 cores @ 3.7Ghz)
    32GB Ram, NVIDIA GeForce RTX 2080 Ti

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Help with find | grep | rm -r

    why not use -delete flag and skip piping entirely?

    Code:
    find ..... -print -delete
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    Apr 2012
    Beans
    6

    Re: Help with find | grep | rm -r

    Thanks everyone, got it working, much appreciated!

Tags for this Thread

Bookmarks

Posting Permissions

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