views:

39

answers:

1

Is there a simple bash executable program (SIMPLE!!! in the repositories!) to iterate the file tree in a specified order (infix, prefix, postfix whatever) while executing a function on the files?

Specifically i want to make a large list of the names of files on the root of one or more dirs. I could do this on java easy (in fact i already have the function) but i want to do it in bash for some reason). I'm not interested in code. I want to use a program that takes care of that for me.

+2  A: 

You could use

find . -type f -exec cat {} \;

...to find all files -type f in the current directory ., and show the contents of each one cat {} \;. Or you could use tree if you want a tree-like output (this one is not installed by default though).

Alberto Zaccagni
Cool that nice thanks.
i30817
Generally, you're better off using the "+" suffix for the -exec command rather than ";" because it runs the command far fewer times with a large argument list instead of running it once per file with a single argument. When you're dealing with 10,000s or 100,000s of files, this can make quite a difference in execution speed. (Some old, non-GNU find commands don't support "+")
Adrian Pronk
@Adrian Pronk: wow, thanks, did not know about that :) Do I still need to escape that?
Alberto Zaccagni
No, the `+` does not need to be escaped.
Dennis Williamson
I somehow prefer to separate the find process from the execution, so I usually end up using find+xargs.
tokland
@tokland: Then you have to worry about file names containing spaces and whether your command should run if no files are found. Spaces can be dealt with using find ... -print0 | xargs -0 ... and not running the command for an empty list with xargs -r
Adrian Pronk
@Adrian. Yeah, those are sensible concerns, but easily overcome using sound aliases, i.e.: 'xargs -r -d"\n"' (I call that xargsn). That's it, 1) by default don't run empty commands and 2) feed by lines, not by words (granted, \n is also valid in filenames, but not really used in practice).
tokland
@tokland: +1: I hadn't seen the -d"\n" option to xargs before. I like it!
Adrian Pronk