Linux Stuff

Tips and Tricks

Recover a deleted file (if a process is holding onto it)

pwillis@bobdobbs ~/ :( echo "this is a test" > foo.txt
pwillis@bobdobbs ~/ :) cat >> foo.txt &
[1] 30781

[1]+  Stopped                 cat >>foo.txt
pwillis@bobdobbs ~/ :) lsof | grep foo.txt
cat       30781    pwillis    1w      REG      253,1        15  472076057 /spln/pwillis/home/foo.txt
pwillis@bobdobbs ~/ :) rm foo.txt
rm: remove regular file `foo.txt'? y
pwillis@bobdobbs ~/ :) lsof | grep foo.txt
cat       30781    pwillis    1w      REG      253,1        15  472076057 /spln/pwillis/home/foo.txt (deleted)
pwillis@bobdobbs ~/ :) ls -l /proc/30781/fd
total 0
lrwx------ 1 pwillis pwillis 64 2009-12-28 09:58 0 -> /dev/pts/10
l-wx------ 1 pwillis pwillis 64 2009-12-28 09:58 1 -> /spln/pwillis/home/foo.txt\ (deleted)
lrwx------ 1 pwillis pwillis 64 2009-12-28 09:58 2 -> /dev/pts/10
pwillis@bobdobbs ~/ :) cp /proc/30781/fd/1 newfoo.txt
pwillis@bobdobbs ~/ :) cat newfoo.txt
this is a test

Count the amount of real memory (minus shared memory) Apache child processes are using, per user (works without smaps support, but not as exact)

#!/usr/bin/perl
opendir DIR,"/proc";
for ( grep /^\d+$/, readdir DIR ) {
    open F, "/proc/$_/status";
    $p{$1} = $2 while ( <F> =~ /^(\w+):\s+(\S+)/g );
    close F;
    open F, "/proc/$_/statm";
    @m = split /\s+/, <F>;
    close F;
    $h{ getpwuid($p{"Uid"}) } += ($m[1]-$m[2])  if ($p{"Name"} eq "httpd" and $p{"PPid"} ne "1");
}
while ( my ($k,$v) = each %h ) {
    print "Rss for $k: " . int( (($v*4096)/1024) /1024) . "MB\n";
}

Scan all IPv6 hosts found on the network via broadcast

ping6 -I eth0 -c 2 ff02::1 2>/dev/null  | grep -e "64 bytes" | awk '{print $4}' | sed -e 's/:$//g' | xargs -n 1 -I "{}" nmap -6 -sT -sV "{}%eth0"
Icon  Name                    Last modified      Size  Description
[DIR] Parent Directory - [TXT] HEADER.html 31-Mar-2010 16:00 1.8K