Alpha
Share and Organize Code
login register
HotNewTop
F.A.Q.
1. To begin adding code first register.
2. Create your own tags for code that you add.
3. To see your own code click Me, to see all code click Everyone, or filter by people rob.
4. To follow a user, click on their username, rob and hit follow.
5. Add other code to your library by clicking the
button near the code titles (note that you can not edit these codes).
6. To create a copy of a code that you can edit use fork.
7. If you can't find your code, double check your filter settings as you can filter by Category, User and Tag simultaneously.
8. Add comments to a code by clicking on it's title or the comments link underneath it.

Select Elements That Have Any Portion Within the Viewing Window (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$.extend($.expr[':'],
{
    inView: function(el)
    {
        var offset = $(el).offset();

        return !(
            (offset.top > $(window).height() + $(document).scrollTop()) ||
            (offset.top + $(el).height() < $(document).scrollTop()) ||
            (offset.left > $(window).width() + $(document).scrollLeft()) ||
            (offset.left + $(el).width() < $(document).scrollLeft())
        )
    }
});

// usage
$(#container :inView")
[+] (13) more...
comments (0) flag fork

Content Editable in HTML5 (plain)

1
2
3
<div contentEditable>
blah blah
</div>
comments (0) flag fork

Hide Directory Structure from URL (plain)

1
2
# In httpd.conf or apache2.conf whatever the case may be...
Options -Indexes
comments (1) flag fork

Hide Apache Version Header (plain)

1
2
3
4
# In httpd.conf or apache2.conf whatever the case may be...
# Ensure the following 2 lines are set:
ServerTokens ProductOnly
ServerSignature Off
comments (0) flag fork

Show Amount of RAM Used (plain)

1
2
3
4
5
# In MB
free -m

# In GB
free -g
[+] (1) more...
comments (1) flag fork

Display Top 3 RAM Hogs (plain)

1
ps auxf | sort -nr -k 4 | head -3 | awk '{print $11 $12}'
comments (1) flag fork

Display Zombie Processes (plain)

1
ps aux | awk '{ print $8 " " $2 }' | grep -w Z
comments (1) flag fork

Find Worst Indexes in all Databases (plain)

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
30
31
32
33
34
35
36
37
38
39
40
/*
SQL script to grab the worst performing indexes in all databases
*/
SELECT
  t.TABLE_SCHEMA AS `db`
, t.TABLE_NAME AS `table`
, s.INDEX_NAME AS `index name`
, s.COLUMN_NAME AS `field name`
, s.SEQ_IN_INDEX `seq in index`
, s2.max_columns AS `# cols`
, s.CARDINALITY AS `card`
, t.TABLE_ROWS AS `est rows`
, ROUND(((s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) * 100), 2) AS `sel %`
FROM INFORMATION_SCHEMA.STATISTICS s
INNER JOIN INFORMATION_SCHEMA.TABLES t
  ON s.TABLE_SCHEMA = t.TABLE_SCHEMA
  AND s.TABLE_NAME = t.TABLE_NAME
INNER JOIN (
  SELECT
     TABLE_SCHEMA
   , TABLE_NAME
   , INDEX_NAME
   , MAX(SEQ_IN_INDEX) AS max_columns
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA != 'mysql'
  GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME
) AS s2
ON s.TABLE_SCHEMA = s2.TABLE_SCHEMA
AND s.TABLE_NAME = s2.TABLE_NAME
AND s.INDEX_NAME = s2.INDEX_NAME
WHERE t.TABLE_SCHEMA != 'mysql'                         /* Filter out the mysql system DB */
AND t.TABLE_ROWS > 10                                   /* Only tables with some rows */
AND s.CARDINALITY IS NOT NULL                           /* Need at least one non-NULL value in the field */
AND (s.CARDINALITY / IFNULL(t.TABLE_ROWS, 0.01)) < 1.00 /* Selectivity < 1.0 b/c unique indexes are perfect anyway */
ORDER BY `sel %`, s.TABLE_SCHEMA, s.TABLE_NAME          /* Switch to `sel %` DESC for best non-unique indexes */
LIMIT 20;
/*
Change this row limit to whatever you prefer
...
*/
[+] (36) more...
comments (1) flag fork

Find Fragmented Tables (plain)

1
2
3
SELECT table_schema,table_name,data_free
FROM information_schema.tables where table_schema
NOT IN ('information_schema','mysql') AND data_free > 0 ORDER BY table_schema, data_free DESC;
comments (0) flag fork

Undo potential damage caused by Magic Quotes (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (get_magic_quotes_gpc() == 1) {
    // Magic quotes *should* be off, but just in case...
    $superglobals = array(&$_REQUEST, &$_GET, &$_POST, &$_COOKIE);
    foreach ($superglobals as &$superglobal) {
        $superglobal = removeMagicQuotes($superglobal);
    }
}

function removeMagicQuotes($data) {
    if (is_array($data) == false) {
        $data = stripslashes($data);
    } else foreach ($data as &$value) {
        $value = Helper::removeMagicQuotes($value);
    }
    return $data;
}
[+] (12) more...
comments (1) flag fork

Create SVN repository and users (plain) (source)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
svnadmin create /path/to/<repo_name>
chown -R apache:apache /path/to/<repo_name>

vi /path/to/<repo_name>/conf/svnserve.conf
auth-access = write
password-db = passwd

<Location /svn>
    DAV svn
    SVNPath /home/svn/<sitename>
    Authtype Basic
    AuthName "My Repository"
    AuthUserFile /home/svn/mofodata/conf/passwd
    Require valid-user
</Location>
[+] (11) more...
comments (0) flag fork

Add php.ini to OS X (plain) (source)

1
2
3
sudo cp /private/etc/php.ini.default /private/etc/php.ini

;When no config is used, you may get a timezone error when parsing php
comments (0) flag fork

Display PHP Config Used (plain)

1
2
3
php -i | grep -i configuration

; Good for showing php.ini use and location
comments (0) flag fork

List Tables Using MyISAM (plain)

1
2
3
4
5
/* Within MySQL Shell you can use this: */

SELECT table_schema, table_name, engine FROM information_schema.tables WHERE engine='myisam' AND table_schema NOT IN ('mysql','performance_schema','information_schema');

/* This will list all tables using MyISAM excluding default mysql table schemas. */
[+] (1) more...
comments (0) flag fork

File Descriptor Limit (plain)

1
2
3
4
5
6
7
8
9
10
11
12
# Total file descriptor limit
sysctl fs.file-nr
# User limit
ulimit -Hn
# Find out how many file descriptors are in use by user
sudo lsof -u <username> 2>/dev/null | wc -l

# Increase file descriptor limit for a user
echo "username hard nofile 64000" >> /etc/security/limits.conf
echo "ulimit -n 64000" >> /home/username/.bashrc ; echo "ulimit -n 64000" >> /home/username/.bash_profile*

# tested in centos
[+] (8) more...
comments (1) flag fork

Remove Comments and Empty Lines (plain)

1
2
3
4
5
6
7
# Example using /etc/my.cnf to easily view DB config in Linux
grep -v -e ^# -e ^$ /etc/my.cnf

# copy to new file...
grep -v -e ^# -e ^$ /etc/my.cnf > /etc/my-nocomments.cnf

# tested in bash amazon ec2 instance ubuntu 12.04
[+] (3) more...
comments (0) flag fork

Bash While Loop (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
# Tested & Created in CentOS

ENVARRAY=(
dev
qa
prd
)

ENV=${#ENVARRAY[*]}
i=0

while [ $i -lt $ENV ]; do
        echo "${ENVARRAY[$i]}"
let i++
done

# Output will be
# dev
# qa
# prd
[+] (17) more...
comments (0) flag fork

Show Table Row Counts (plain)

1
2
SELECT TABLE_SCHEMA,TABLE_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IN ('db1','db2','db3') ORDER BY `TABLE_SCHEMA`, `TABLE_NAME`, `TABLE_ROWS`;
/* This will show you database table counts */
comments (0) flag fork

Size of All tables in MySQL (plain)

1
2
3
4
5
6
7
8
/* Size of ALL Table Schemas in GB and MB */
SELECT round(sum(((data_length + index_length)/1024/1024/1024)),2) GB, round(sum(((data_length + index_length)/1024/1024)),2) MB FROM information_schema.TABLES;

/* Size of ALL Table Schemas Ordered by Table Schema Name */
SELECT TABLE_SCHEMA, TABLE_NAME, round(((data_length + index_length)/1024/1024/1024),2) GB, round(((data_length + index_length)/1024/1024),2) MB FROM information_schema.TABLES ORDER BY TABLE_SCHEMA, TABLE_NAME;

/* You can also filter on
Database (Table_Schema) Name, Order by Size, etc */
[+] (4) more...
comments (0) flag fork

Top 3 Processes Using RAM (plain)

1
2
3
4
# after using free -m OR free -g ... you may like to know what's using up the RAM:
ps auxf | sort -nr -k 4 | head -3

# Tested / used on Ubuntu and CentOS
comments (0) flag fork

SSH - Convert Win Pub Key to Linux (plain)

1
2
3
4
5
6
7
# This assumes you generated a keypair on a Windows client with puttygen for a Linux server
ssh-keygen -i -f ~/.ssh/id_rsa_win.pub > ~/.ssh/id_rsa.pub
# OR convert it and add it directly to ~/.ssh/authorized_keys file on Linux server
ssh-keygen -i -f ~/.ssh/id_rsa_win.pub >> ~/.ssh/authorized_keys

# SSH key pair notes:
# PUB key always goes on server / PRIVATE key saved on local client
[+] (3) more...
comments (0) flag fork

Monitor Network, Disc, CPU, RAM Simultaneously (plain) (source)

1
2
3
4
# --- Install dstat: Improved systat like system monitoring tool
dstat -talm

# --- Very useful for any Sys Admin
comments (1) flag fork

SHOW tips (plain)

1
2
3
4
5
6
7
8
9
10
/* From your MySQL client you can get a lot of good info with SHOW cmds */
SHOW TABLE STATUS;
SHOW TABLE STATUS LIKE "%partial_table_name%";
SHOW TABLE STATUS WHERE Rows > 15000;
SHOW TABLE STATUS WHERE Rows > 15000 AND Index_length > 52564;

/* Some probably don't know you can have WHERE clauses in your SHOW cmds */


/* Enjoy */
[+] (6) more...
comments (0) flag fork

Generate GRANT Statements (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
mysql -N -ss -u root -p -e"SELECT CONCAT('SHOW GRANTS FOR ', user, '@', host, ';') FROM mysql.user;" > ~/showgrants.sql
sed -i "s/FOR /FOR '/g" ~/showgrants.sql
sed -i "s/@/'@'/g" ~/showgrants.sql
sed -i "s/;/';/g" ~/showgrants.sql
mysql < ~/showgrants.sql > ~/grants.sql

# --- OR

mysql -N -ss -u root -p -e"SELECT CONCAT('SHOW GRANTS FOR ', user, '@', host, ';') FROM mysql.user;" > ~/showgrants.sql
sed "s/FOR /FOR '/g" ~/showgrants.sql > ~/showgrantsa.sql
sed "s/@/'@'/g" ~/showgrantsa.sql > ~/showgrantsb.sql
sed "s/;/';/g" ~/showgrantsb.sql > ~/showgrantsc.sql
mysql -N -ss -u root -p < ~/showgrantsc.sql > ~/grants.sql

# --- Don't forget to add the mysql delimiters in ~/grants.sql since they aren't added by default.
# --- Feel free to alter or improve in anyway



#
[+] (17) more...
comments (1) flag fork

Whitelist urls for iOS (plain)

1
2
3
4
ADD: "EternalHosts" to PhoneGap.plist
SET VALUE in the array to "*" for all URLS
OR "www.url.com" for specific URL
ALSO *.url.com for all sub-domains for a given url
comments (0) flag fork

Display Table Schema Sizes (plain)

1
SELECT table_schema, sum(data_length+index_length)/1024/1024/1024 "Gigs" FROM information_schema.tables GROUP BY table_schema;
comments (1) flag fork

Execute Shell Script on Multiple Servers from 1 Host (plain)

1
2
3
4
5
6
7
8
9
# Bash Script
ssh -p 22 user@remotehost exec /bin/bash -s "$LE_PATH" < /usr/local/bin/shellscript
# Perl Script
ssh -p 22 user@remotehost exec /bin/perl -s "$LE_PATH" < /usr/local/bin/shellscript

# --- If you have a script you normally execute locally on a machine and you want to run your scripts on multiple machines from a centralized location, this is a good method.
# --- Just ensure you have key pair access set up accordingly between machines
#
#
[+] (5) more...
comments (1) flag fork

List and remove a stash (plain)

1
2
git stash list
git stash drop stash@{0}
comments (0) flag fork

Remove working directory changes and reset to last commit (plain)

1
git reset --hard
comments (0) flag fork

Hide Spinner when app loading (plain)

1
2
ADD: "ShowSplashScreenSpinner" to PhoneGap.plist
SET VALUE "NO" or FALSE
comments (0) flag fork