From d5021abc07a20689a582940ec50178f64e4d4d24 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Mon, 13 Nov 2017 15:14:57 +0100 Subject: [PATCH] Add --xdev capability to files.pl This allows one to easily grasp the file sizes on a given mount point. E.g. on my laptop, I'm running out of disk space on my root partition. Now I can run the following command to easily find large files: sudo files.pl --xdev / | flamegraph.pl > /tmp/out.svg Previously, I'd have to find a custom whitelist of folders in / to pass to `files.pl` explicitly. --- files.pl | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/files.pl b/files.pl index 27654be6..50426b2e 100755 --- a/files.pl +++ b/files.pl @@ -16,25 +16,44 @@ use File::Find; sub usage { - print STDERR "USAGE: $0 directory\n"; + print STDERR "USAGE: $0 [--xdev] [DIRECTORY]...\n"; print STDERR " eg, $0 /Users\n"; + print STDERR " To not descend directories on other filesystems:"; + print STDERR " eg, $0 --xdev /\n"; print STDERR "Intended to be piped to flamegraph.pl. Full example:\n"; print STDERR " $0 /Users | flamegraph.pl " . "--hash --countname=bytes > files.svg\n"; print STDERR " $0 /usr /home /root /etc | flamegraph.pl " . "--hash --countname=bytes > files.svg\n"; + print STDERR " $0 --xdev / | flamegraph.pl " . + "--hash --countname=bytes > files.svg\n"; exit 1; } usage() if @ARGV == 0 or $ARGV[0] eq "--help" or $ARGV[0] eq "-h"; +my $filter_xdev = 0; +my $xdev_id; + foreach my $dir (@ARGV) { - find(\&wanted, $dir); + if ($dir eq "--xdev") { + $filter_xdev = 1; + } else { + find(\&wanted, $dir); + } } sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = lstat($_); return unless defined $size; + if ($filter_xdev) { + if (!$xdev_id) { + $xdev_id = $dev; + } elsif ($xdev_id ne $dev) { + $File::Find::prune = 1; + return; + } + } my $path = $File::Find::name; $path =~ tr/\//;/; # delimiter $path =~ tr/;.a-zA-Z0-9-/_/c; # ditch whitespace and other chars