Quick Scripts

From wiki.alexcollins.org
Jump to: navigation, search

Contents

Introduction

I've always needed a place to store code that I use often, or that I find useful and want to keep just in case. A wiki is perfect for this as it allows me to add quickly, and for others to do the same. I hope this article is of some use to people.

I would like to declare, however, that any code found here is used at the your own risk. I hold no liability, expressed or implied, nor is there warranty of any kind.

Code placed on this page is available under the terms set out at the bottom of this page (namely GNU Free Documentation License 1.2).

MySQL

Create a user

   GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost' IDENTIFIED BY 'some_pass' WITH GRANT OPTION;

Replace monty with your desired username. *.* means ALL databases and ALL tables, so you may want to change this; the format is DATABASENAME.TABLE

Please note that in never versions of MySQL (5.0.2 and up), it's preferred to use the CREATE USER statement:

   CREATE USER 'monty'@'localhost' IDENTIFIED BY 'password'

Backup/Restore

To backup all MySQL databases, run the following:

   mysqldump -u root -ppassword -x --all-databases > backup.sql

To restore a MySQL database:

   mysql -u root -ppassword < backup.sql

BASH

Find Files Under Current Directory Greater Than 10MB

The following bash one-liner will print all files under the current directory (i.e. in sub-directories too) that are greater that 10MB. The output is piped into du to show actual size in human-readable form:

find . -size +10240k -exec du -h '{}' \;

Convert FLAC to MP3

for file in *.flac; do $(flac -cd "$file" | lame -V 0 - "/tmp/${file%.flac}.mp3"); done

Perl

List files modified today

   die "Usage: scriptname.pl directory" if @ARGV < 1;
   my $BASE_DATA_DIR = @ARGV[0];
   opendir(DIR,$BASE_DATA_DIR) || die("Could not open $BASE_DATA_DIR for reading!");
   my @files = readdir(DIR);
   use constant DAY_IN_SECONDS => 60 * 60 * 60 * 24;
   print_file_list(\@files);
   closedir(DIR);
   # prints all files in $BASE_DATA_DIR that are modified today
   sub print_file_list {
       my $file_list = shift;
       foreach my $file (@$file_list) {
           my $now = time;
           my $epoch_yest = $now-DAY_IN_SECONDS;
           my $write_secs = (stat("$BASE_DATA_DIR/$file"))[9];
           if($write_secs > $epoch_yest) {
               # less than a day old, this file was modified today
               print "$file was created today";
           }
       }
   }

PHP

List all files in the current directory in a table

Lists all the files in a given directory with modification time and size in KBs (excluding those beginning with '.')

   <?php
   /* 
    * Lists the files in the current directory. Safer option than providing the default apache directory listing
    */ 
   $dir = "/some/dir/";?>
   <table name="filesTbl" border="1px">
   <tr>
   <th>Filename</th><th>Modification Date</th><th>Size (KB)</th>
   </tr>
   <?php
   if (is_dir($dir)) {
       if ($dh = opendir($dir)) {
           while (($file = readdir($dh)) !== false) {
               if(strpos($file,".") && $file != "index.php") {
                   echo "<tr><td><a href=\"$file\">$file</a></td><td>";
                   echo date("Y-m-d",filemtime("$dir$file")) . "</td><td>"; 
                   echo number_format((filesize("$dir$file")/1024),2) . "</td></tr>";
               }
           }
       }
   }
   ?>
   </table>

Debian/Ubuntu

List packages that aren't in a repository in sources.list

This is a nice way to list packages that no longer are in the debian repositories (make sure you have installed the apt-show-versions package).

 apt-show-versions -b |grep 'No available'

And to print only the package names:

 apt-show-versions -b |grep 'No available' | awk '{print $1}'

List uninstalled packages that still have config files

 deborphan --find-config

To get rid of those (apt-get remove won't work):

 sudo dpkg --purge `deborphan --find-config`

To prevent this happening, provide the '--purge' option when executing 'apt-get remove packagename'. This removes any directories and configuration files related to the package you're removing.

List unused libraries

 deborphan

To remove (potentially dangerous, make sure you know what you're doing):

 apt-get remove --purge `deborphan`

Check integrity of installed files

You must have the 'debsums' application installed:

 #First install it
 apt-get install debsums
 
 #Then create md5sums for packages we don't have info about (just ignore any errors, they are from .debs
 #that weren't installed with apt)
 apt-get --reinstall -d install `debsums -l`

To list any changed files (run as root to make sure we have read permissions for all files):

 sudo debsums -c

To list changed configuration files:

 sudo debsums -se

Listing and Restoring Installed Packages

The following set of commands are useful for backup and restoring a server's packages when something's gone sour:

 dpkg --get-selections > /etc/package.selections # save the list of installed packages

To restore the installed packages at a later date, or on a newly installed Debian machine:

 dpkg --set-selections < /etc/package.selections && apt-get dselect-upgrade

Thanks to Keith Edmunds of the Hampshire Linux User Group for this one.

Reinstall config files

To reinstall config files, delete the config file you want to reinstall, then do the following

 apt-get -o DPkg::Options::="--force-confmiss" --reinstall install PACKAGE_NAME

where PACKAGE_NAME is the name of the package that contains the config file you want to replace

Personal tools
Namespaces
Variants
Actions
Navigation
Eternal Lands
alexcollins.org
Toolbox