When you’re using a *nix based operating system (Linux, OS X etc.) it’s easy to underestimate the power and speed of the command line. Here’s a list of my top 10 cli tools.
lsof gives you a list of every currently open file on the system in addition it provides a ton of other useful info such as who has the file open and if applicable what program and process has it open.
cat outputs the provided file to the screen. Very useful when you want to pipe ( | ) the output to another program. (I’ll go more in depth on piping in a later posting)
top shows us a listing of the “top” running processes (top is reatlive it can show the “top” memory consumers, “top” cpu users etc.) The beauty of top is that it updates in real time.
dig is similar to (but more powerful than) nslookup for Windows. You can use dig to do DNS lookups. For example if I wanted to find the IP address for a host like say , www.pownce.com I’d run the following query
“dig A www.pownce.com”
telling dig I’d like to do an “A” record lookup on www.pownce.com. I’ll then get the following result.
; <<>> DiG 9.4.1-P1 <<>> A www.pownce.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63464
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 4
;; QUESTION SECTION:
;www.pownce.com. IN A
;; ANSWER SECTION:
www.pownce.com. 1126 IN A 220.127.116.11
;; AUTHORITY SECTION:
pownce.com. 71348 IN NS ns1.everydns.net.
pownce.com. 71348 IN NS ns2.everydns.net.
pownce.com. 71348 IN NS ns3.everydns.net.
pownce.com. 71348 IN NS ns4.everydns.net.
;; ADDITIONAL SECTION:
ns1.everydns.net. 138211 IN A 18.104.22.168
ns2.everydns.net. 138211 IN A 22.214.171.124
ns3.everydns.net. 138211 IN A 126.96.36.199
ns4.everydns.net. 138211 IN A 188.8.131.52
;; Query time: 62 msec
;; SERVER: 184.108.40.206#53(220.127.116.11)
;; WHEN: Sun Nov 18 22:46:43 2007
;; MSG SIZE rcvd: 196
Which shows us that www.pownce.com resolves to the IP address 18.104.22.168
dig has a whole bunch more options and command line switches I’ll go into this further if there’s interest.
I don’t think I’ve gone through an entire day without using scp. The scp command allows you to copy files to or from any machine running an ssh server you can even copy something from a remote machine to another remote machine (Which should be any *nix box worth it’s salt) It’s fast it’s quick and it’s easy and doesn’t involve any sort of mounting. One of the enjoyable side effects is that all the data is encrypted from machine to machine (we always like encryption). Syntax is “scp source destination” if the source or destination is remote you need to preface the filename with username@hostname: so if I wanted to copy a file named test.txt in the root folder of my local machine to my home folder on a remote server named host.server.org I’d run ” scp /test.txt email@example.com:~ ” (~ is a shortcut to your home folder).
ps shows every running process for the current user. It’s invaluable for finding the pid or process ID of a program you need to kill.
Tail shows you the last 10 lines of a file. I use this almost every day to check the recent output from a log file. Were this become especially useful to me is when you add the command line argument ” -f ” this makes tail continue to update in real time. so you can watch a log file for example update in real time. I use this to watch email logs when I’m testing mail accounts or potential problems.
screen is a whole subject in and of itself. It’s almost like a window manager for the terminal. It has a plethora of keyboard shortcuts that allow you to send a terminal to the background while allowing the program to continue running. Where I use this is in running things like Half-Life 2 death match servers. They output a lot of data to the screen and it’s nice to be able to start them running background them and then be able to pull up those terminal sessions again to run a couple commands or just watch the console output. I could write an essay on all the options and shortcuts in screen so I’ll cut this off here.
This is my de-facto text editor I know the whole emacs vs. vi debate still rages. But I tend to think that most people simply become comfortable with whatever they learned first and that was vi for me. vi is one of those programs where I’m probably using about 2% of the power available to me and I find that oddly comforting. There will always be shortcuts for me to find to make my work day easier or less redundant. I can’t go a single workday without having vi open for 50% or more of it.
grep is a decent search utility but that’s not the power to me. Where I find the power is combining it with other commands such as tail or cat. What grep does is return a line in which it finds a match (you can alter this to include lines before or after as well). What I often do is if I’m having problems with a specific email address for example I’ll tail -f the mail log file but pipe ( | ) it to grep so
“tail -f /var/log/mail.log |grep firstname.lastname@example.org”
so if I leave this running for a while I’ll come back and any lines that have been added containing “email@example.com” will now be showing on my screen.
Well I hope this has put someone on to at least one command they’ve ignored or underused. Please let me know what your most used command line apps or commands are.