Tag Archives: Command Line

Recessively search for a string within files using grep

To recursively search all files in a directory for ‘SearchText’ and print out matching files you can use the grep command with the ‘–files-with-matches / -l’ and ‘–recursive / -r’ flags. For example, to search the current directory (‘.’):

grep -rl "SearchText" .

Note, if you run this on a directory with lots of subdirectories (e.g., your home folder) it will take a long time to run.

You can specify to only include files matching a pattern using the ‘–include’ flag:

grep -rl --include "*py" "SearchText" .

or exclude files matching a pattern using the ‘–exclude’ flag.

grep -rl --exclude "*pyc" "SearchText" .

For more info see the man page (available online here) or the full manual available at http://www.gnu.org/software/grep/manual/.

UNIX Commands I wish I’d known earlier

I’ve been using command line UNIX / Linux for a while but like many people have just picked up bits as and when I’ve needed them. Here are some tips I wish I’d known when I started out.

  1. Use tab to autocomplete.
  2. This is one of the ones that’s really basic but not mentioned in some tutorials. As you start typing a command or file path just hit tab to complete. You just need to type enough letters for the command / path to be identified.

  3. Use Ctrl+r to search previous commands
  4. You can use the up arrow to cycle through previous commands, a slightly cooler trick is typing ctrl+r to search through your command history. Just press and start typing.

  5. Learn to use an editor from the command line (It doesn’t have to be vi!)
  6. If you’re logged into a server using ssh, being able to quickly open a text file and edit a few lines is very handy, and a lot easier than downloading the file, editing and uploading again. A lot of guides use vi, which is amazingly powerful, pretty much universally available but has a really steep learning curve. I love vi (or more accurately vim, which is an updated version) but it took a lot of effort to get to a stage where I was proficient enough for it to be useful. There are much more user friendly alternatives such as nano or ne. Nano is installed with OS X and most Linux distributions, ne will need to be installed but has more features and familiar commands (ctrl+s to save etc.), you can double-tab escape to bring up a menu bar.

  7. Use tmux, screen or byobu to keep sessions running when you log out.
  8. If you’re logged into a machine over ssh and running an interactive proccess it will often stop when you close the ssh connection. Using GNU screen, tmux or byobu, will allow you to detach your session and the process will continue in the background. You can reattach to check the progress. These also allow you to have multiple terminals within the same ssh session.

  9. Think before you press enter.
  10. An obvious one but on a UNIX system with the right commands you can do pretty much anything, this does mean you can do some ridiculously stupid things, especially with the sudo command. Poor use of rm with wild characters has caught me out before (luckily by this time I’d developed somewhat of a paranoid backup system!). You can always use ls before running rm to check which files will be removed.

    If you’re worried about breaking something on your computer learning the command line, you could set up a virtual machine (e.g., using VirtualBox) with linux while your learning and then if anything goes badly wrong you can just delete the machine and start again.

  11. Googling bits as you need them is no substitute for actually sitting down and learning it.
  12. If it looks like your going to be spending a lot of time using the command line of a UNIX / Linux system (and it’s a very useful skill to have), as with anything worth learning you need to invest the time. There are lots of tutorials on the internet and books available, you may find some more suitable than others. My personal favourite is The Linux Command Line by William Shotts, you can download the PDF for free or buy the hard copy, more information is available here