Five useful command one liners
24 December 2008
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.
- 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?



1 Sam says...
I'd recommend checking out a program called 'ack' in lieu of using find + xargs + grep. Also, check out htop and dstat are fun.
Posted at 1:23 p.m. on December 24, 2008
2 eliasp says...
Trick '1' can be done easier, if one of both MySQL daemons is reachable via network...
mysqldump -uroot -p database-name -h hostname-A | mysql -uroot -p database-name -h hostname-B
Posted at 1:51 p.m. on December 24, 2008
3 tob says...
"man watch"
;-) thx didn't know this one
Posted at 2:05 p.m. on December 24, 2008
4 Troy Curtis says...
Much like the unneccesary use of 'cat' that you see in many posted scripts (i.e. cat /some/file | grep "mypattern" instead of just grep "mypattern" /some/file), tip 2 is an unnecessary use of find! :)
To duplicate that functionality with just grep you can use:
grep -R term
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).
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.
The great thing about CLI interfaces? There are so many ways to accomplish a given task!
Posted at 2:06 p.m. on December 24, 2008
5 tulcod says...
as for the grep problem, it's probably easier to do: grep needle -R .
Posted at 2:20 p.m. on December 24, 2008
6 Binny V A says...
I have actually setup a site to store just short commands... http://txt.binnyva.com/
Posted at 6:32 p.m. on December 25, 2008
7 CorkyAgain says...
Is the watch command you're describing a Linuxism?
On my FreeBSD box, "man watch" seems to be describing something completely different.
Posted at 6:46 p.m. on December 25, 2008
8 Zeth says...
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.
Posted at 4:53 p.m. on December 26, 2008
9 Samuel Huckins says...
Great tips! I have had occasion to do a lot of MySQL instance migrations lately, so here is an improvement for Trick 1:
mysqldump <DATABASE_NAME> [mysqldump_options] | gzip -c | ssh user@remotehost "gunzip -c > mysql <DATABASE_NAME> [mysql_options]"
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!
Posted at 5:04 a.m. on January 4, 2009
10 Stéphane Bortzmeyer says...
@CorkyAgain: On FreeBSD (I tested on a 7.0), you can have this watch by installing the port gnu-watch.
% sudo pkg_add -r gnu-watch
% gnu-watch --no-title "df -h"
Posted at 10:15 p.m. on January 10, 2009
11 Jadu Saikia says...
Nice one. A post on watch http://unstableme.blogspot.com/2008/03/execute-program-periodically-bash.html
Posted at 12:54 p.m. on August 13, 2009