coreutils 中的 ls 默认执行区域设置感知排序,因此在某些情况下可能会产生令人惊讶的结果(例如,% foo 将在 LANG=en_US 中的 bar 和 quux 之间排序。 如果您想要 ASCIIbetical 排序,请使用
LC_ALL=C ls
ls from coreutils performs a locale-aware sort by default, and thus may produce surprising results in some cases (for instance, %foo will sort between bar and quux in LANG=en_US). If you want an ASCIIbetical sort, use
-rw-rw-r-- 1 luckydonald luckydonald 532 Feb 21 2017 Makefile
-rwxrwxrwx 1 luckydonald luckydonald 4096 Nov 17 23:47 file.txt
So with 9,9 you sort column 9 up to the column 9, being the file names. You have to provide where to stop, which is the same column in this case. The columns start with 1.
Also, if you want to ignore upper/lower case, add --ignore-case to the sort command.
I had files from file1 to file 20. 'ls' would display them out of order and the best method I found for ordering them quickly was ls -v
The description in the man pages (man 1 ls) explains
-v natural sort of (version) numbers within text
I tested this with folders for instance ls -vd1 */ will display only folders in order. It seems to rely on asci values, so you will see capital letters always take precedence over lower case. This is because capitals are always at a lower asci value.
In Debian Jessie, this works nice:
ls -lah --group-directories-first
# l=use a long listing format
# a=do not ignore entries starting with .
# h=human readable
# --group-directories-first=(obvious)
# Note: add -r for reverse alpha
# You might consider using lh by appending to ~/.bashrc as the alias:
~$ echo "alias lh='ls -lah --group-directories-first'" >>~/.bashrc
# -- restart your terminal before using lh command --
In Debian Jessie, this works nice:
ls -lah --group-directories-first
# l=use a long listing format
# a=do not ignore entries starting with .
# h=human readable
# --group-directories-first=(obvious)
# Note: add -r for reverse alpha
# You might consider using lh by appending to ~/.bashrc as the alias:
~$ echo "alias lh='ls -lah --group-directories-first'" >>~/.bashrc
# -- restart your terminal before using lh command --
发布评论
评论(15)
对于简单的事情,您可以将 ls 与 sort 结合起来。 对于仅文件名列表:
要按相反顺序对它们进行排序:
For something simple, you can combine ls with sort. For just a list of file names:
To sort them in reverse order:
我的 ls 默认按名称排序。 你看到什么了?
man ls
指出:My ls sorts by name by default. What are you seeing?
man ls
states:coreutils
中的ls
默认执行区域设置感知排序,因此在某些情况下可能会产生令人惊讶的结果(例如,% foo
将在LANG=en_US
中的bar
和quux
之间排序。 如果您想要 ASCIIbetical 排序,请使用ls
fromcoreutils
performs a locale-aware sort by default, and thus may produce surprising results in some cases (for instance,%foo
will sort betweenbar
andquux
inLANG=en_US
). If you want an ASCIIbetical sort, use*nix 工具的美妙之处在于您可以将它们组合起来:
ls -l
的输出将如下所示因此使用
9,9
您可以对列9
进行排序code> 到9
列,即文件名。 您必须提供停止位置,在本例中是同一列。 这些列以1
开头。另外,如果您想忽略大小写,请在排序命令中添加
--ignore-case
。The beauty of *nix tools is you can combine them:
The output of
ls -l
will look like thisSo with
9,9
you sort column9
up to the column9
, being the file names. You have to provide where to stop, which is the same column in this case. The columns start with1
.Also, if you want to ignore upper/lower case, add
--ignore-case
to the sort command.仅数字字符串不同的文件可以根据该数字进行排序,前提是该数字前面有分隔符。
在这种情况下,可以使用以下语法:
示例:
Files being different only by a numerical string can be sorted on this number at the condition that it is preceded by a separator.
In this case, the following syntax can be used:
Example:
注意:“a”出现在“Z”之后:
A.txt Z.txt aa.txt
NOTICE: "a" comes AFTER "Z":
A.txt Z.txt aa.txt
从手册页(对于 bash ls):
From the man page (for bash ls):
ls -X 可以用于此目的,以防您将 ls 别名设置为更有用的默认值。
ls -X works for that purpose, in case you have aliased ls to a more useful default.
ls
实用程序应符合 IEEE Std 1003.1-2001 (POSIX.1
) 其中指出:The
ls
utility should conform to IEEE Std 1003.1-2001 (POSIX.1
) which states:检查您的
.bashrc
文件中的别名。Check your
.bashrc
file for aliases.您可以尝试:
ls -lru
-u 和 -lt:排序并显示访问时间;
You can try:
ls -lru
-u with -lt: sort by, and show, access time;
我使用 ubuntu/debian。
我有从 file1 到 file 20 的文件。“ls”会乱序显示它们,我发现快速排序它们的最佳方法是
ls -v
手册页中的描述 (man 1 ls)解释说
我用文件夹进行了测试,例如
ls -vd1 */
将仅按顺序显示文件夹。 它似乎依赖于 asci 值,因此您会看到大写字母始终优先于小写字母。 这是因为大写字母的 asci 值始终较低。I use ubuntu/debian.
I had files from file1 to file 20. 'ls' would display them out of order and the best method I found for ordering them quickly was
ls -v
The description in the man pages (man 1 ls) explains
I tested this with folders for instance
ls -vd1 */
will display only folders in order. It seems to rely on asci values, so you will see capital letters always take precedence over lower case. This is because capitals are always at a lower asci value.如果您有编号的文件,则会很好地排序,例如
will sort nicely if you have numbered files, e.g.
我使用以下命令获取了按名称排序的目录内容:
ls -h
I got the contents of a directory sorted by name using below command:
ls -h