Answers:
规范的解决方案:
ls -l | sort -k3,3
一个单独的3(如在'-k3'中)将告诉在行尾sort
使用第3列进行排序。这样,您就可以进行更高级的排序,如ls -l | sort -k3,3 -rnk5,5
,该排序将首先按用户名对文件进行排序,然后再按大小对文件进行排序,然后再对最大文件进行排序。
与往常一样,有关更多信息,请运行man sort
。
我会用 find -printf "%u %h/%f\n" | sort
find: bad option -printf
find --version find (GNU findutils) 4.4.2 Copyright (C) 2007 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Eric B. Decker, James Youngman, and Kevin Dalley. Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)
ls -l | awk '{print $3"\t\t"$9}' | sort
也可以做到。第一列是用户名,第二列是文件/目录名称:
[ 09:20 jon@host /home ]$ ls -l | awk '{print $3"\t\t"$9}' | sort
bettina bettina
caldavd caldavd
davical_app postgres
davical_dba davical_dba
istat istat
jared jared
jason jason
jon jon
jon repo
root lost+found
root SCN_RepositoryB.tar.gz
tomcat tomcat
这将仅显示文件/目录名称(按所有者排序):
[ 09:24 jon@host /home ]$ ls -l | awk '{print $3"\t\t"$9}' | sort | awk '{print $2}'
bettina
caldavd
postgres
davical_dba
istat
jared
jason
jon
repo
lost+found
SCN_RepositoryB.tar.gz
tomcat
这将显示它们已排序但在一行上:
[ 09:26 jon@host /home ]$ ls -l | awk '{print $3"\t\t"$9}' | sort | awk '{print $2}' | tr "\n" " "
bettina caldavd postgres davical_dba istat jared jason jon repo lost+found SCN_RepositoryB.tar.gz tomcat
ls -l | sort -k3
3
-k3之后 无需指定
kracekumar@python-lover:~$ ls -l /tmp | sort -k3
total 36
drwx------ 2 gdm gdm 4096 2011-10-14 08:36 orbit-gdm
drwx------ 2 gdm gdm 4096 2011-10-14 08:36 pulse- 2L9K88eMlGn7
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 keyring-4O5hSc
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 pulse-SBBBfzrceWvC
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 ssh-UkESZoNj1595
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 virtual-kracekumar.5D8Mlv
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:43 orbit-kracekumar
drwx------ 2 root root 4096 2011-10-14 08:36 pulse-PKdhtXMmr18n
drwxr-xr-x 3 www-data www-data 4096 2011-10-14 08:36 www-data-temp-aspnet-0
kracekumar@python-lover:~$ ls -l /tmp | sort -k3,3
total 36
drwx------ 2 gdm gdm 4096 2011-10-14 08:36 orbit-gdm
drwx------ 2 gdm gdm 4096 2011-10-14 08:36 pulse-2L9K88eMlGn7
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 keyring-4O5hSc
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 pulse-SBBBfzrceWvC
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 ssh-UkESZoNj1595
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 virtual-kracekumar.5D8Mlv
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:43 orbit-kracekumar
drwx------ 2 root root 4096 2011-10-14 08:36 pulse-PKdhtXMmr18n
drwxr-xr-x 3 www-data www-data 4096 2011-10-14 08:36 www-data-temp-aspnet-0
kracekumar@python-lover:~$