Useful find Commands

The find command is very verstile, and I use it all the time. Here are the parameters that I use most often:
-name <string> Finds files in specified directory whose name matches (case-sensitive) string
-iname <string> Finds files in specified directory whose name matches (case-insensitive) string
-exec Executes a command on the files found (see ls -l example below)
-perm <mode> Finds files in specified directory that match the permission mode
-size Finds files in specified directory
-print Finds the files and prints out the filename. This is useful in a find . -exec grep
-regex<pattern> Finds files in specified directory with name that matches regular expression pattern.
-atime <+-><n> Finds files in specified directory that were accessed less than, more than, or exactly n minutes ago
-amin <+-><n> Finds files in the specified directory that were accessed less than, more than, or exactly n minutes ago
-mmin <+-><n> Finds files in specified directory that were last modified less than, more than, or exactly n minutes ago
-mtime <+-><n> Finds files in specified directory that were last modified less than, more than, or exactly n*24 hours ago
-type <filetype> Find files in the specified directory of type:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link; this is never true if the -L option
or the -follow option is in effect, unless the
symbolic link is broken. If you want to search for
symbolic links when -L is in effect, use -xtype.
s socket

Examples:
Find files in current directory that end in .txt:
find . -name "*.txt"

Find files in current directory that were created within the last hour:
find . -ctime -60

Find 5 largest files
find / -type f -exec ls -la {} + 2>/dev/null | sort -k5 -rn | head -5

Find files in current directory not owned by root:
find . \! -user root -print

Find files in current directory with 0777 permissions and chmod them to 644:
find . -type f -perm 0777 -print -exec chmod 644 {} \;

Find files in current directory with owner apache and change to www-data:
find . -user apache -exec chown www-data {} \;

Find all files in current directory that contain string and output their names:
find . -exec grep string {} \; -print

Find files in the current directory with ACLs set:
find . -type f -exec ls -l {} \; | grep -v "\+"

Find files older than 7 days matching string “vicki_test2 and move to new directory
mkdir -p /var/mitto/data/vicki_dir && find /var/mitto/data/ -regextype posix-extended -regex '^.*vicki_test2.*\.[^.]+$' -mtime +7 -exec mv {} /var/mitto/data/vicki_dir \;


Posted in Linux | Comments Off on Useful find Commands

Method to keep private libraries private

One method I have seen programmers (including myself) use to keep folks out of private libraries / include modules is to put a check in there.

Easiest example:

in index.php (or any script with permission to be called directly), this line, immediately after the “

Posted in Linux | Comments Off on Method to keep private libraries private

Recovering Corrupted InnoDB Table

Corrupted MySQL database:

If you suspect data corruption in a MySQL database,

First make a copy of the database for safe keeping:

mysqldump –user=root post_planner > /tmp/post_plannerdb.sql

Prove it:

To prove that the database is corrupted, do:

mysql> check database

Find the table:

To detemine which table has corruption, do this for each table that you suspect:

mysql> check table post_planner.posted;
+—————————-+—————-+
| Table | Op | Msg_type | Msg_text |
+—————————-+—————-+
| post_planner.posted | check | Error | Can’t open table |
| post_planner.posted | check | error | Corrupt |
+—————————-+—————-+
2 rows in set (0.01 sec)

Once you know that the table is corrupt, you need to recover it. This is easy in MyISAM tables with the CHECK TABLEand REPAIR TABLE, but that doesn’t work with InnoDB. InnoDB tables can be checked with CHECK TABLE, but not repaired with REPAIR TABLE.

Copying over the good data to a new databases:

Here is what I did to fix it:

I created a second table called posted2 like this:

create table posted2 like posted;

SO that gets you the structure. Now lets see what we can do about getting the data:

mysql> insert ignore into posted2 select * from posted limit 1000000;
Query OK, 1000000 rows affected (0.00 sec)
Records: 1000000 Duplicates: 0 Warnings: 0

