6 Linux Commands to Make Life Easier

By Lorna Jane Mitchell
09 December 2010 | Category: Code
I spend a lot of my life at the command line and rarely use a platform other than linux, and I'd like to share some of the commands that make my life easier while I'm looking at that flashing prompt. These include man, the command to rule all commands, tools to find things, view files, and to find changes between files as well as how to work with remote files.
Man
Man is the Manual Page, and holds all the instructions for all the various commands and how to control them. Personally I often find I can’t remember the correct usage for different programs so I use the man page quite frequently. The first part of the output is the most useful, it shows how to use a command and gives an overview of what it does.

The usage patten shows what arguments to pass to the command. It also shows the additional “switches” you can pass, usually either a minus sign followed by a single letter (these are always case sensitive) or two minus signs followed by a word. Items in square brackets are optional, and the different switches are described further down the page. If you are lucky, you will also see included in this output some examples of using the program in question.
Understanding the man page is the key to being able to research and use any of the commands on your *nix-based system.
Grep
Grep looks for strings or regular expressions in files. You can ask it to look for more or less any pattern in more or less any type of file, including into directories. It’s like “Find in Files”, but better. I mostly use it to check where a function is used before I delete/rename it, or to check what functions are in a given class.
Trivia: Grep is not actually a word in its own right, in fact it stands for “global / regular expression / print”. I’ve been using this command for years and thinking it was a word in its own right!
Grep takes a number of arguments, I mostly use -R to search recursively and -i for a case-insensitive match. You also supply the pattern you would like to search for; grep supports regular expressions so there are no limits to the power of this tool. Finally, you supply a pattern for the files you want to search in – this can also come from stdin so you could pipe the output of another command in to grep. In the example below, grep is used to check all the function declarations in the Zend Framework JSON class.

Diff
The diff command shows the difference between two files. This is especially useful if you are looking to see what changed between two copies of code where at least one is not under version control (for example on a production server!).
Diff is simple to use, simply use “diff” with the two paths or files you want to compare. The man page will show the many options available. The switch I use most often is the -w to ignore whitespace, since I don’t care if the indentation levels have changed (I’m a PHP coder!) In this example, a fairly long file had a single line removed between the two versions

Understanding the diff output can be a bit tricky for humans, the output is marked up so that the output can be used to apply the changes to another file (called patching!). The beginning of the output shows which file has the plus signs and which has the minus signs – and the output shows line numbers with some context around the changes. You can even configure how much context you see using the -U switch as shown in this example. There are several tools around to make viewing diffs easier – check out Beyond Compare as one example, and also check if your IDE has this functionality built in.
Less
Less is an improved version of the command “more” – and it is definitely true that “less is more” in this instance! It is a simple paginator that lets you look through a large file, without having to open it in your editor. You can also use less in combination with other commands where the output is too long to view on a terminal; less lets you read it a page at a time – see below where less was used to view a draft of this article.

Wget
If you just need to download something onto a server, use the wget command – this is really valuable for grabbing libraries without pulling them onto your local machine and then pushing them back out to a server. The syntax is very simple – just wget and supply the file you need to download:

Curl
Curl, or cURL as it should be more correctly written, is the C URL library and as its name implies, is a way of requesting URLs from the command line. This is very powerful for checking that web requests and responses are behaving as expected and as a web developer I use curl rather often!
Curl allows you to make all kinds of HTTP requests, and to inspect the headers of both the request and the response. So for example to see the headers and response that you get from google, you could do:

The -I (capital i) switch means that only the headers of the response are displayed without the page content, and there are a few other switches that I use regularly for curl:
- -X to specify which HTTP verb to use
- -d to pass in data, you can supply a key=value pair with each -d switch, and use as many as you like
- -v to see details of both request and response
Using the Command Line
There really wasn’t enough time to show you everything about the command line but I hope there are some tricks there which will help someone next time they find themselves at a flashing prompt with work to do. If you have a favourite tip that you think should have been included, please share by leaving a comment!
Follow @thinkvitamin on Twitter Please check out Treehouse

