ivoruJavaBoy
ivoruJavaBoy

Reputation: 1357

Unix find and sort with wildcards

let's suppose i've a folder with some xml file:

I would like to pipe the find with some kind of sort command using wildcards and my custom sorting logic.

That because I want that the order of the filename returned will be always the same.

For example, i want always:

How can i achieve this? Any idea? I did it in the past using arrays but don't remember exactly how i did now...

Thanks

Upvotes: 0

Views: 737

Answers (3)

Jack
Jack

Reputation: 6158

I recommend just putting your find commands in the order you want:

$ find . -name config.xml; \
> find . -name \*.as-jdbc.xm; \
> find . -name \*-jdbc.xml -a ! -name \*as-jdbc.xml; \
> find . -name router.xml; \
> ... and so on.

Upvotes: 0

Alex Harvey
Alex Harvey

Reputation: 15472

It is certainly easier to do this in a higher level language like Python.

This is not a sorting problem; it is an ordering problem. As such, you cannot use the Unix sort command.

Inevitably, you will need to make 4 passes anyway so I would do either:

$ find /tmp/alex -name config.xml ; \
> find /tmp/alex -name *-as-jdbc.xml ; \
> find /tmp/alex \( \! -name *-as-jdbc.xml -a -name *-jdbc.xml \) ; \
> find /tmp/alex \( -type f -a \! -name config.xml -a \! -name *-jdbc.xml \)
/tmp/alex/config.xml
/tmp/alex/a-as-jdbc.xml
/tmp/alex/z-as-jdbc.xml
/tmp/alex/fa-jdbc.xml
/tmp/alex/cleardown.xml
/tmp/alex/paster.xml
/tmp/alex/router.xml

Or use grep:

$ find /tmp/alex -type f > /tmp/aaa
$ grep /config.xml /tmp/aaa ; \
> grep -- -as-jdbc.xml /tmp/aaa ; \
> grep -- -jdbc.xml /tmp/aaa | grep -v -- -as-jdbc.xml ; \
> egrep -v '(?:config.xml|-jdbc.xml)' /tmp/aaa
/tmp/alex/config.xml
/tmp/alex/a-as-jdbc.xml
/tmp/alex/z-as-jdbc.xml
/tmp/alex/fa-jdbc.xml
/tmp/alex/cleardown.xml
/tmp/alex/paster.xml
/tmp/alex/router.xml

Upvotes: 1

Esteban
Esteban

Reputation: 1815

Not too pretty but :

rules.txt:

config\.xml
.*\.as\-jdbc\.xml
^[^-]*\-jdbc\.xml
router\.xml

Commands:

$ find /path/to/dir > /tmp/result.txt
$ cat rules.txt | xargs -I{} grep -E "{}" /tmp/result.txt
config.xml
a-as-jdbc.xml
z-as-jdbc.xml
fa-jdbc.xml
router.xml

You will have to add the two others patterns needed for paster and cleardown

Upvotes: 3

Related Questions