Five useful command one liners

I had a browse through my shell history (history | less), and there are some interesting commands that I have used recently. The really experienced command line warriors among you will probably know them already, but it never hurts to have a reminder.

1. How to migrate a MySQL database

If you want to export data from an MySQL server on one server, and then import the data on a new server, this is pretty easy when you know how, just use the redirect symbols, greater than ('>') and less than ('<').

On the first server use the following command:

mysqldump --user root -p database-name > filename.dump

This will give you a new file called filename.dump, transfer that file to the second server using SSH or however else you want.

On the second Server, you can import the data with the following command:

mysql -u root -p -D database-name < filename.dump

2. How to grep a large number of files.

The following command searches for files that contain the word 'term':

grep term *

If you grep over a very large number of files, grep will fail with "the parameter list is too long".

The way around this is to use xargs and pipe the filenames to grep.

find . -print0 | xargs -0 grep -H term

This approach uses null characters instead of newlines to separate the results. This means things won't go screwy if there are files with white space or newlines in the file names, e.g. files that have come from Windows or are on a Windows partition.

3 How to force rpm install

I don't like the RPM tool, there are more modern, i.e. less brain-dead, package managers available. Anyway if you have a machine with an RPM based distribution that is completely messed up, you may want to reinstall packages that have gone wrong. RPM does not let you reinstall the same version over itself, however you can force it, here is how:

sudo rpm -iv --replacepkgs --replacefiles packagefile.rpm

4 Mounting Mac Disk images

Mounting regular disk images in the ISO format is easy:

sudo mount -t iso9660 -o loop filename.iso /media/iso

What about Macintosh disk images (commonly known as DMG format)?

Well we have a similar plan, we want to go:

sudo mount -t hfs -o loop filename.dmg /media/iso

Or for newer files:

sudo mount -t hfsplus -o loop filename.dmg /media/iso

There are a couple of problems that might arise:

  • You might not have the relevant kernel modules for reading hfs or hfsplus.
  • It might not be a true .dmg file. Sometimes the files are archived in a zip or tar, even though the files may end .dmg.

The second one can be sorted out pretty quickly using the trusty file command. For example:

file GnuPG1.4.8.dmg

Results in:

GnuPG1.4.8.dmg: Macintosh HFS Extended version 4 data (mounted) (unclean) last mounted by: 'H+Lx', created: Tue Dec 30 17:24:08 2003, last modified: Wed Dec 24 03:40:12 2008, last checked: Tue Dec 30 18:24:08 2003, block size: 4096, number of blocks: 4096, free blocks: 1377

This is a true dmg file in hfsplus format.

  1. Don't forget to watch

If you want to repeat a command many times, for example you are monitoring something, then don't forget about the watch command. It will print the results of the command to screen every 2 seconds (you can change the interval with -n).

I tend to use watch with disposable one-off Python scripts for which writing a proper interface is overkill. However you can also use watch with sys-admin commands. Lets do some generic and clichéd examples.

Watching the disk space:

watch --no-title "df -h"

Watching who is logged in:

watch --no-title who

Watching the current IP Address on eth0:

watch --no-title "ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | cut -d ' '  -f1"

You will know what commands you use and how watch can be helpful.

Conclusion

So that was some of the commands from my shell history. What commands have you been using recently?

11 thoughts on “Five useful command one liners

  1. <p>I'd recommend checking out a program called 'ack' in lieu of using find + xargs + grep. Also, check out htop and dstat are fun.</p>

  2. <p>Trick '1' can be done easier, if one of both MySQL daemons is reachable via network...</p>
    <p>mysqldump -uroot -p database-name -h hostname-A | mysql -uroot -p database-name -h hostname-B</p>

  3. <p>Much like the unneccesary use of 'cat' that you see in many posted scripts (i.e. cat /some/file | grep &quot;mypattern&quot; instead of just grep &quot;mypattern&quot; /some/file), tip 2 is an unnecessary use of find! <img src="/static/forum/img/smilies/smile.png"></p>
    <p>To duplicate that functionality with just grep you can use:</p>
    <p><tt class="docutils literal"><span class="pre">grep</span> <span class="pre">-R</span> <span class="pre">term</span></tt></p>
    <p>Not only is it less things to type and less syntax to get wrong, but it may be quicker because with the xargs version will cause multiple grep invocations (depending on just how many files there are).</p>
    <p>Of course with the find command you could have a lot more flexibility in trimming out items you don't want (though grep does have '--include/--exclude' options), controlling the depth of recursion and those kind of things.</p>
    <p>The great thing about CLI interfaces? There are so many ways to accomplish a given task!</p>

  4. <p>I have actually setup a site to store just short commands... <a class="reference external" href="http://txt.binnyva.com/">http://txt.binnyva.com/</a></p>

  5. <p>Is the watch command you're describing a Linuxism?</p>
    <p>On my FreeBSD box, &quot;man watch&quot; seems to be describing something completely different.</p>

  6. <p>CorkyAgain, good question, I don't have a FreeBSD box available at the moment so I can't comment. On Linux at least watch does as I have described.</p>

  7. <p>Great tips! I have had occasion to do a lot of MySQL instance migrations lately, so here is an improvement for Trick 1:</p>
    <p>mysqldump &lt;DATABASE_NAME&gt; [mysqldump_options] | gzip -c | ssh <a class="reference external" href="mailto:user&#64;remotehost">user&#64;remotehost</a> &quot;gunzip -c &gt; mysql &lt;DATABASE_NAME&gt; [mysql_options]&quot;</p>
    <p>This creates the dump, compresses it, securely transfers to the remote host via SSH, decompresses the dump, and imports it into the new instance. Fastest possible method, no files to cleanup!</p>

  8. <p>&#64;CorkyAgain: On FreeBSD (I tested on a 7.0), you can have this watch by installing the port gnu-watch.</p>
    <p>% sudo pkg_add -r gnu-watch</p>
    <p>% gnu-watch --no-title &quot;df -h&quot;</p>

  9. <p>Nice one. A post on watch <a class="reference external" href="http://unstableme.blogspot.com/2008/03/execute-program-periodically-bash.html">http://unstableme.blogspot.com/2008/03/execute-program-periodically-bash.html</a></p>

How about Global Thermonuclear War? Wouldn't you prefer a good game of chess? Powered by zpress