3 # l / ll / lf / llf - better replacement of the classic ls command
5 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
7 # Perl Artistic License
13 # the name of the game
18 # unshift @ARGV,split /\s+/,$ENV{'l_opt'} if $ENV{'l_opt'};
20 @ARGV = grep { chomp } <STDIN> if "@ARGV" eq '-';
23 $opt_l = $opt_i = $opt_t = $opt_s = $opt_a = $opt_r = $opt_d = $opt_n = 0;
24 $opt_L = $opt_N = $opt_c = $opt_u = $opt_S = $opt_R = $opt_z = $opt_h = 0;
25 $opt_U = $opt_x = $opt_E = 0;
27 $opt_m = $opt_f = $opt_F = $opt_D = '';
28 getopts('hdnlLNitcuarsxUSREz*m:f:D:F:') or usage(1);
30 $opt_z = 1 unless $opt_R;
31 $opt_l = 1 if $0 eq 'll';
32 $opt_l = $opt_i = $opt_a = $opt_S = 1 if $0 eq 'lll';
34 if ($0 eq 'lf' or $0 eq 'llf') {
35 $opt_F ||= shift or usage(1);
36 $opt_R ||= scalar(@ARGV) || ($opt_F eq '.');
37 $opt_l ||= $0 eq 'llf';
40 $postsort = $opt_t||$opt_s;
41 $postproc = $postsort||$opt_z;
43 # mark for squeeze operation
44 $z = $opt_z ? "\0" : '';
46 # default sorting methode
47 if ($opt_U) { $lcsort = sub { return @_ } }
48 elsif ($opt_r) { $lcsort = sub { sort { lc $b cmp lc $a } @_ } }
49 else { $lcsort = sub { sort { lc $a cmp lc $b } @_ } }
51 # default: list only files not beginning with a dot
53 if ($opt_a) { $opt_m = '.' }
54 else { $opt_m = '^[^\.]' }
60 if ($opt_D =~ /:(\d+)([mhd])/) {
63 if ($z =~ /m/) { $older *= 60 }
64 elsif ($z =~ /h/) { $older *= 60*60 }
65 elsif ($z =~ /d/) { $older *= 60*60*24 }
66 } elsif ($opt_D =~ /:(\d\d\d\d-\d\d-\d\d)$/) {
69 if ($opt_D =~ /(\d+)([mhd]):/) {
72 if ($z =~ /m/) { $newer *= 60 }
73 elsif ($z =~ /h/) { $newer *= 60*60 }
74 elsif ($z =~ /d/) { $newer *= 60*60*24 }
75 } elsif ($opt_D =~ /^(\d\d\d\d-\d\d-\d\d):/) {
80 # preselect date field number
81 if ($opt_c) { $sdf = 'c' }
82 elsif ($opt_u) { $sdf = 'a' }
86 if (@ARGV) { @ARGV = &$lcsort(@ARGV) }
87 else { @ARGV = &getfiles('.') }
92 # post process files list?
93 # remark: if no postprocessing, files list has been already printed in list()
94 if (@LIST && $postproc) {
96 # on -t or -s option sort list on date or size
97 # and then strip of leading sorting pre-string
98 @LIST = grep { s/.{21}// } reverse sort @LIST if $postsort;
100 # squeeze size field (= remove unnecessary spaces)
101 if ($opt_z and not $opt_f) {
102 $opt_z = '%'.$opt_z.'s ';
103 @LIST = grep { s/\0 *([,\d\.\-]+) /sprintf($opt_z,$1)/e } @LIST;
106 @LIST = reverse @LIST if $opt_r;
108 if (not ($opt_t or $opt_U) and grep /^d[rR-][wW-][xX-]/,@LIST) {
109 foreach (@LIST) { print if /^d/ }
110 foreach (@LIST) { print unless /^d/ }
116 # print statistics summary?
118 print "$SS file(s):";
119 printf " r=%d (%s Bytes)",$SS{'-'},&d3($Ss) if $SS{'-'};
121 foreach my $type (qw(l d c b p s ?)) {
122 printf " %s=%d",$type,$SS{$type} if $SS{$type};
125 foreach my $type (keys %SS) { printf " %s=%d",$type,$SS{$type} }
129 exit ($found ? 0 : 1);
132 # collect files and build file lists
141 # loop over all argument files/directories
142 foreach $f (@files) {
144 # skip jed and emacs backup files
145 # next if $f =~ /~$/ and not $opt_a and not $opt_l;
153 warn "$0: dangling symlink $f\n";
160 # traverse real subdirs
161 if (-d $f and not -l $f) {
163 # skip other file systems on -x
165 my @pd = stat(dirname($f));
167 next if $pd[0] ne $sd[0];
169 collect(getfiles($f));
174 # suppress trailing / on -d option
175 $f =~ s:/$:: if $opt_d;
177 # on trailing / list subdirs, too
178 if ($f =~ m:/$:) { &list(&getfiles($f)) }
179 elsif ($f eq '') { &list('/') }
183 warn "$0: dangling symlink $f\n";
200 # GLOBAL: @LIST (filenames-list)
203 my ($file,$line,$linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates);
206 foreach $file (@files) {
208 next if $opt_F and not fmatch($file);
209 next if $opt_N and (not -f $file or -l $file);
211 # get file information
212 # if ($opt_L and stat $file or not $opt_L and lstat $file) {
214 ($linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates) = &info($file);
215 } elsif ($! eq "Permission denied") {
217 $inode = $links = $size = $uid = $gid = '?';
218 $mode = $opt_l ? '?---------' : '?---';
219 $date = '????-??-?? ??:??:??';
220 %dates = ('m' => 0, 'a' => 0, 'c' => 0);
222 warn "$0: ".quote($file)." - $!\n";
230 next if $older =~ /-/ and $day gt $older;
231 next if $older !~ /-/ and $dates{m} > time-$older;
234 next if $newer =~ /-/ and $day lt $newer;
235 next if $newer !~ /-/ and $dates{m} < time-$newer;
238 if (defined $linkname) {
240 # prepend sorting string
242 $line = sprintf '%21s',$date if $opt_t;
243 $line = sprintf '%21s',$size if $opt_s;
246 $uid = substr($uid,0,8);
247 $gid = substr($gid,0,8);
250 # user defined format?
252 foreach my $i (split '',$opt_f) {
255 if ($i eq 'm') { $line .= sprintf '%06o ', $mode }
256 elsif ($i eq 'u') { $line .= sprintf '%6d ', $uid }
257 elsif ($i eq 'g') { $line .= sprintf '%6d ', $gid }
258 elsif ($i eq 's') { $line .= sprintf "$z%16s ",$size }
259 elsif ($i eq 'l') { $line .= sprintf '%3s ', $links }
260 elsif ($i eq 'i') { $line .= sprintf '%14s ', $inode }
261 elsif ($i eq 'd') { $line .= sprintf '%10s ', $date }
262 elsif ($i eq 'a') { $line .= sprintf '%10s %10s %10s ',
263 $dates{'a'},$dates{'m'},$dates{'c'} }
265 if ($i eq 'm') { $line .= $mode.' ' }
266 elsif ($i eq 'u') { $line .= sprintf '%-8s ', $uid }
267 elsif ($i eq 'g') { $line .= sprintf '%-8s ', $gid }
268 elsif ($i eq 's') { $line .= sprintf "$z%19s ",$size }
269 elsif ($i eq 'l') { $line .= sprintf '%3s ', $links }
270 elsif ($i eq 'i') { $line .= sprintf '%14s ', $inode }
271 elsif ($i eq 'd') { $line .= $date.' ' }
272 elsif ($i eq 'D') { $line .= $date.' ' }
273 elsif ($i eq 'a') { $line .= &isodate($dates{'a'}).' '.
274 &isodate($dates{'m'}).' '.
275 &isodate($dates{'c'}).' ' }
276 elsif ($i eq 'A') { $line .= &isodate($dates{'a'}).' '.
277 &isodate($dates{'m'}).' '.
278 &isodate($dates{'c'}).' ' }
286 if ($opt_l) { $line .= sprintf "%06o %6d %6d $z%15s %10d ",
287 $mode,$uid,$gid,$size,$date }
288 else { $line .= sprintf "%06o $z%15s %10d ",
291 if ($opt_l) { $line .= sprintf "%s %-8s %-8s $z%19s %s ",
292 $mode,$uid,$gid,$size,$date }
293 else { $line .= sprintf "%s $z%19s %s ",
294 $mode,$size,substr($date,0,-3) }
297 if ($opt_i) { $line .= sprintf '%3s %10s ',$links,$inode }
300 $line .= $linkname."\n";
312 warn "$0: cannot get dir-info for ".quote($file)." - $!\n";
318 # get file information
322 # OUTPUT: filename with linkname, inode, hard link count, size, mode string,
326 my ($linkname,$links,$mode,$bmode,$uid,$gid,$date,%dates,@stat);
329 my @rwx = qw/--- --x -w- -wx r-- r-x rw- rwx/;
332 if ($opt_L) { @stat = stat $file }
333 else { @stat = lstat $file }
340 %dates = ('m' => $stat[9],
347 $date = $dates{$sdf};
349 $uid = getpwuid($stat[4]) || $stat[4];
350 $gid = getgrgid($stat[5]) || $stat[5];
351 $date = &isodate($dates{$sdf});
354 if (-f _) { $type = '-'; $size = $stat[7]; }
355 elsif (!$opt_L && -l _) { $type = 'l'; }
356 elsif (-d _) { $type = 'd'; }
357 elsif (-c _) { $type = 'c'; $size = &nodes($stat[6]); }
358 elsif (-b _) { $type = 'b'; $size = &nodes($stat[6]); }
359 elsif (-p _) { $type = 'p'; }
360 elsif (-S _) { $type = 's'; }
361 else { $type = '?'; }
365 $size = $stat[7] if $size eq '-';
368 $mode = $rwx[$bmode & 7];
370 $mode = $rwx[$bmode & 7] . $mode;
372 $mode = $rwx[$bmode & 7] . $mode;
373 substr($mode,2,1) =~ tr/-x/Ss/ if -u _;
374 substr($mode,5,1) =~ tr/-x/Ss/ if -g _;
375 substr($mode,8,1) =~ tr/-x/Tt/ if -k _;
378 # with short list display only effektive file access modes
382 . (-x _ ? 'X' : '-');
383 substr($mode,2,1) =~ tr/-x/Ss/ if -u _ or -g _;
384 substr($mode,3,1) =~ tr/-x/Tt/ if -k _;
388 # fall back to ls command if perl lstat failed
393 ($mode,$links,$uid,$gid,$size) = split /\s+/,`ls -ld $file 2>/dev/null`;
394 return undef unless defined $mode;
395 $type = substr($mode,0,1);
396 # for (my $i=0;$i<3;$i++) { push @dates,'????-??-?? ??:??:??' }
397 # $date = `gfind $dir -maxdepth 1 -name $file -printf '%Ty-%Tm-%Td %TT\n'`;
401 # summarize statistics
405 $Ss += $size if $type eq '-';
410 # determine longest size field
412 my $x = length $size;
413 $opt_z = $x if $x>$opt_z;
415 $linkname = ${'opt_*'} ? $file : quote($file) ;
416 if ($type eq 'l' and $opt_f !~ /n/) {
417 my $link = readlink($file);
419 $linkname .= ' -> ' . (${'opt_*'} ? $link : quote($link));
423 #$mode .= ' ' unless $mode =~ /\+$/;
425 return ($linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates);
429 # reformat integer into 3-digit doted format
430 # (when non-numerical mode is set)
432 # INPUT: integer or '-'
437 if ($opt_n) { s/-/0/ }
438 else { while (s/(\d)(\d\d\d\b)/$1,$2/) {} }
443 # get all files matching pattern $opt_m
445 # INPUT: directory to scan
447 # OUTPUT: files which match (sorted, directories first)
454 if (opendir D,$dir) {
455 $dir = '' if $dir eq '.';
456 while (defined($f = readdir D)) {
458 # skip . and .. pseudo-subdirs
459 next if $f =~ m:(^|/)\.\.?/*$:;
460 # skip ONTAP snapshot dir
461 next if $f =~ m:(^|/)\.snapshot/*$:;
464 # skip jed and emacs backup files
465 # next if $f =~ /~$/ and not $opt_a and not $opt_l;
467 if ($f =~ /$opt_m/) {
469 if (not -l $x and -d $x and not ($opt_R or $postsort or $opt_U)) {
478 @files = &$lcsort(@files);
479 @dirs = &$lcsort(@dirs);
482 warn "$0: cannot read $dir : $!\n";
485 return (@dirs,@files);
489 # reformat integer to string node
491 # INPUT: integer node
493 # OUTPUT: string node
496 return sprintf("%03d,%03d", ($rdev >> 8) & 255, $rdev & 255);
500 # reformat timetick to ISO date string
504 # OUTPUT: ISO date string
506 my @d = localtime shift;
507 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
508 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
512 # quote file name to printable name and escape shell meta chars
514 # INPUT: original file name
516 # OUTPUT: printable file name
519 my $mc = '\'\[\]\\\\ `"$?&<>$*()|{};';
521 unless (defined $_) {
526 if (s/[\000-\037\200-\237\241-\250]/?/g or /\'/) {
529 # } elsif (/[$mc]/ or -d and /:/) {
539 my $link = readlink($file)||'';
541 return $file if basename($file) =~ /$opt_F/i;
542 return $link if basename($link) =~ /$opt_F/i;
548 my $opts = '[-lastcuidnrzLRxNS*] [-f format] [-D X:Y]';
549 local *OUT = $status ? *STDERR : *STDOUT;
552 print OUT "usage: $0 $opts [-F regexp] [file...]\n";
555 print OUT "usage: lf $opts regexp [directory...]\n";
557 options: -l long list (implicit if called 'll')
558 -a list also .* files
561 -U sort by nothing (original i-node order)
562 -c list status change time instead of modification time
563 -u list last access time instead of modification time
564 -i list also inode and hard links numbers
565 -d do not list contents of diretories
568 -z squeeze size field (slows down output)
569 -L derefernce symbolic links
570 -R recursive into subdirs
571 -x do not cross filesystem boundaries with -R
572 -F find files matching case insensitive regexp
573 -N show only normal (regular) files
574 -S print statistics summary at end
575 -* list plain file names (without \\ masking)
576 -f user defined format output, format characters are:
577 m=mode, u=user, g=group, s=size, l=hard links count, i=inode
578 n=name only, d=date, a=access+modification+inodechange dates
579 -D list only files newer than X and older than Y
580 XY format: NUMBER[smhd] (s=seconds, m=minutes, h=hours, d=days)
581 XY format: YYYY-MM-DD (Y=year, M=month, D=day)
589 l *.c # list files ending with .c
590 l -la # list all files in long format
591 l -Rrs # list files recursive reverse sorted by size
592 l -*f mus # list files native names with format: mode+user+size
593 l -D 10d: # list files newer than 10 days
594 ll # list files long format (equal to: l -l)
595 lll # list files extra long format (equal to: l -liS)
596 lf 'status.*mp3' # list files matching regexp (equal to: l -F 'status.*mp3')
597 lf sda1 /dev # list devices matching sda1 (equal to: l -RF sda1 /dev)