LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2012, 08:13 AM   #1
brcjacks
LQ Newbie
 
Registered: Mar 2012
Location: Atlanta
Posts: 10

Rep: Reputation: Disabled
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.

Last edited by brcjacks; 03-01-2012 at 10:17 AM.
 
Old 03-01-2012, 09:05 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried so far ?
Code:
find . -name "*grp*" -exec mv '{}' /whatever/floats/your/boat \;

Last edited by schneidz; 03-01-2012 at 09:07 AM.
 
Old 03-01-2012, 09:26 AM   #3
brcjacks
LQ Newbie
 
Registered: Mar 2012
Location: Atlanta
Posts: 10

Original Poster
Rep: Reputation: Disabled
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.

Last edited by brcjacks; 03-01-2012 at 09:29 AM.
 
Old 03-01-2012, 09:31 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.

Last edited by schneidz; 03-01-2012 at 09:32 AM.
 
Old 03-01-2012, 09:41 AM   #5
brcjacks
LQ Newbie
 
Registered: Mar 2012
Location: Atlanta
Posts: 10

Original Poster
Rep: Reputation: Disabled
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'
 
Old 03-01-2012, 09:44 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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*"

Last edited by schneidz; 03-01-2012 at 09:48 AM.
 
1 members found this post helpful.
Old 03-01-2012, 09:46 AM   #7
brcjacks
LQ Newbie
 
Registered: Mar 2012
Location: Atlanta
Posts: 10

Original Poster
Rep: Reputation: Disabled
same error message.
find: missing argument to `-exec'
 
Old 03-01-2012, 10:06 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by brcjacks View Post
same error message.
find: missing argument to `-exec'
Throw a {} \; onto the end, eg:
Code:
find . -name "*grp*" -exec rm -i -r {} \;
 
1 members found this post helpful.
Old 03-01-2012, 10:10 AM   #9
brcjacks
LQ Newbie
 
Registered: Mar 2012
Location: Atlanta
Posts: 10

Original Poster
Rep: Reputation: Disabled
still the same.

just to be sure, this should work right from the command line, right?
 
Old 03-01-2012, 10:16 AM   #10
brcjacks
LQ Newbie
 
Registered: Mar 2012
Location: Atlanta
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
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
 
Old 03-01-2012, 10:19 AM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by brcjacks View Post
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
 
Old 03-01-2012, 10:22 AM   #12
brcjacks
LQ Newbie
 
Registered: Mar 2012
Location: Atlanta
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
find . -name "*grp*" -exec rm
find: missing argument to `-exec'
 
Old 03-01-2012, 10:28 AM   #13
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by schneidz View Post
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.
 
Old 03-01-2012, 10:33 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by brcjacks View Post
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 {} \;

Last edited by suicidaleggroll; 03-01-2012 at 10:34 AM.
 
1 members found this post helpful.
Old 03-01-2012, 11:58 AM   #15
brcjacks
LQ Newbie
 
Registered: Mar 2012
Location: Atlanta
Posts: 10

Original Poster
Rep: Reputation: Disabled
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?

Last edited by brcjacks; 03-01-2012 at 12:41 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could someone help me with looping in vb cobrien Programming 9 11-12-2010 08:17 AM
looping in script fw12 Programming 3 11-04-2009 03:48 PM
looping images? Cording44 Linux - Newbie 1 05-05-2009 09:34 AM
Not able to do looping in linux Amey Joshi Linux - Newbie 7 08-13-2008 03:25 AM
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration