I was looking for a simple bash one-liner for traversing a file system and printing all the files in pre-order arrangement. It seems as though find is not capable of doing this. I could try to sort the results of find, but that doesn't seem feasible either. Is there an easy way to do this in bash?
For example,
find .
.
./sub2
./sub2/e.txt
./sub1
./sub1/b.txt
./sub1/c.txt
./sub1/sub3
./sub1/sub3/d.txt
./sub1/z.txt
./a.txt
It should instead be:
.
./a.txt
./sub2
./sub2/e.txt
./sub1
./sub1/b.txt
./sub1/c.txt
./sub1/z.txt
./sub1/sub3
./sub1/sub3/d.txt
What I've tried:
- looked at all
findflags. It cannot print this way. - looked at all sort flags. It can not interpret lines as paths and re-order them this way.
This is not pre-order because ./sub1/sub3/d.txt comes before ./sub1/z.txt.
You can do it like this with bash and GNU find:
It's slow but does what you want.