最新消息:

Linux中ls对文件进行按大小排序和按时间排序

Linux ipcpu 13410浏览

一、使用sort排序

[root@vps ~]# ll | grep ‘^[^d]‘ | sort -n
-rw——- 1 root root 0 Aug 4 19:21 ipp.txt
-rw——- 1 root root 232 Aug 4 19:23 openvpn-status.log
-rw-r–r– 1 root root 2095419 Jul 27 14:57 nagios-plugins-1.4.15.tar.gz
-rw-r–r– 1 root root 3215350 Mar 9 11:49 nagios-3.2.1.tar.gz
-rw-r–r– 1 root root 626042 Apr 30 2008 lzo-2.03.tar.gz
-rw-r–r– 1 root root 669076 Mar 17 2009 openvpn-2.0.9.tar.gz
-rw-r–r– 1 root root 6697 Aug 2 00:39 1.tar.gz
total 6500
[root@vps ~]# ll -h | grep ‘^[^d]‘ | sort -n
-rw——- 1 root root 0 Aug 4 19:21 ipp.txt
-rw——- 1 root root 232 Aug 4 19:23 openvpn-status.log
-rw-r–r– 1 root root 2.0M Jul 27 14:57 nagios-plugins-1.4.15.tar.gz
-rw-r–r– 1 root root 3.1M Mar 9 11:49 nagios-3.2.1.tar.gz
-rw-r–r– 1 root root 612K Apr 30 2008 lzo-2.03.tar.gz
-rw-r–r– 1 root root 654K Mar 17 2009 openvpn-2.0.9.tar.gz
-rw-r–r– 1 root root 6.6K Aug 2 00:39 1.tar.gz
total 6.4M
总结:使用sort对数字进行排序那就大错特错了!
例如:
[root@vps ~]# cat haha.txt
1
2
10
19
[root@vps ~]# sort haha.txt
1
10
19
2
[root@vps ~]#
sort只会排第一个字符!

二、直接使用ls参数
[root@vps ~]# ll -S | grep ‘^[^d]‘
total 10000
-rw-r–r– 1 root root 3565833 Aug 5 10:07 httpd-2.2.16.tar.gz.filepart
-rw-r–r– 1 root root 3215350 Mar 9 11:49 nagios-3.2.1.tar.gz
-rw-r–r– 1 root root 2095419 Jul 27 14:57 nagios-plugins-1.4.15.tar.gz
-rw-r–r– 1 root root 669076 Mar 17 2009 openvpn-2.0.9.tar.gz
-rw-r–r– 1 root root 626042 Apr 30 2008 lzo-2.03.tar.gz
[root@vps ~]# ll -Sh 
[root@vps ~]# ll -Srh —这两个最常用

三、按时间排序

[root@vps ~]# ll -rt
total 6500
-rw-r–r– 1 root root 626042 Apr 30 2008 lzo-2.03.tar.gz
-rw-r–r– 1 root root 669076 Mar 17 2009 openvpn-2.0.9.tar.gz
-rw-r–r– 1 root root 3215350 Mar 9 11:49 nagios-3.2.1.tar.gz
-rw-r–r– 1 root root 2095419 Jul 27 14:57 nagios-plugins-1.4.15.tar.gz
drwxrwxrwx 13 1000 users 4096 Aug 1 02:30 lzo-2.03
drwxrwxrwx 16 root root 4096 Aug 2 00:28 openvpn-2.0.9
-rw-r–r– 1 root root 6697 Aug 2 00:39 1.tar.gz
drwxrwxrwx 16 nagios root 4096 Aug 3 20:26 nagios-plugins-1.4.15
drwxrwxr-x 15 nagios nagios 4096 Aug 3 21:08 nagios-3.2.1
-rw——- 1 root root 0 Aug 4 19:21 ipp.txt
-rw——- 1 root root 232 Aug 4 19:26 openvpn-status.log
[root@vps ~]#

=====THE END=====

精彩评论:

 wss8848 | #1
November 29, 2012 at 1:30 pm
使用sort -n 可以按照数字形式进行排序

转载请注明:IPCPU-网络之路 » Linux中ls对文件进行按大小排序和按时间排序