Find the answer to your Linux question:
Results 1 to 10 of 10
I know how to loop over files using "for i in * do ... done;" but how do I loop over directories? What I want to do is execute a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Nov 2006
    Posts
    123

    loop over directories?


    I know how to loop over files using "for i in * do ... done;"
    but how do I loop over directories?
    What I want to do is execute a command in each directory, or possibly rename directories.

    Thanks

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi,

    You can use the find command to search for files in a directory hierarchy.

    Have a read of this man page with some examples, and see if you can gleen some helpful information from it:

    http://www.die.net/doc/linux/man/man1/find.1.html

    If not, feel free to ask more questions!

    Regards

  3. #3
    Linux Newbie
    Join Date
    Nov 2006
    Posts
    123
    mmm....bit lost still - that link looks like it searches for files in a directory, not the directories themselves, but cheers anyway...
    can anyone give an example of how you would use it to start from a parent directory, and enumerate sub directories, changing into each and executing a command?
    Thanks

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by bentaylor
    mmm....bit lost still - that link looks like it searches for files in a directory, not the directories themselves, but cheers anyway...
    can anyone give an example of how you would use it to start from a parent directory, and enumerate sub directories, changing into each and executing a command?
    Thanks
    Mmmm...., did you find that out in 4 minutes?

    Regards

  5. #5
    Linux Newbie
    Join Date
    Nov 2006
    Posts
    123
    Sorry to seem rude, no offence intended - but I didn't find anything out because you posted a link to a website you presumably happen to like, rather than the answer to the question.
    Surely the latter woul be better?
    You're not a substitute for google. If I want links, I'll search for them. If I want answers to a specific question, I'll ask on a forum such as this. You should interpret that the fact I'm asking on this forum means I've already searched for links and looked in pages such as that and for whatever reason have been unable to glean the information. Whether you believe I 'should' have been able to glean it is immaterial.

    If you don't know the answer to the specific question, as it would appear, then please don't make a reply - doing so makes it look as if the issue has been dealt with and discourages others who probably do know from replying.

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    We're trying to solve problems here, not to find the solutions.
    I don't know what you have yet, but it might be helpful to check the man page, then look on the internet for a howto/guide or an FAQ.
    To learn Linux is to read about it and find it out yourself, not by asking for the right answers.

    And.... I do know the answer to the question.

    Good luck.

  7. #7
    Just Joined!
    Join Date
    Jan 2007
    Posts
    3

    looping (recursing) directories

    well in very pseudocode terms

    1. create stack (First In First Out)
    2. push directories onto stack
    3. pull directory off of stack
    4. cd directory
    5. execute your command or whatever
    6. push directories onto stack
    7. goto 3 until stack is empty

  8. #8
    Linux Engineer
    Join Date
    Nov 2004
    Location
    home
    Posts
    796
    Code:
    for i in * ; do
      if [ -d $i ] ; then
        cd $i
        do_something
        cd ..
      fi
    done
    Something like that might just work. Check the syntax though, been a good while since I've done any shell scripting.

  9. #9
    Just Joined!
    Join Date
    Sep 2006
    Location
    Cincinnati, OH, USA
    Posts
    6
    The find command is pretty good at this:

    Code:
    find . -name "*.pdf" -exec cp {} $HOME/pdfs \;
    The -exec option allows you to do neat things. The {} takes the output of find and allows you to do something with it.

    In this example, we look for all the files with .pdf extension (recursively in all subdirectories!) and then copy them to the $HOME/pdfs directory.

    Works great for me! For other uses, the person that suggested the man pages of find was right. find has LOTS of options!

  10. #10
    Linux Newbie
    Join Date
    Nov 2006
    Posts
    123
    Quote Originally Posted by Franklin52
    We're trying to solve problems here, not to find the solutions.
    I don't know what you have yet, but it might be helpful to check the man page, then look on the internet for a howto/guide or an FAQ.
    To learn Linux is to read about it and find it out yourself, not by asking for the right answers.

    And.... I do know the answer to the question.

    Good luck.
    I suspect you think you know the answer, but couldn't actually remember it.
    There's always going to be people willing to help if you're not, so like I say - in future, if you don't know the answer or aren't willing to share it as is evident here, don't post.

    Quote Originally Posted by valan
    Code:
    for i in * ; do
      if [ -d $i ] ; then
        cd $i
        do_something
        cd ..
      fi
    done
    Something like that might just work. Check the syntax though, been a good while since I've done any shell scripting.
    Thanks - that's excellent, works great.


    Quote Originally Posted by mattypiper
    The find command is pretty good at this:

    Code:
    find . -name "*.pdf" -exec cp {} $HOME/pdfs \;
    The -exec option allows you to do neat things. The {} takes the output of find and allows you to do something with it.

    In this example, we look for all the files with .pdf extension (recursively in all subdirectories!) and then copy them to the $HOME/pdfs directory.

    Works great for me! For other uses, the person that suggested the man pages of find was right. find has LOTS of options!
    excellent - cheers, will remember that.

Posting Permissions

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