get list of files in a folder BUT in a predetermined order

时间:2017-12-06 15:58:34

标签: bash

I am using the below code to extract the names of files in a folder and then performing an operation on those files.

for library in lib/*
 do
  echo ${library##*/}
  cp -v ${library##*/} /opt/java/deploy/
 done
}

The above yields an output of :

camel-amqp-2.16.0.jar
netty-all.jar
proton-j.jar
qpid-jms-client.jar

I then need to copy the jar files one by one in the application deployment folder.

Right now I have 4 jar files but in the future the # of files will increase.

The above works all fine. But the dilemma for me is that I have to make sure that qpid-jms-client.jar & camel-amqp-2.16.0.jar are the last to load.

Basically I need to sort the output so that the qpid & camel jar files are at the bottom of the list and are hence the last ones to be loaded.

Using sort does not help here since sorting alphabetically wont be of much use.

Is there something else I can try out ?

1 个答案:

答案 0 :(得分:0)

使用extglob即可:

shopt -s extglob nullglob

for lib in !(@(qpid-jms-client|camel-amqp-*)).jar qpid-jms-client.jar camel-amqp-*.jar; do

  echo "${lib##*/}"
  cp -v "${lib##*/}" /opt/java/deploy/

done

工作原理:

  • \ textglob pattern !(@(qpid-jms-client|camel-amqp-*)).jar匹配除*.jar&之外的所有qpid-jms-client.jar个文件camel-amqp-2.16.0.jar个文件。
  • 最后列出了这两个文件,以确保这两个jar文件最终始终匹配