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.

  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?

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


What do you have to say?

Show Editing Help

About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

Essex Web Design

September 3, 2010
A lot of contract providers give you free internet usage now, but if you have Pay As You Go, then you are going to be paying heavy prices.
Calling time on mobile internet nonsense?

Krasochka

September 2, 2010
Hack again?!
Adding more terminals to your function keys

GenryFlorist

September 2, 2010
<b>Cheap flowers delivery around the world!</b> Celebrate summer with our gorgeous flowers. They?re the perfect gift for any summer occasion. From birthdays to anniversaries, we offer beautiful flowers, lush plants, ...
Burning an iso to CD on Windows

auto-financing.co.cc

September 2, 2010
auto-financing
ReStructuredText tables and doctests

rubaxa

September 1, 2010
FTP = NOT RANDOM software Dominated hands postflop suckout often on all-ins. EX. AK vs. A9 or KQ vs. K6. Both players hit top pair. Bad player goes all in ...
Burning an iso to CD on Windows

empodayaddelm

September 1, 2010
Sorry admin - my post is test
This Week: Heroes and Monsters

increase synthroid dosage

September 1, 2010
Latest world news: 1 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/viagra-cialis-buy-no>buy cheap cialis generic levitra viagra</a> Viagra 2 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/buy-viagra-online-at-lowest>rainbowpush discussion board buy viagra</a> Viagra 3 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/how-to-get-generic-brand>search viagra ...
SFTP in Python: Paramiko

Lacilslaw

September 1, 2010
HYUN JAIMIE enniless and homele JAMILA
This Week: Heroes and Monsters

domaserisk

August 31, 2010
who was shaking his head back and forth knowingly Grissom shifted his eyes over at Brass,
How I Removed Windows from my Laptop

get ready loan

August 30, 2010
Though, by the you kill the legitimate PC user from visiting the site. Also, think about the dynamic IP's issue.
Only the penitent man will pass - on captchas and cotton wool

Packers and movers in pune

August 30, 2010
The topic you disscussed here is very amazing, informative and useful in future...
On Comment Spam

serhanters1

August 30, 2010
?? ???????? ??... ??????...... ??. ????????? ??? ??? ???????D ???????? ??. ????? ????? ???? ??? ???=) ?? ?????- http://letitbit.net/download/8746.894a84bc20f38f1661895aeee0/stereokartinki.html ???http://f-zona.ru ? ? ?? ????????????
Burning an iso to CD on Windows

strona startowa

August 29, 2010
Thanks For This Post, was added to my bookmarks.
Python CGI contact forms

lerexottori

August 29, 2010
?????????????? ??????????
Adding more terminals to your function keys

KelpAugmeme

August 29, 2010
aofaapsymp, http://forums.quark.com/members/jennaq.aspx online stock trading broker, rdgofzary
PuTTY Series: Adding PuTTY to your system path

Cheeday

August 28, 2010
What flowers do you like?
This Week: Heroes and Monsters

magfcvb

August 28, 2010
??????? ?????????????? ?????? - ????? ?????? ?????????????? ??????, ?????????????? ?????? ???????, ?????????????? ?????? crosman, ???? ??????????????? ??????, ?????????????? ?????? ?????? ????????. ???? magazin-oruzhie.ru
Include ODF support in the Linux Standard Base?

noni

August 28, 2010
I find myself coming to your blog more and more often to the point where my visits are almost daily now!
On Comment Spam

Latenadsfes

August 28, 2010
http://mynewblog.for-breastcancer.com/ http://mynewblog.photoblogcentral.com/ http://ilovezebras.thechicks.org/ http://mynewblog.cyberbardsymposium.com/ http://wewphost.com/ilovezebras/
Burning an iso to CD on Windows

LeupoldEst

August 28, 2010
pretty cool stuff here thank you!!!!!!!
OOXML Vote Coverage