您可以按名称对ls列表进行排序吗?
您可以按名称对ls列表进行排序吗?
Answers:
我的ls默认按名称排序。你在看什么
man ls 状态:
List information about the FILEs (the current directory by default). Sort entries alpha‐betically if none of -cftuvSUX nor --sort is specified.:
ls如果-c1指定则不排序:有没有办法做到这一点?(--sort=name似乎无效)
ls -la,我看到:.bashrc - can - .config - Downloads - .local - tmp,排序名称将是:.bashrc - .config - .local - Downloads - can - tmp
对于简单的事情,可以将ls与sort结合使用。仅列出文件名:
ls -1 | 分类
以相反的顺序对它们进行排序:
ls -1 | 排序-r
ls | sort -n 1.1.1; 1.1.2; 1.1.3; 2.10.1; 2.10.15;2.10.2; 2.10.20; 2.10.21; 2.1.1; 2.1.10; 2.1.15; 2.1.2; 2.1.3; 2.1.4; 10.1.1; 10.1.2; 10.1.3; 11.0.1; 11.0.2; 11.0.20; 11.0.21; 11.0.22; 如您所见2.10.1之前的2.10.15。
ls -1r。
ls从coreutils执行一个区域识别排序缺省,并且因此可以产生在一些情况下令人惊讶的结果(例如,%foo将间排序bar和quux在LANG=en_US)。如果要ASCIIbetical排序,请使用
LANG=C ls
LANGilfnuence的排序行为,此帖子对我有很大帮助!
?当输出是终端时,对于每个非ASCII字符输出(ls的错误功能会检查它是否输出到终端,在配管时起作用)。您可以使用管道将其“修复”到目录,使用C.UTF-8语言环境(如果系统支持)和/或使用-b标志。更好的是,根本不要使用ls,最好使用`
ls在连字符前对连字符进行排序。
* nix工具的妙处在于您可以将它们结合起来:
ls -l | sort -k9,9
的输出ls -l将如下所示
-rw-rw-r-- 1 luckydonald luckydonald 532 Feb 21 2017 Makefile
-rwxrwxrwx 1 luckydonald luckydonald 4096 Nov 17 23:47 file.txt
因此,9,9您9将列排序到column 9,即文件名。您必须提供在哪里停止,在这种情况下,这是同一列。列以开头1。
另外,如果要忽略大写/小写,请添加--ignore-case到sort命令。
-k9,9意思
9到同一列9。正常ls输出如下所示:drwx------ 8 999 user 4.0K Feb 5 2017 file.txt,因此第9列是文件名。如果要忽略大小写,请使用--ignore-case排序。
注意:“ a”在“ Z”之后:
$ touch A.txt aa.txt Z.txt
$ ls
A.txt Z.txt aa.txt
normal这里尝试解决方案。
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 --