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


What do you have to say?

Show Editing Help

Europython

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

gutes Qualitätscasino

July 3, 2009
The paragraph is the most basic block in a reST document. Paragraphs are simply chunks of text separated by one or more blank lines. As in Python, indentation is significant ...
An Introduction to ReStructuredText

sreejith

July 3, 2009
I want to download a file from remote server in binary format. Can anyone let me know the command to do so? Thanks in advance
PuTTY Series: Using PSFTP

jythlkedl;rg

July 2, 2009
????? ??? ????????? ?????? ?? ???????? ? ????????? ???, ? ??????? ??? ??????? ??????? ? ??? ?? ???? ?? ?? ????? ???????????????? ??????????????????. ??????????????????? ????? ?? ?? ????, ?, ???, ...
Burning an iso to CD on Windows

gbi-service-ru

July 1, 2009
???? ?????????, ?????????? ?? ?? ? ???"??? ??????, ?????????? ??? ?? ???? ? ????, ? ? ?????. ?? ??? ???? ???? ??? ???. ?? ?????????? ???? ?? ?. ???????? ?, ...
Burning an iso to CD on Windows

seo techniques

July 1, 2009
I would like to thank you for the inforamtion you have put on this article no matter.
Only the penitent man will pass - on captchas and cotton wool

Online Craps lernen

July 1, 2009
I would like to thank you for the making these clarifications in such a detailed manner to rebuilt the communication and enhancing the strategies of the organization which could be ...
Disclaimer: NO WARRANTY

ZK@Web Marketing Blog

July 1, 2009
Django is an amazing web framework; we built a lot of features in a very short period of time and Django [mostly] stayed out of our way. Last night as ...
Baby Steps with Django - Part 4 Django Applications and flow

overnight payday loans

July 1, 2009
I found commandline.org.uk very informative. The article is professionally written and I feel like the author knows the subject very well. commandline.org.uk keep it that way.
Only the penitent man will pass - on captchas and cotton wool

Drogo

June 30, 2009
Gotta agree with your sentiments about many modern games. The cost of a new game is prohibitive, especially for consoles (although I've noticed that PS2 games have crashed in price ...
Retro British Gaming - Part 3: Amstrad CPC Games

pppiohooddd

June 29, 2009
Free vadult video site! http://crech.us/ 1000 free video every day!
OpenSolaris, Gobuntu, and be careful who you kiss

Tesyimasystus

June 29, 2009
...Love this dude!!! http://www.esnips.com/doc/79c22395-7bd6-4299-92db-cf392e381698/kutiman---this-is-what-it-became Peace
5 Homebrew Python Games

Simon Tite

June 28, 2009
twitterfall is still there, I just tried it, and to me it beats Visible Tweets hands down. Problem with Visible Tweets: * Extremely **irritating** animations! (There are three available, but ...
Visualising your favourite keywords in Twitter

piffAltetle

June 28, 2009
??? ??? ???? ???????????? ??????,?????????? ???? ?????? ??????????? ???????,??????????? ????? mp3,??????? ??????????? ??????.
Encrypt your /home this Christmas: part three - moving your data to the encrypted partition

idhyougjdsyhfr

June 26, 2009
SMS Trap is something that never fails to help you get your partner off guard? Our software will make reading other people?s SMS as easy as ABC. Ready for some ...
Burning an iso to CD on Windows

Sozdanie-saitov-com

June 26, 2009
???? ???????? ? ?????????????????? ????????????? - ??? - ???? ?? ????? ?????. ???? ?? ??? ? ??????? ?????? ????????? ?????! ???, ?????23126 sozdanie-saitov.com@mail.ru
Burning an iso to CD on Windows

gameskillz

June 26, 2009
Killzone 2 - the best PS3 game yet?Still LittleBigPlanet for me, but Sony's new shooter is mightily impressive. What you think about my web? http://www.easyfaxlesspaydayloan.com/payday-loans-online.html
Email Syntax Check in Python

Anish

June 25, 2009
hey Moritz, Check this http://commandline.org.uk/python/my-merry-five-minutes-with-bazaar/
Setting up a bazaar server

gbi zavod 177

June 24, 2009
???? ?????????, ?????????? ?? ?? ? ???"??? ??????, ?????????? ??? ?? ???? ? ????, ? ? ?????. ?? ??? ???? ???? ??? ???. ?? ?????????? ???? ?? ?. ???????? ?, ...
Burning an iso to CD on Windows

vettone

June 24, 2009
??? ????? ????? ????,??????? ?? ???,????? ???? ???.????? ?? ??????????? ????,?????????? ?????? ???????? ?? ????.???? ????????: http://euro-football.ucoz.com ????? ???? ??????????.
Burning an iso to CD on Windows

tuegjhg78kjfhuey

June 23, 2009
? ???????????????? ???? ??? ???, ?? ?????? ?? ?????????, ???, ???????????????? ??? ??????????, ???? ????? ??? ??? http://remont.ucoz.ua/
Burning an iso to CD on Windows