UnixFu

From Zoelife4U Wiki
(Redirected from Work:UnixFu)
Jump to: navigation, search

Contents

Unix Fu !!

Bash Command Line

Generate MD5 Password

perl -e'use Digest::MD5 qw(md5_hex);print md5_hex("password")."\n";'

A Shell Script Version

#!/bin/sh
/usr/bin/perl -e"use Digest::MD5 qw(md5_hex);print md5_hex($1);"
echo "\n";

Delete all error logs on the account:

   find public_html/ -name error_log -exec rm -f {} \;

Search all files in a path for a string or filename:

   grep -Rl <search query> <path>

Example:

   grep -Rl 0666 public_html/

A Fancier Grep to Display Filename, Line # And the Content in Question

   grep -HRn <search query> <path or files to search> | sed -e 's/:/\ | \ /g'

Example:

   grep -HRn antastico ./| sed -e 's/:/\ | \ /g'

Bit more Verbose:

grep -HRn "use" ~/*.pl | sed -e 's/:/\ | \ /g'                                [04/27/09][14:59:07]
/home/jjones/strictenv.pl |  26 |  use strict;
/home/jjones/strictenv.pl |  27 |  use warnings;
/home/jjones/temp.pl |  25 |  use strict;
/home/jjones/temp.pl |  26 |  use warnings;
/home/jjones/temp.pl |  30 |  use DBI;
/home/jjones/temp.pl |  32 |  #use Date |   |  Calc; # Ended up not needing this yet..
/home/jjones/temp.pl |  33 |  #use Data |   |  Dumper; # For quick data display, never used
/home/jjones/temp.pl |  39 |      $dbuser, $dbpass,  $dsn,    $dbh,    $sth,
/home/jjones/temp.pl |  63 |  $dbh = DBI->connect_cached( $dsn, $dbuser, $dbpass, )
/home/jjones/temp.pl |  104 |  # I understand there exists in-house modules to make this prettier, but I don't seem
/home/jjones/test.pl |  21 |  use strict;
/home/jjones/test.pl |  22 |  use warnings;

View Slow MySQL Queries

Be sure to change YYYYMMDD-HH to the actual files name, like 20090214-05

awk '/^use/ {print $2}' ~/tmp/mysql_slow_queries/YYYYMMDD-HH.log | sort | uniq -c | sort -nr | head

Copy a file to all directories (Bash)

 
   find * -type d|xargs -i cp --verbose filename.ext {}/.

Diff switches

Side by Side diff listing (File 1 on the Left, File 2 on the Right)

   diff -y file1 file2

Contextual diff listing with Line numbers removed to line numbers added

   diff -u file1 file2

View All Processes By The Logged In User (Bash)

   (date; ps waux | awk '{print $1 " CPU: " $3 " Memory: " $5 " Command: " $11 "<br />"}' | sort | uniq ) | grep `whoami` | grep -v $0

Kill All Processes Owned By The Logged In User (Bash)

Tested on FreeBSD 6.3 and CentOS 4.4 and Ubuntu 8.04

   kill -9 `ps -U \`whoami\` -o "pid="`;

This works too and doesn't kill your SSH Session:

 ps aux | grep `whoami` | grep ramdisk | awk '{ print $2 }' | xargs -i kill -9 {};

Acquiring The System Load

Useful for including the System Load in Scripts, etc.

Linux

 cat /proc/loadavg | awk '{ print $1 $2 $3 }'

FreeBSD

 sysctl vm.loadavg | awk '{ print $3 " " $4 " " $5 }'

Formatted Uptime Value

This has use for scripts etc, looks the same on CentOS 4.4, FreeBSD 6.3 and Ubuntu 8.04

 uptime | sed 's/\,.*user.*,/,/'

Remove String of Text from all files

HTML files in this case

 for i in `grep -Rl 124.217.252.62 ~/public_html`;do [ -e $i ]  | sed -i 's/<iframe.*><\/iframe>//g' $i; done;

Find all files modified in the Last 24 hours

find ./ -mtime -1

PHP Scripts

Copy a file to all directories (PHP)

   <?php system("find * -type d|xargs -i cp --verbose filename.ext {}/."); ?>

View All Processes By The Logged In User (PHP)

Copy this anywhere in the web accessable file tree and name it viewproc.php

<?php
$exec = '(date; ps waux | awk \'{print $1 " CPU: " $3 " Memory: " $5 " Command: " $11 "<br />"}\' | sort | uniq ) | grep `whoami` | grep -v $0';
system($exec);
?>

Kill All Processes Owned By The Logged In User (PHP)

Copy this to any web accessable directory and name it killall.php

Note this has not yet been tested to see if it kills all the processes or gets killed before finishing. Update: I've ran this a couple times and it appears to kill itself before killing everything else..

<?php
$kill='kill -9 `ps -U \`whoami\` -o "pid="`';
echo "<strong>Killing all processes owned by " .`whoami`." now</strong><br />\n\n";
system($kill);
?>

Perl Scripts

View All Processes By The Logged In User (Perl/Command Line)

#!/usr/bin/perl -w
 
use strict;
use warnings;
 
my $exec = q((date; ps waux | awk '{print $1 " CPU: " $3 " Memory: " $5 " Command: " $11 }' | sort | uniq ) | grep `whoami` | grep -v $0);
system($exec);
Personal tools
Online Users
Zoelife4U: