Tricky recursive removal (find with grep)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tricky recursive removal (find with grep)
# 1  
Old 12-15-2011
Tricky recursive removal (find with grep)

Tricky one:

I want to do several things all at once to blow away a directory (rm -rf <dir>)

1) I want to find all files recursively that have a specific file extension (.ver) for example.
2) Then in that file, I want to grep for an expression ( "sp2" ) for example.
3) Then I want to blow away only that top level directory that contains that .ver file that contains the "sp2" match (the .ver is nested a few deep)


So for example:

A .ver file might be located here:
./KB981322/update/update.ver

and in that file I find the expression "sp2".


I want to blow away (rm -rf ) the top level directory "/KB981322/"

Other update.ver files may contain the expression "sp3" and I do not want to touch those.

I know how to do a find with grep, but not get the top level directory and pass that to rm -rf.
# 2  
Old 12-15-2011
Start with the parent directory (in your case ".") and find all directories under this (eg ./KB981322) if a find turns up a *.ver file that contains "sp2" delete the subdirectory:

Code:
PARENT=.
LOOK=sp2
 
for subdir in "$PARENT"/*
do
    if [ -d "$subdir" ]
    then
        find "$PARENT"/"$subdir" -type f -name "*.var" -print | while read file
        do
            if grep -q "$LOOK" "$file"
            then
                rm -rf "$PARENT"/"$subdir"
                break
            fi
        done
done


Last edited by Chubler_XL; 12-15-2011 at 08:28 PM.. Reason: Updated to work with spaces in file/directory names
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recursive find / grep within a file / count of a string

Hi All, This is the first time I have posted to this forum so please bear with me. Thanks also advance for any help or guidance. For a project I need to do the following. 1. There are multiple files in multiple locations so I need to find them and the location. So I had planned to use... (9 Replies)
Discussion started by: Charlie6742
9 Replies

2. Shell Programming and Scripting

Recursive Grep with replace

I have seen some useful infomation about recursive grep in one of the thread. Can it is possible to combine resursive grep and replace togather? Means I need to replace old server names in all the files with new server names as we are upgrading our applications. There are lots of files in... (2 Replies)
Discussion started by: yale_work
2 Replies

3. UNIX for Dummies Questions & Answers

Tricky GREP question..

I have some large login files that I need to extract (user)@(server) from. Where it gets tricky is that there is usually more than one '@' sign on each line(although it does have a leading space if it's not part of the (user)@(server) string), I need only the (user)@(server) section, I need only... (6 Replies)
Discussion started by: Mordaris
6 Replies

4. Shell Programming and Scripting

grep variable with tricky content

Hello, i have this issue: text="-8x7YTVNk2KiuY-PWG5zzzjB-zzw" string=-8x7YTVNk2KiuY-PWG5zzzjB-zzw echo $text | grep -v \'$string\' -8x7YTVNk2KiuY-PWG5zzzjB-zzw echo \'$string\' '-8x7YTVNk2KiuY-PWG5zzzjB-zzw' ..and ofcourse if I do like this : echo $text | grep -v $string grep: invalid... (5 Replies)
Discussion started by: black_fender
5 Replies

5. UNIX for Advanced & Expert Users

recursive grep

Hi, on AIX 6.1 , in man document for grep : -r Searches directories recursively. By default, links to directories are followed. But when I use : oracle@XXX:/appli/XXX_SCRIPTS#grep -r subject *.sh It returns nothing. However I have at least one row in a file : ... (3 Replies)
Discussion started by: big123456
3 Replies

6. UNIX for Dummies Questions & Answers

recursive grep output

I'm using this command to get a recursive grep find . -name *.i -exec grep 'blah blah' {} \; -exec ls {} \; now I would like to obtain just the list of the files and not also the line of the file. How should I change the syntax? thank you, (5 Replies)
Discussion started by: f_o_555
5 Replies

7. UNIX for Dummies Questions & Answers

Recursive grep

Hello, First time post - I have no formal unix training and could use some help with this. I have a list of strings in File1 that I want to use to do a recursive search (grep) under a specific directory. Here is an example of the string I need to search: /directory/dire... (16 Replies)
Discussion started by: upstate_boy
16 Replies

8. UNIX for Dummies Questions & Answers

recursive GREP ?

Hi! Suppose I have a directory (no symbolic links) called /WORK that contains 3 subdirectories: /A /B /C My problem is this: I want to look for a file that contains an order number. So far, I obtain what I want by doing this /home/acb% cd /WORK/A /home/acb/WORK/A% grep '093023553' *.*... (3 Replies)
Discussion started by: alan
3 Replies

9. UNIX for Dummies Questions & Answers

grep recursive directories

I am trying to locate a file or files with specific data in them. Problem is the file(s) could reside in any one of many directories. My question is. Is there a way of recursively greping directories for the file(s) with the data I am looking for. I have tried - 1. $HOME> grep 47518 | ls... (8 Replies)
Discussion started by: jagannatha
8 Replies
Login or Register to Ask a Question