File: remove-leaf-dirs

package info (click to toggle)
policycoreutils 3.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,832 kB
  • sloc: ansic: 4,848; sh: 591; makefile: 497; perl: 20
file content (29 lines) | stat: -rwxr-xr-x 577 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/perl
use strict;

# Takes directories on stdin and puts a list on stdout where any directory
# that has another directory as it's stem is removed
# so if you have /a/b and /a/b/c then /a/b/c is removed

my @arr;

while(<STDIN>)
{
  chomp;
  # strip "" and "/" to avoid problems
  if(length($_) >1)
  {
    push(@arr, $_);
  }
}

for(my $i = 0; $i <= $#arr; $i++)
{
  print "$arr[$i]\n";
  my $stem = $arr[$i] . "/";
  my $stemlen = length($arr[$i]) + 1;
  while ($i + 1 <= $#arr and $stem eq substr($arr[$i + 1], 0, $stemlen))
  {
    splice(@arr, $i + 1, 1);
  }
}