Curated by Carsonified

Learn with Treehouse

Accessibility, CSS3, Design, Django, HTML & CSS, HTML5, JavaScript, jQuery, NoSQL, PHP, Responsive Web Design, Ruby, Ruby on Rails, Tools, UX, Version Control, WordPress, iOS and more

Article 11

6 Linux Commands to Make Life Easier

By

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.

Give a Think Vitamin Membership gift subscription to someone for Christmas

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

Other Posts You Might Find Interesting

  • Sorry - No Related Posts Found

Comments

  • http://codeutopia.net Jani Hartikainen

    I would suggest checking out “ack”, which is a grep-like tool, but at least for programming-related searching it’s much better. http://www.betterthangrep.com/

  • Pedro

    awesome, thanks!

  • Visus

    Good post! thanks!
    As for diff, I recommand the GUI version ‘meld’ as well. It’s particularly useful when you need to diff two versions of directory

  • Jehan

    Yes, these are most useful. Also I quite often use ‘find’ command to locate files. Even more useful when find and grep combined as follows to make a content based search.

    http://lkamal.blogspot.com/2010/10/linux-find-content-filenames-search.html

  • http://andytson.com/ Andy Thompson

    My favourite is xargs, used in conjunction with other commands like find it makes working with a recursive file structure more pleasant,

    e.g. syntax check all php files

    find -name ‘*.php’ | xargs -I {} php -l {}

    make only directories group executable

    find -type d | xargs chmod g+x

    add all unversioned files (I don’t like svn add –force .)

    svn status | grep ^? | cut -b 9- | xargs svn add

  • http://chelakkandupoda.blogspot.com ചെലക്കാണ്ട് പോടാ

    more and more commands i need :P

  • http://twitter.com/craig552uk Craig Russell

    Without a shadow of a doubt the most valuable command in Linux is the pipe.
    |
    Learn it. Love it.

    I’d also add that the forward slash is useful for finding content in files (using less) and man pages too.

  • http://twitter.com/stefeee juanirams

    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.
    http://www.articlespeak.com/renadex-male-penis-enhancement-review/

  • http://lornajane.myopenid.com/ Lorna Jane Mitchell

    Enjoying reading which commands each of you would have included in this post – so far they’re all excellent recommendations!

  • http://twitter.com/AirElecCigratte AirElectronicCigaret

    Linux was started by the users, is developed by the users, and is maintained and supported by the users.
    http://healthproductadvice.com/air-electronic-cigarette-reviews

  • http://twitter.com/ronnyandre Ronny-André

    wget isn’t installed in Mac OS by default, but you can use cURL instead like this:

    curl -O http://domain.com/file.zip

Learn Web Design!

Ads Via The Deck

Think Vitamin Radio
Episode #34: Amazon Fire and Responsive Roundtable

Check out our bi-weekly radio show. Covering the hot topics on the web.

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

Download Podcast as mp3

Advisory Board

The Think Vitamin Advisory Board in place make sure that you receive the best content possible.