Terminal find, directories last instead of first

bash, makefile, terminal

Solution

Possible solution:

- use `find` for getting filenames and directory depth, i.e `find ... -printf "%d\t%p\n"`

- sort list by directory depth with `sort -n`

- remove directory depth from output to use filenames only

test:

without sorting:

$ find folder1/ -depth -type f -printf "%d\t%p\n"
2   folder1/f2/f3
1   folder1/file0

with sorting:

$ find folder1/ -type f -printf "%d\t%p\n" | sort -n | sed -e "s|[0-9]*\t||"
folder1/file0
folder1/f2/f3

the command you need looks like

cat $(find src/js -type f -name "*.js" -printf "%d\t%p\n" | sort -n | sed -e "s|[0-9]*\t||")>min.js

Problem

I have a `makefile` that concatenates JavaScript files together and then runs the file through `uglify-js` to create a `.min.js` version. I'm currently using this command to find and concat my files ``` find src/js -type f -name "*.js" -exec cat {} >> ${jsbuild}$@ \; ``` But it lists files in directories first, this makes heaps of sense but I'd like it to list the `.js` files in the `src/js` files above the directories to avoid getting my `undefined` JS error. Is there anyway to do this or? I've had a google around and seen the `sort` command and the `-s` flag for `find` but it's a bit above my understanding at this point! [EDIT] The final solution is slightly different to the accepted answer but it is marked as accepted as it brought me to the answer. Here is the command I used ``` cat `find src/js -type f -name "*.js" -print0 | xargs -0 stat -f "%z %N" | sort -n | sed -e "s|[0-9]*\ \ ||"` > public/js/myCleverScript.js ```

Original source