[英]sort files found by command 'find'
My program takes a list of files and processes them according to the order in which the files are given. 我的程序获取文件列表,并根据文件给出的顺序对其进行处理。 For instance: 例如:
$ ./myScript.sh --takeFiles a b c d e f g
Now, since I have to pass a considerable number of files, I use the find
command and specify how to find the exact files I want: 现在,由于必须传递大量文件,因此我使用find
命令并指定如何查找所需的确切文件:
sudo find . -path "./aFolder/*_parameterOne_*_*/*_parameterTwo_*_*/*_someCommonString_*" ! -name "*_aStringToExclude*" -exec ./myScript.py --takeFiles {} +
It works like a charm, except that I would my files to be passed to myScript.sh after being sorted first by "parameterTwo_* " (where in the star I have an integer) and then by "parameterTwo *_" where again the star stands for a numerical value. 它的工作原理就像一种魅力,除了我先将文件通过“ parameterTwo _ * ”(星号中我有一个整数) ,然后再通过“ parameterTwo * _”(星号再次) 排序后,将文件传递到myScript.sh代表数值。
Is it possible? 可能吗?
The the parts before parameterOne
and parameterTwo
do not contain the character _
, you can simply use sort
: parameterOne
和parameterTwo
之前的部分不包含字符_
,您可以简单地使用sort
:
find ... -print0 |
sort -z -t_ -k6n -k3n |
xargs -r0 ./myScript.py --takeFiles
Update: A more complex solution might look as follows. 更新:一个更复杂的解决方案可能如下所示。 However, I think it would be easier to sort the pathnames in the Python script. 但是,我认为在Python脚本中对路径名进行排序会更容易。
#! /bin/bash
find ... -print0 |
while IFS= read -r -d '' pathname; do
[[ "$pathname" =~ "_parameterOne_"([0-9]+).*"_parameterTwo_"([0-9]+) ]] &&
printf '%05d%05d %s\0' "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}" "$pathname"
done |
sort -z |
while IFS= read -r -d '' pathname; do
printf '%s\0' "${pathname#* }"
done |
xargs -r0 ./myScript.py --takeFiles
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.