#!/usr/bin/perl -w
#
-# l / ll / lf / llf - substitute of the classic ls command
+# l / ll / lf / llf - better replacement of the classic ls command
#
# Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
#
-# Copyright: GNU General Public License
+# Copyright: Perl Artistic License
use Cwd qw'abs_path';
use File::Basename;
# parse CLI arguments
$opt_l = $opt_i = $opt_t = $opt_s = $opt_a = $opt_r = $opt_d = $opt_n = 0;
$opt_L = $opt_N = $opt_c = $opt_u = $opt_S = $opt_R = $opt_z = $opt_h = 0;
-$opt_U = $opt_x = 0;
-${'opt_*'} = ${'opt_?'} = 0;
+$opt_U = $opt_x = $opt_E = 0;
+${'opt_*'} = 0;
$opt_m = $opt_f = $opt_F = $opt_D = '';
-&usage if !getopts('hdnlLNitcuarsxUSRz*?m:f:D:F:') || $opt_h;
+getopts('hdnlLNitcuarsxUSREz*m:f:D:F:') or usage(1);
+usage(0) if $opt_h;
$opt_z = 1 unless $opt_R;
$opt_l = 1 if $0 eq 'll';
$opt_l = $opt_i = $opt_a = $opt_S = 1 if $0 eq 'lll';
+&examples if $opt_E;
if ($0 eq 'lf' or $0 eq 'llf') {
- unless ($opt_F) {
- $opt_F = shift;
- unless (length $opt_F) {
- print "find regexp: ";
- chomp($opt_F = <STDIN>||'');
- }
- }
- $opt_l = $0 if $0 eq 'llf';
- $opt_F = '.' unless length $opt_F;
- $opt_R = $opt_F;
+ $opt_F ||= shift or usage(1);
+ $opt_R ||= scalar(@ARGV) || ($opt_F eq '.');
+ $opt_l ||= $0 eq 'llf';
}
$postsort = $opt_t||$opt_s;
$postproc = $postsort||$opt_z;
-&examples if ${'opt_?'};
-
# mark for squeeze operation
$z = $opt_z ? "\0" : '';
sub usage {
+ my $status = shift;
my $opts = '[-lastcuidnrzLRxNS*] [-f format] [-D X:Y]';
+ local *OUT = $status ? *STDERR : *STDOUT;
+
if ($0 ne 'lf') {
- print "usage: $0 $opts [-F regexp] [file...]\n";
+ print OUT "usage: $0 $opts [-F regexp] [file...]\n";
}
$opts =~ s/R//;
- print "usage: lf $opts regexp [directory...]\n";
- print <<EOD;
-options: -l long list
+ print OUT "usage: lf $opts regexp [directory...]\n";
+ print OUT <<EOD;
+options: -l long list (implicit if called 'll')
-a list also .* files
-s sort by size
-t sort by time
-F find files matching case insensitive regexp
-N show only normal (regular) files
-S print statistics summary at end
- -* list plain file names (without masking \\)
+ -* list plain file names (without \\ masking)
-f user defined format output, format characters are:
m=mode, u=user, g=group, s=size, l=hard links count, i=inode
n=name only, d=date, a=access+modification+inodechange dates
-D list only files newer than X and older than Y
XY format: NUMBER[smhd] (s=seconds, m=minutes, h=hours, d=days)
XY format: YYYY-MM-DD (Y=year, M=month, D=day)
- -? show examples
+ -E show examples
EOD
- exit 2;
+ exit $status;
}
sub examples {
l -D 10d: # list files newer than 10 days
ll # list files long format (equal to: l -l)
lll # list files extra long format (equal to: l -liS)
-lf 'status.*mp3' # list files recursive matching regexp (equal to: l -RF)
-lf sda3 /dev # list devices matching sda3 (equal to: l -RF sd3 /dev)
+lf 'status.*mp3' # list files matching regexp (equal to: l -F 'status.*mp3')
+lf sda1 /dev # list devices matching sda1 (equal to: l -RF sda1 /dev)
EOD
exit;
}