My Favorite Linux Command Line Tricks

Dual Linux
My laptop, android phone, and a Raspberry Pi plugged into a crappy hotel TV, all running terminals.  This happened while trying to compile Sage on the Pi in January, 2012.

This week I’m at the IMA workshop on Modern Applications of Representation Theory.  So far it’s been really cool!

One of the graduate students asked me about how one goes about learning the Linux command line, so I thought I would write down a few of the things I think are most useful on a day-to-day basis.  Such a list is sure to stir controversy, so feel free to comment if you see a grievous omission.  In fact, I learned the command line mainly through installing Gentoo Linux back before there was any automation whatsoever in the process, and suffering through lengthy forum posts getting every bit of my system more-or-less working.  (Note: Starting with Gentoo is probably a bad idea.  I chose it at the time because it had the best forums, but there are probably better places to start these days.  I mainly use Xubuntu these days.)

So, off to the races. I’m going to skip the really, really basic ones, like ls, cd, apt-get and sudo.  In fact, there’s a nice tutorial at LinuxCommand.org which covers a lot of the basics, including IO redirection.  Finally, I’m assuming that one is using the bash terminal.

  • man:
    Pulls up the manual page of most commands, a life-saver in a pinch.  Try reading the bash manual page if you have a bunch of free time; it’s worth it.  In spite of Jefferson’s idealism, not all men are created equal.
  • cron:
    Run specified programs at prescheduled times, or at boot time.  Cron itself is the name of the program; it’s a daemon, meaning it is always running in the background, invisibly doing what it’s meant to do.  The actual list of commands lives in a file called a crontab, which is typically edited using the ‘crontab’ command (as root) which just shows you the system’s crontab file in an editor.  To learn the proper layout of the crontab file, try ‘man 5 crontab’.
    Cron is useful for automating mindless tasks, and is thus the favored tool of sys-admins everywhere.  I’m pretty sure there are people whose entire careers consist of writing shell scripts and then running them with cron.
  • ctrl+r:
    Reverse-search through the last couple thousand commands you’ve used in bash.  Hit ctrl+r and start typing and Bash will start searching for the last command that used your typed characters.  Typing something partial and then hitting ctrl+r again will then cycle back through all commands that match what you’ve typed.
    Note that to get back a very recent command, you can just hit the up button repeatedly.
    This is has probably saved hours of my life.  Use it, love it.
  • Tab completion:
    If you start typing a command, you can finish it by hitting ‘tab.’  If there are more than one possible completions, hitting tab twice will show you a list of everything that matches.
  • screen:
    Screen is a program for running other programs in the background.  This is useful when you want to, say, ssh into your department server, start an enormously long computation, and then log out while having the computation continue.  It also saves you from losing work due to lost network connections or accidentally closed terminal windows.  Just type ‘screen’ to start a screen session.  To leave the screen session (keeping it running in the background), hit ‘ctrl+a+d’. We then say that the screen is ‘detached.’  To see what screens you have running, type ‘screen -ls’.  And to reattach to a detached screen, type ‘screen -r NUM’, where NUM is the number that appears in ‘screen -ls’.
  • dmesg | tail:
    Shows the last few lines of your system log.  Note that the pipe character ‘|’ takes the output of dmesg (which is ALL of the system log since the last boot) and runs it into a program called ‘tail’ which just shows the end of whatever you feed it.  To see, say, fifty lines of the log, try ‘dmesg | tail -n 50’.  By the way, most logs live in ‘/var/log’, if you want to read them directly.
  • grep:
    Grep is the sledgehammer of the command line.  It searches for text.  It’s really common to pipe output of other programs into grep.  For example, ‘dmesg | grep usb’ will show all lines of the current system log containing the phrase ‘usb’.  Grep can also handle regular expressions, if you’re feeling fancy.  The -A and -B options show a number of lines after/before the searched terms, which is occasionally useful.
  • top:
    Shows what processes are hogging your cpu and/or memory.  You can then kill them with ‘pkill’.
  • ifconfig, ping, and nmap:
    The ‘ifconfig’ command lists your current network connections, including your IP and MAC addresses.  ‘ping’ sends a very short packet somewhere, and is useful for seeing if your internet connection is working.  Try ‘ping -c 5 google.com’, for example.  Finally, ‘nmap’ is useful for scanning your local network for other machines.  For example, my IP address as I write this might be something like 192.168.42.137.  So I know that my local network is probably assigning all of its connections as 192.168.42.X, with X between 1 and 254.  Then you can run: ‘nmap 192.168.42.0/24 -sP’ to silently ‘ping’ every address 192.168.42.X and get a report of who ponged.  I tend to use this to figure out where my Raspberry Pi is on the wireless network so I can SSH in.

Ok, that’s what I’ve got off the top of my head… Any suggestions for more?

2 thoughts on “My Favorite Linux Command Line Tricks

  1. Ed Hamilton July 28, 2014 / 11:08 am

    Thank you very much for the -B and -A options in grep. Long ago I had a Norton Utility that did that in DOS and was looking for something similar in Linux. (If only I had read the manual :). With color enabled for grep, it will help find the context of a term I am searching for.

    Also, I will try Crtl-R to search for the last commands. Right now I am using Page-Up after typing a bit of the command, but to do so in Debian based distrobutions you have to edit the /etc/inputrc file and uncomment the appropriate lines.

  2. Anne French July 31, 2014 / 7:59 am

    Check out “Unix Power Tools”. My favorite Unix reference ever (well, other than google).

Comments are closed.