bash 按排序顺序列出文件名 - ls 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15268108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
list file names with sorted order - ls command
提问by Navin
I have some files in a UNIX directory:
我在 UNIX 目录中有一些文件:
/opt/apps/testloc $ ls -mn
test_1.txt
test_2.txt
test_11.txt
test_12.txt
test_3.txt
I want to list this with lscommand and I need the output in sorted order based on the numbers at the end of the file name. Say output should be like below.
我想用ls命令列出它,我需要根据文件名末尾的数字按排序顺序输出。说输出应该如下所示。
test_1.txt, test_2.txt, test_3.txt, test_11.txt, test_12.txt
I am not able to get as mentioned. These file names were considered as text and they were sorted as below,
我无法获得如上所述的结果。这些文件名被视为文本,它们的排序如下,
test_11.txt, test_12.txt, test_1.txt, test_2.txt, test_3.txt
My command ls –mn(I need the output to be in comma separated format so I have used -m)
我的命令ls –mn(我需要以逗号分隔的格式输出,所以我使用了-m)
I need this to be done to process the files in incremental format in my next process.
我需要这样做才能在我的下一个过程中以增量格式处理文件。
回答by Chris Seymour
If you version of sortcan do a version sortwith -Vthen:
如果你的版本sort可以做一个version sort有-V那么:
$ ls | sort -V | awk '{str=str$ ls | sort -t_ -nk2,2 | awk '{str=str# Bash or ksh + GNU or other sort that handles NUL delimiters
function sortFiles {
[[ -e ]] || return 1
typeset a x
for x; do
printf '%s %sls -1 *_* | sort -t_ -n -k2 | sed ':0 N;s/\n/, /;t0'
' "${x//[^[:digit:]]}" "$x"
done |
LC_ALL=C sort -nz - | {
while IFS= read -rd '' x; do
a+=("${x#* }")
done
typeset IFS=,
printf '%s\n' "${a[*]}"
}
}
sortFiles *
","}END{sub(/,$/,"",str);print str}'
test_1.txt, test_2.txt, test_3.txt, test_11.txt, test_12.txt
", "}END{sub(/, $/,"",str);print str}'
test_1.txt, test_2.txt, test_3.txt, test_11.txt, test_12.txt
If not do:
如果不这样做:
ls -1 *_* |
awk -F '_' '
{
key[gensub(/\..*$/, "", 1, $NF) "a" NR] = NR;
name[NR] = ##代码##;
}
END {
len = asorti(key, keysorted, "@ind_num_asc");
for (i = 1; i < len; i++) {
printf "%s, ", name[key[keysorted[i] ] ];
}
printf "%s\n", name[key[keysorted[len] ] ];
}'
回答by ormaaj
That you require output to be in a specific format tells me that you shouldn't be using ls. Since recursive results aren't required, use a glob.
您要求输出采用特定格式告诉我您不应该使用 ls。由于不需要递归结果,请使用 glob。
##代码##回答by Andrey
If all the file names contain exactly one _character, followed by a numeric value, this relatively simple script will sort file names by the numeric field and output them in a ,[space]separated list (as ls -mdoes):
如果所有文件名只包含一个_字符,后跟一个数字值,这个相对简单的脚本将按数字字段对文件名进行排序,并将它们输出到一个,[space]单独的列表中(同样ls -m如此):
However, if there are multiple _characters in file names and you want to sort them by the last numeric field (not necessarily the same within the file names, e.g. test_1_3.txtand test_2.txt), more complex script is required:
但是,如果_文件名中有多个字符并且您想按最后一个数字字段对它们进行排序(文件名中不一定相同,例如test_1_3.txt和test_2.txt),则需要更复杂的脚本:
回答by Yogendra Sharma
ls -al | sort +4n : List the files in the ascending order of the file-size. i.e sorted by 5th filed and displaying smallest files first.
ls -al | sort +4n :按文件大小的升序列出文件。即按第 5 个文件排序并首先显示最小的文件。