Keep increasing the limit until you see that the number of Records is fewer than the number you requested. At this point, do the following:

select max(id) from posted2;

This will give you how many records were successfully imported before it encountered corruption. Then you increment through with a the rest of the data using the first record that is not corrupted. You might have to try skipping one by selecting all records that are greater than the last known good record +1. It that fails, skip two on the next attempt. The command to skip the records basically looks like this:

mysql> insert ignore into posted2 select * from posted where ID>1000001;
Query OK, 9999999 rows affected (0.00 sec)

If you encounter more corruption, lather, rinse, repeat

Posted in Linux | Comments Off on Recovering Corrupted InnoDB Table

Restricting sshd by username

Step # 1: Open sshd_config file

# vi /etc/ssh/sshd_config

Step # 2: Specify a user

Only allow user king to login by adding following line:
AllowUsers king

Step # 3: Restart sshd

Save and close the file. In the above example, user vivek has already been created on the system. Now just restart sshd:
# service sshd restart

Posted in Linux | Comments Off on Restricting sshd by username

Changing the MySQL timezone via PHP

If you don’t have root access in your mysql instance and want to change the time_zone, you’ll have to change it in the code:

To have the time zone setup as you would like, you would you need to execute the following command each time you make a connection:
SET time_zone=’-3:00′

Here is an example of how to achieve that in PHP:

$offset = ‘-3:00’
$db = new \PDO(‘mysql:host=localhost;dbname=test’, ‘dbuser’, ‘dbpassword’);
$db->exec(“SET time_zone=’$offset’;”);

To verify:

mysql> select now()

Posted in Linux | Comments Off on Changing the MySQL timezone via PHP

Letting WordPress users install themes, plugins without FTP credentials

Quick answer: Put this in the user’s wp-config.php file:
define(‘FS_METHOD’, ‘direct’);

If you are having trouble updating the core wordpress, remember that no matter what the permissions are, wordpress needs it to be owned by apache:apache.

Posted in Linux | Comments Off on Letting WordPress users install themes, plugins without FTP credentials

Redirecting MySQL

To redirect traffic from one db to another (as a temporary measure if you have multiple places in your app to change the login data) you might try this:

iptables -t nat -A PREROUTING -p tcp –dport 3306 -j DNAT –to :3306

I ran across this when a customer wanted to redirect traffic from the mysql instance on his server to the CloudDB that he was creating. He wanted to make sure that if he missed an update in the app, he stil got data. I definitely put the YMMV with this one!

Posted in Linux | Comments Off on Redirecting MySQL

Which script sent this email

Sometimes you have multiple php scripts that send email and want to find out which one sent a specific email. I followed the instructions here:

http://www.iezzi.ch/archives/217

Worked like a charm.

Posted in Linux | Leave a comment

Browser-Side Caching with Apache

Browser-side caching relies on HTTP headers to know whether or not the content it has in its local cache is still valid. If it is, then it uses the local copy, rather than downloading the file from the server again. Many sites like whysoslow.com will report that you’re missing this HTTP header on your static files, which is normal, since you aren’t explicitly setting that in the files, since they’re mostly images and the like. In order to improve browser-side caching of your static content you could add mod_expires to your Apache configuration. This would add the necessary HTTP headers to make browser-side caching of your static content possible.

Install mod_expires and add the following to the .htaccess:

# Deflate output configuration
#
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/javascript
#AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

ExpiresActive On
ExpiresDefault “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType text/css “access plus 1 month”

AddType image/x-icon .ico
ExpiresByType image/x-icon “access plus 1 year”
ExpiresByType image/ico “access plus 1 years”

Posted in Apache, Linux | Leave a comment

Magento Notes

Deleting magento cache:

You may safely delete the content of var/cache/* and var/session/* or use the “Refresh Cache” functionality within the Admin.

To determine magento version:
include ‘app/Mage.php’;
php > echo Mage::getVersion();
1.6.2.0
php >exit();

Posted in Uncategorized | Leave a comment