Linux History Clean Script

Flattr this
Tweet this: Linux History Clean Script
Dent this: Linux History Clean Script

Da für mich unter Linux die history der Shell sehr wichtig ist und die mit der zeit dann doch auch recht vollgemüllt mit gleichen einträgen ist, hab ich mir ein kleines PHP-Script gebastelt mit dem ich die History aufräumen kann ohne die reihenfolge oder ähnliches zu verändern.

Während die mir bekannten Shell-Einzeiler mit uniq und sort alle die Reihenfolge innerhalb der History kaputt gemacht haben, hab ich mir ein kleines Script mit PHP gebastelt, das eine Datei einliest und jeweils das letzte vorkommen einer Zeile wieder in eine Datei schreibt, so bleibt die Reihenfolge innerhalb der history komplett erhalten.

Das Script einfach irgendwo abspeichern und dann einen Symlink in einem Ordner innerhalb von PATH setzen, dann kann man es von überall aufrufen.

usage: cleanfile [OPTION] FILE
cleanHistory 0.2
-h, --help Shows this help
-d, --debug Shows debug information after cleaning the file
#! /usr/bin/php
<?php
// Copyright (c) 2008-2009, Benjamin Steininger (Robo47)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright notice,
//      this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of Robo47 nor the names of its contributors may be
//       used to endorse or promote products derived from this software without
//       specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.

define('startTime', microtime(true));
error_reporting(E_ALL | E_STRICT);

define('CLEAN_HISTORY_VERSION',     '0.2');
define('FILE_NOT_FOUND',            1);
define('FILE_NOT_READABLE',         2);
define('FILE_NOT_WRITEABLE',        3);
define('NO_FILE',                   4);


function showHelp($binary) {
    echo 'usage: ' . basename($binary) . ' [OPTION] FILE' . PHP_EOL;
    echo 'cleanHistory ' . CLEAN_HISTORY_VERSION . PHP_EOL;
    echo ' -h, --help   Shows this help' . PHP_EOL;
    echo ' -d, --debug  Shows debug information after cleaning the file' .
         PHP_EOL;
}

$debug = false;
if ($argc > 3) {
    echo 'too many parameters';
    return 0;
}
$count = 1;
foreach($argv as $key => $arg) {
    switch($arg) {
        case '--help':
        case '-h':
            showHelp($argv[0]);
            return 0;
        break;
        case '--debug':
        case '-d':
            if ($key == 1) {
                $count++;
            }
            $debug = true;
        break;
    }
}

if (!isset($argv[$count])) {
    echo 'no file given' . PHP_EOL;
    return NO_FILE;
}

$sourceFile = $argv[$count];

if (!file_exists($sourceFile)) {
    echo 'File not found: ' . $sourceFile;
    return FILE_NOT_FOUND;
}
if (!is_readable($sourceFile)) {
    echo 'File not readable: ' . $sourceFile;
    return FILE_NOT_READABLE;
}
if (!is_writeable($sourceFile)) {
    echo 'Unable to write to file: ' . $sourceFile;
    return FILE_NOT_WRITEABLE;
}

$beforeSize = filesize($sourceFile);

// read sourcefile into array
$aSource = file($sourceFile);

// reverse array to filter
$aSource = array_reverse($aSource);
$aTarget = array();

// add all new lines to thenew array if they arent already in
foreach ($aSource as $line) {
    $line = trim($line);
    if (!isset($aTarget[$line]) && !empty($line)) {
        $aTarget[$line] = $line;
    }
}

// reverse array again for writing into file
$aTarget = array_reverse($aTarget);

file_put_contents($sourceFile, implode(PHP_EOL, $aTarget));

if (true === $debug) {
    $sizeAfter = filesize($sourceFile) / 1024;
    $beforeSize = $beforeSize / 1024;
    $memUsage = memory_get_peak_usage() / 1024;
    echo 'Time: ' . round(microtime(true) - startTime, 3) . PHP_EOL;
    echo 'lines before: ' . count($aSource) . PHP_EOL;
    echo 'lines after:  ' . count($aTarget) . PHP_EOL;
    echo 'size before:  ' . round($beforeSize, 2) . 'kb ' . PHP_EOL;
    echo 'size after:   ' . round($sizeAfter, 2) . 'kb ' . PHP_EOL;
    echo 'mem-peak    : ' . round($memUsage, 0) . 'kb' . PHP_EOL;
}
return 0;

Trackbacks (0)

Trackbackurl: http://www.robo47.net/trackback/snippet/8

Es sind keine Trackbacks vorhanden.


Kommentare (0)

Es sind noch keine Kommentare vorhanden.

Die Kommentare zu diesem Beitrag sind gesperrt.

You liked it ? Link it on your homepage or blog: