3 # l / ll / lf / llf - substitute of the classic ls command
5 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
7 # Copyright: GNU General Public 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;
26 ${'opt_*'} = ${'opt_?'} = 0;
27 $opt_m = $opt_f = $opt_F = $opt_D = '';
28 &usage if !getopts('hdnlLNitcuarsUSRz*?m:f:D:F:') || $opt_h;
29 $opt_z = 1 unless $opt_R;
30 $opt_l = 1 if $0 eq 'll';
31 $opt_l = $opt_i = $opt_a = $opt_S = 1 if $0 eq 'lll';
32 if ($0 eq 'lf' or $0 eq 'llf') {
35 unless (length $opt_F) {
36 print "find regexp: ";
37 chomp($opt_F = <STDIN>||'');
40 $opt_l = $0 if $0 eq 'llf';
41 $opt_F = '.' unless length $opt_F;
45 $postsort = $opt_t||$opt_s;
46 $postproc = $postsort||$opt_z;
48 &examples if ${'opt_?'};
50 # mark for squeeze operation
51 $z = $opt_z ? "\0" : '';
53 # default sorting methode
54 if ($opt_U) { $lcsort = sub { return @_ } }
55 elsif ($opt_r) { $lcsort = sub { sort { lc $b cmp lc $a } @_ } }
56 else { $lcsort = sub { sort { lc $a cmp lc $b } @_ } }
58 # default: list only files not beginning with a dot
60 if ($opt_a) { $opt_m = '.' }
61 else { $opt_m = '^[^\.]' }
67 if ($opt_D =~ /:(\d+)([mhd])/) {
70 if ($z =~ /m/) { $older *= 60 }
71 elsif ($z =~ /h/) { $older *= 60*60 }
72 elsif ($z =~ /d/) { $older *= 60*60*24 }
73 } elsif ($opt_D =~ /:(\d\d\d\d-\d\d-\d\d)$/) {
76 if ($opt_D =~ /(\d+)([mhd]):/) {
79 if ($z =~ /m/) { $newer *= 60 }
80 elsif ($z =~ /h/) { $newer *= 60*60 }
81 elsif ($z =~ /d/) { $newer *= 60*60*24 }
82 } elsif ($opt_D =~ /^(\d\d\d\d-\d\d-\d\d):/) {
87 # preselect date field number
88 if ($opt_c) { $sdf = 'c' }
89 elsif ($opt_u) { $sdf = 'a' }
93 if (@ARGV) { @ARGV = &$lcsort(@ARGV) }
94 else { @ARGV = &getfiles('.') }
99 # post process files list?
100 # remark: if no postprocessing, files list has been already printed in list()
101 if (@LIST && $postproc) {
103 # on -t or -s option sort list on date or size
104 # and then strip of leading sorting pre-string
105 @LIST = grep { s/.{21}// } reverse sort @LIST if $postsort;
107 # squeeze size field (= remove unnecessary spaces)
108 if ($opt_z and not $opt_f) {
109 $opt_z = '%'.$opt_z.'s ';
110 @LIST = grep { s/\0 *([,\d\.\-]+) /sprintf($opt_z,$1)/e } @LIST;
113 @LIST = reverse @LIST if $opt_r;
115 if (not ($opt_t or $opt_U) and grep /^d[rR-][wW-][xX-]/,@LIST) {
116 foreach (@LIST) { print if /^d/ }
117 foreach (@LIST) { print unless /^d/ }
123 # print statistics summary?
125 print "$SS file(s):";
126 printf " r=%d (%s Bytes)",$SS{'-'},&d3($Ss) if $SS{'-'};
128 foreach my $type (qw(l d c b p s ?)) {
129 printf " %s=%d",$type,$SS{$type} if $SS{$type};
132 foreach my $type (keys %SS) { printf " %s=%d",$type,$SS{$type} }
136 exit ($found ? 0 : 1);
139 # collect files and build file lists
148 # loop over all argument files/directories
149 foreach $f (@files) {
151 # skip jed and emacs backup files
152 # next if $f =~ /~$/ and not $opt_a and not $opt_l;
160 warn "$0: dangling symlink $f\n";
167 # traverse real subdirs
168 if (-d $f and not -l $f) {
170 collect(getfiles($f));
175 # suppress trailing / on -d option
176 $f =~ s:/$:: if $opt_d;
178 # on trailing / list subdirs, too
179 if ($f =~ m:/$:) { &list(&getfiles($f)) }
180 elsif ($f eq '') { &list('/') }
184 warn "$0: dangling symlink $f\n";
201 # GLOBAL: @LIST (filenames-list)
204 my ($file,$line,$linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates);
207 foreach $file (@files) {
209 next if $opt_F and not fmatch($file);
210 next if $opt_N and (not -f $file or -l $file);
212 # get file information
213 # if ($opt_L and stat $file or not $opt_L and lstat $file) {
215 ($linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates) = &info($file);
216 } elsif ($! eq "Permission denied") {
218 $inode = $links = $size = $uid = $gid = '?';
219 $mode = $opt_l ? '?---------' : '?---';
220 $date = '????-??-?? ??:??:??';
221 %dates = ('m' => 0, 'a' => 0, 'c' => 0);
223 warn "$0: ".quote($file)." - $!\n";
231 next if $older =~ /-/ and $day gt $older;
232 next if $older !~ /-/ and $dates{m} > time-$older;
235 next if $newer =~ /-/ and $day lt $newer;
236 next if $newer !~ /-/ and $dates{m} < time-$newer;
239 if (defined $linkname) {
241 # prepend sorting string
243 $line = sprintf '%21s',$date if $opt_t;
244 $line = sprintf '%21s',$size if $opt_s;
247 $uid = substr($uid,0,8);
248 $gid = substr($gid,0,8);
251 # user defined format?
253 foreach my $i (split '',$opt_f) {
256 if ($i eq 'm') { $line .= sprintf '%06o ', $mode }
257 elsif ($i eq 'u') { $line .= sprintf '%6d ', $uid }
258 elsif ($i eq 'g') { $line .= sprintf '%6d ', $gid }
259 elsif ($i eq 's') { $line .= sprintf "$z%16s ",$size }
260 elsif ($i eq 'l') { $line .= sprintf '%3s ', $links }
261 elsif ($i eq 'i') { $line .= sprintf '%14s ', $inode }
262 elsif ($i eq 'd') { $line .= sprintf '%10s ', $date }
263 elsif ($i eq 'a') { $line .= sprintf '%10s %10s %10s ',
264 $dates{'a'},$dates{'m'},$dates{'c'} }
266 if ($i eq 'm') { $line .= $mode.' ' }
267 elsif ($i eq 'u') { $line .= sprintf '%-8s ', $uid }
268 elsif ($i eq 'g') { $line .= sprintf '%-8s ', $gid }
269 elsif ($i eq 's') { $line .= sprintf "$z%19s ",$size }
270 elsif ($i eq 'l') { $line .= sprintf '%3s ', $links }
271 elsif ($i eq 'i') { $line .= sprintf '%14s ', $inode }
272 elsif ($i eq 'd') { $line .= $date.' ' }
273 elsif ($i eq 'D') { $line .= $date.' ' }
274 elsif ($i eq 'a') { $line .= &isodate($dates{'a'}).' '.
275 &isodate($dates{'m'}).' '.
276 &isodate($dates{'c'}).' ' }
277 elsif ($i eq 'A') { $line .= &isodate($dates{'a'}).' '.
278 &isodate($dates{'m'}).' '.
279 &isodate($dates{'c'}).' ' }
287 if ($opt_l) { $line .= sprintf "%06o %6d %6d $z%15s %10d ",
288 $mode,$uid,$gid,$size,$date }
289 else { $line .= sprintf "%06o $z%15s %10d ",
292 if ($opt_l) { $line .= sprintf "%s %-8s %-8s $z%19s %s ",
293 $mode,$uid,$gid,$size,$date }
294 else { $line .= sprintf "%s $z%19s %s ",
295 $mode,$size,substr($date,0,-3) }
298 if ($opt_i) { $line .= sprintf '%3s %10s ',$links,$inode }
301 $line .= $linkname."\n";
313 warn "$0: cannot get dir-info for ".quote($file)." - $!\n";
319 # get file information
323 # OUTPUT: filename with linkname, inode, hard link count, size, mode string,
327 my ($linkname,$links,$mode,$bmode,$uid,$gid,$date,%dates,@stat);
330 my @rwx = qw/--- --x -w- -wx r-- r-x rw- rwx/;
333 if ($opt_L) { @stat = stat $file }
334 else { @stat = lstat $file }
341 %dates = ('m' => $stat[9],
348 $date = $dates{$sdf};
350 $uid = getpwuid($stat[4]) || $stat[4];
351 $gid = getgrgid($stat[5]) || $stat[5];
352 $date = &isodate($dates{$sdf});
355 if (-f _) { $type = '-'; $size = $stat[7]; }
356 elsif (!$opt_L && -l _) { $type = 'l'; }
357 elsif (-d _) { $type = 'd'; }
358 elsif (-c _) { $type = 'c'; $size = &nodes($stat[6]); }
359 elsif (-b _) { $type = 'b'; $size = &nodes($stat[6]); }
360 elsif (-p _) { $type = 'p'; }
361 elsif (-S _) { $type = 's'; }
362 else { $type = '?'; }
366 $size = $stat[7] if $size eq '-';
369 $mode = $rwx[$bmode & 7];
371 $mode = $rwx[$bmode & 7] . $mode;
373 $mode = $rwx[$bmode & 7] . $mode;
374 substr($mode,2,1) =~ tr/-x/Ss/ if -u _;
375 substr($mode,5,1) =~ tr/-x/Ss/ if -g _;
376 substr($mode,8,1) =~ tr/-x/Tt/ if -k _;
379 # with short list display only effektive file access modes
383 . (-x _ ? 'X' : '-');
384 substr($mode,2,1) =~ tr/-x/Ss/ if -u _ or -g _;
385 substr($mode,3,1) =~ tr/-x/Tt/ if -k _;
389 # fall back to ls command if perl lstat failed
394 ($mode,$links,$uid,$gid,$size) = split /\s+/,`ls -ld $file 2>/dev/null`;
395 return undef unless defined $mode;
396 $type = substr($mode,0,1);
397 # for (my $i=0;$i<3;$i++) { push @dates,'????-??-?? ??:??:??' }
398 # $date = `gfind $dir -maxdepth 1 -name $file -printf '%Ty-%Tm-%Td %TT\n'`;
402 # summarize statistics
406 $Ss += $size if $type eq '-';
411 # determine longest size field
413 my $x = length $size;
414 $opt_z = $x if $x>$opt_z;
416 $linkname = ${'opt_*'} ? $file : quote($file) ;
417 if ($type eq 'l' and $opt_f !~ /n/) {
418 my $link = readlink($file);
420 $linkname .= ' -> ' . (${'opt_*'} ? $link : quote($link));
424 #$mode .= ' ' unless $mode =~ /\+$/;
426 return ($linkname,$inode,$links,$size,$mode,$uid,$gid,$date,%dates);
430 # reformat integer into 3-digit doted format
431 # (when non-numerical mode is set)
433 # INPUT: integer or '-'
438 if ($opt_n) { s/-/0/ }
439 else { while (s/(\d)(\d\d\d\b)/$1,$2/) {} }
444 # get all files matching pattern $opt_m
446 # INPUT: directory to scan
448 # OUTPUT: files which match (sorted, directories first)
455 if (opendir D,$dir) {
456 $dir = '' if $dir eq '.';
457 while (defined($f = readdir D)) {
459 # skip . and .. pseudo-subdirs
460 next if $f =~ m:(^|/)\.\.?/*$:;
461 # skip ONTAP snapshot dir
462 next if $f =~ m:(^|/)\.snapshot/*$:;
465 # skip jed and emacs backup files
466 # next if $f =~ /~$/ and not $opt_a and not $opt_l;
468 if ($f =~ /$opt_m/) {
470 if (not -l $x and -d $x and not ($opt_R or $postsort or $opt_U)) {
479 @files = &$lcsort(@files);
480 @dirs = &$lcsort(@dirs);
483 warn "$0: cannot read $dir : $!\n";
486 return (@dirs,@files);
490 # reformat integer to string node
492 # INPUT: integer node
494 # OUTPUT: string node
497 return sprintf("%03d,%03d", ($rdev >> 8) & 255, $rdev & 255);
501 # reformat timetick to ISO date string
505 # OUTPUT: ISO date string
507 my @d = localtime shift;
508 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
509 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
513 # quote file name to printable name and escape shell meta chars
515 # INPUT: original file name
517 # OUTPUT: printable file name
520 my $mc = '\'\[\]\\\\ `"$?&<>$*()|{};';
522 unless (defined $_) {
527 if (s/[\000-\037\200-\237\241-\250]/?/g or /\'/) {
530 # } elsif (/[$mc]/ or -d and /:/) {
540 my $link = readlink($file)||'';
542 return $file if basename($file) =~ /$opt_F/i;
543 return $link if basename($link) =~ /$opt_F/i;
548 my $opts = '[-lastcuidnrzLRNS*] [-f format] [-D X:Y]';
550 print "usage: $0 $opts [-F regexp] [file...]\n";
553 print "usage: lf $opts regexp [directory...]\n";
555 options: -l long list
556 -a list also .* files
559 -U sort by nothing (original i-node order)
560 -c list status change time instead of modification time
561 -u list last access time instead of modification time
562 -i list also inode and hard links numbers
563 -d do not list contents of diretories
566 -z squeeze size field (slows down output)
567 -L derefernce symbolic links
568 -R recursive into subdirs
569 -F find files matching case insensitive regexp
570 -N show only normal (regular) files
571 -S print statistics summary at end
572 -* list plain file names (without masking \\)
573 -f user defined format output, format characters are:
574 m=mode, u=user, g=group, s=size, l=hard links count, i=inode
575 n=name only, d=date, a=access+modification+inodechange dates
576 -D list only files newer than X and older than Y
577 XY format: NUMBER[smhd] (s=seconds, m=minutes, h=hours, d=days)
578 XY format: YYYY-MM-DD (Y=year, M=month, D=day)
586 l *.c # list files ending with .c
587 l -la # list all files in long format
588 l -Rrs # list files recursive reverse sorted by size
589 l -*f mus # list files native names with format: mode+user+size
590 l -D 10d: # list files newer than 10 days
591 ll # list files long format (equal to: l -l)
592 lll # list files extra long format (equal to: l -liS)
593 lf 'status.*mp3' # list files recursive matching regexp (equal to: l -RF)
594 lf sda3 /dev # list devices matching sda3 (equal to: l -RF sd3 /dev)