![有没有办法为“ls”命令提供定制的排序规则?](https://linux22.com/image/1010388/%E6%9C%89%E6%B2%A1%E6%9C%89%E5%8A%9E%E6%B3%95%E4%B8%BA%E2%80%9Cls%E2%80%9D%E5%91%BD%E4%BB%A4%E6%8F%90%E4%BE%9B%E5%AE%9A%E5%88%B6%E7%9A%84%E6%8E%92%E5%BA%8F%E8%A7%84%E5%88%99%EF%BC%9F.png)
我从事材料科学工作,因此我的许多目录都是按化学元素或化学式命名的。因此,我的目录的ls
列出ls -v
顺序与化学顺序不符。我知道我可以重命名目录,使它们以元素的原子序数开头,但我不想这样做。我想知道是否有一种简单的方法可以提供自定义规则,ls
以便它根据元素的原子序数而不是字母或数字顺序列出目录。如果有替代方案,ls
那也是可以接受的。
答案1
制作一些测试目录:
cd /tmp
for atom in H He Li Be B C N O F Ne Na;do mkdir $atom-directory;done
每个符号的原子序数:
export ATOMS="H 1 He 2 Li 3 Be 4 B 5 C 6 N 7 O 8 F 9 Ne 10 Na 11" #and so on...
创建一个 bash 函数,确保所有内容都在一行上:
atomls () { for f in $(perl -le'%m=split" ",$ENV{ATOMS};$re=join"|",keys%m;print for map$$_[0],sort{$m{$$a[1]}<=>$m{$$b[1]}}map[$_,/($re)\b/],@ARGV' $*);do ls -ld $f; done }
运行:
atomls *
结果:
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 H-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 He-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 Li-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 Be-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 B-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 C-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 N-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 O-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 F-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 Ne-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 Na-directory
仅列出两个字母原子:
atomls ??-dir*
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 He-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 Li-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 Be-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 Ne-directory
drwxrwxr-x 2 idiot idiot 4096 okt. 22 19:56 Na-directory
可能有更好的方法......