LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   looping through linux directories (https://www.linuxquestions.org/questions/linux-newbie-8/looping-through-linux-directories-932147/)

brcjacks 03-01-2012 08:13 AM

looping through linux directories
 
I need to loop through a long list of directories checking the files in each of these directories for file names that include grp and delete the matching files.

schneidz 03-01-2012 09:05 AM

what have you tried so far ?
Code:

find . -name "*grp*" -exec mv '{}' /whatever/floats/your/boat \;

brcjacks 03-01-2012 09:26 AM

I haven't tried anything yet. I used rsync to try to bring all the files over that I wanted to the new dir and that worked but it left behind that I copied and I needed them to me deleted. I guess, at this point, I really just need to iterate through the source directory structure and delete the files with grp in the name. Sorry if I started this off in the wrong direction.

I was thinking that I could write a short script like this:

my @dirs = ??????
foreach my $sub_dir(@dirs) {
system("cd $sub_dir");
system("rm -f */*grp*");
system("cd ..");
}

I just don't know how to populate the array with the list of dirs. I only need to go 1 level deep on the hierarchy.

schneidz 03-01-2012 09:31 AM

no prob... people usually post the code tidbits that are causing them problems then responders can correct the issues.

i would use find to generate a list of the relative paths of the files i want to move and go from there.

brcjacks 03-01-2012 09:41 AM

trying to use your find example.
find . -name "*grp*" -exec rm -i-r

wanted to find and remove anything with grp in the name starting in the current dir and recursing through all dirs asking before deleting.
I get error::: find: missing argument to `-exec'

schneidz 03-01-2012 09:44 AM

Code:

find . -name "*grp*" -exec rm -i -r
i think you are missing a space between -i and -r.

also this '{}' tells find to use that as the filename argument.

not sure why you need to recurse with find


what does this yeild for you ?:
Code:

find . -name "*grp*"

brcjacks 03-01-2012 09:46 AM

same error message.
find: missing argument to `-exec'

suicidaleggroll 03-01-2012 10:06 AM

Quote:

Originally Posted by brcjacks (Post 4616023)
same error message.
find: missing argument to `-exec'

Throw a {} \; onto the end, eg:
Code:

find . -name "*grp*" -exec rm -i -r {} \;

brcjacks 03-01-2012 10:10 AM

still the same.

just to be sure, this should work right from the command line, right?

brcjacks 03-01-2012 10:16 AM

Quote:

Originally Posted by schneidz (Post 4616022)
Code:

find . -name "*grp*" -exec rm -i -r
i think you are missing a space between -i and -r.

also this '{}' tells find to use that as the filename argument.

not sure why you need to recurse with find


what does this yeild for you ?:
Code:

find . -name "*grp*"

This looks like to returned the correct set of files

suicidaleggroll 03-01-2012 10:19 AM

Quote:

Originally Posted by brcjacks (Post 4616050)
still the same.

just to be sure, this should work right from the command line, right?

Yes. Could you copy and paste the command and the error into code tags? Like this:
Code:

[eggroll@picard test]$ ls
blah  blah.grp  grp  grp.blah
[eggroll@picard test]$ find . -name "*grp*" -exec rm -i -r {} \;
rm: remove regular empty file `./blah.grp'? y
rm: remove regular empty file `./grp'? y
rm: remove regular empty file `./grp.blah'? y
[eggroll@picard test]$ ls
blah


brcjacks 03-01-2012 10:22 AM

Code:

find . -name "*grp*" -exec rm
find: missing argument to `-exec'


schneidz 03-01-2012 10:28 AM

Quote:

Originally Posted by schneidz (Post 4616022)
Code:

find . -name "*grp*" -exec rm -i -r
i think you are missing a space between -i and -r.

also this '{}' tells find to use that as the filename argument.

not sure why you need to recurse with find


what does this yeild for you ?:
Code:

find . -name "*grp*"

the important blip is highlighted in red.

suicidaleggroll 03-01-2012 10:33 AM

Quote:

Originally Posted by brcjacks (Post 4616070)
Code:

find . -name "*grp*" -exec rm
find: missing argument to `-exec'


In post #8 I suggested you add "{} \;" onto the end, in post #9 you said you did and nothing changed, but they're not there in this example.

Please run the following command and post the output
Code:

find . -name "*grp*" -exec rm -i -r {} \;

brcjacks 03-01-2012 11:58 AM

ok, I'm a complete ass.
I thought the semi colon was a type so I left it off.
just ran it again and it looks like it worked.

thank you for your patients.
damn, I hate being a noob some times.

can you do my one last favor and explain what the {} \; means?


All times are GMT -5. The time now is 01:44 AM.