Ten Useful Terminal Commands for Developers

The terminal is a very powerful tool and once you grasp its basics; you’ll love it and use it in ways never imagined by the original developers. Here are some really useful commands.

1. Ls

I use this tool everyday and it is a workhorse. It is great for listing directory contents and checking file ownership and permissions. I normally use something like ls -alth, this means:

a – show all file contents (including hidden files)

l – display a long list

h – Human-readable format (for file sizes)

t – Sort the output by the most recently updated file.

Check the man files on ls for more information.

2. Grep

Grep is absolutely one of the most useful commands I have found; it find lines that match a pattern in some input.

For example, if I want to search for the file “username.txt” in a directory containing over 100 files; I don’t have to check each file; ls -a | grep “username.txt” is all I need!

To check if some file has some text:

grep text filename

Grep can also interpret regular expressions. For more information on grep; type info grep.

3. Less

“Less is more”; actually less is similar to the more command – both are text viewers; however less can do ‘more’ than more :). Its improvements including allowing bidirectional movement and faster load times.

I find the key bindings similar to vim’s; you can move through the lines using k & j and search for patterns by typing /pattern. To quit, press q.

4. Cat

Do you want to dump the contents of some file? Just cat filename; cat prints out file contents to standard output and can work on multiple input files too. Some examples:

Concatenate filename1 and filename2, write output into newFile

cat filename1 filename2 > newFile

There is also a tac command too, it reads files in reverse order. If you cat an object file and your terminal starts showing gibberish; type reset.

5. Man

man gives you access to the reference manuals. Use it to look up the syntax and options of command. It is great for finding out new combinations of tricks to use in the terminal too.

6. Curl

Use curl to download files, execute POST requests, upload files to a server and view HTML pages; in addition it can also do proxying and user authentication. Curl supports a lot of protocols (including but not limited to FTP, SCP and telnet). Curl can be very useful when working with Couchdb which actually listens to HTTP methods.

Better still, curl bindings are available for a lot of programming languages such as PHP, Java, Python etc…

Simple GET request to the localhost (if you have a web server running);

curl localhost

7. Tar

Most Linux downloads come in the form .tar.gz; which are zipped archives. To extract, you have to unzip the file to get the .gz off and then untar the tar archive to get the package contents. Tar does this for you; I normally use this command: tar -xzvf package.tar.gz.

x –  Indicates extraction

v – Verbose mode

z –  Notifies tar that the archive is zipped so it can unzip it

f –  Informs it that the filename follows.

To create a tar archive, use the following command tar -czvf java_archive.tar.gz *.java

c – indicates create an archive.

8. Chmod

Chmod is used to change the read, write and/or execute permissions on files.  Files are associated with their owners (u), other users in the file’s group (g) and other users not in the file group (o); the (a) tag refers to all users.  For example, to make a file executable to all users; I do chmod a+x bash_script.sh

a   indicates all users

x   indicates executable (others include r and w)

+  indicates permissions are to be added (the – sign signifies the opposite)

9. Sudo

Whenever you get the “Permission denied” message, just prefix that command with sudo.

Sudo allows a user to execute a command as root and requires the user to provide a password. On standalone boxes; it will work just fine; however, it will not work on remote hosts if the user is not listed in the sudoers file.

10. History, ! and !!

The history command prints out a list of commands that have been used; this makes it easier to look for some  command that you executed in the past but don’t know how to write anymore. I usually combine it with grep. For example, history | grep ssh.

To repeat one of the old commands, I take the number before that line; lets assume it is 892 and execute !892 and viola! Done!!

!! works in a similar way; it executes the last command. Whenever I get the “Permission denied” message, I usually just sudo !!. To access the latest argument, use !$.

There are more advanced commands like sed and awk for text processing, find and locate for finding files but I don’t use these commands often. I hope you liked the post and found it interesting and useful.

Kindly tell me about your favourite commands.



Leave a Reply

  1. You might want to include chown with chmod. Chown allows you to change the ownership and group of a file or files and directories. Chown [options] owner.group . For example:

    chown -R www-data.www-data /var/www

    The -R switch recursively acts on all the files/directories under /var/www and makes the www-data user the owner and gives the www-data group access to the files. You could have another group, for example a group named graphics-group:

    chown -R www-data.graphics-group /var/www

    The period/dot separates the owner from the group. Of course if all you want to do is give the www-data user ownership you can just:

    chown -R www-data /var/www

    The group for the file(s) stays the same as it was before, only the owner changes.

    I work with disks a lot, so df is also a really helpful command. df reports the disk space.

    df -hH

    results in something like:

    /dev/sda1 489G 6.8G 457G 2% /
    udev 2.2G 4.1k 2.2G 1% /dev
    tmpfs 845M 807k 845M 1% /run
    none 5.3M 0 5.3M 0% /run/lock
    none 2.2G 1.3M 2.2G 1% /run/shm
    /home/charm/.Private 489G 6.8G 457G 2% /home/charm

    The first -h switch makes the bytes display in a human readable format, instead of the following:

    /dev/sda1 476593176 6585308 445798256 2% /

    The second H translates this into the “marketing” (1000 versus 1024) format.


  2. Why would you use “ls -a | grep “username.txt”?

    If you know the name of the file, why do you need to search for it?

    If you want to see whether the file exists, use ls directly:

    ls username.txt

    If you want to find all files whose names contain “username.txt”
    (which is what your command will do) use filename expansion:

    ls *username.txt*

    But for that, you don’t even need an external command (ls):

    printf “%s\n” *username.txt*


  3. When creating an archive, you mention using “tar -cxvf java_archive.tar.gz *.java” . You might want to remove “x” from the options since that means “extract” and it conflicts with the “c” flag.

    Thus, the proper command for creating an archive is “tar -cvzf java_archive.tar.gz *.java”. If you don’t specify the “z” flag, the created archive will not be zipped.


  4. I would also suggest :

    find : to find files based on their name/size/owner anywhere on your disk

    xargs : to process file list (given by find/grep for example)

    find /tmp -name core -type f -print | xargs /bin/rm -f
    (delete core files in the /tmp dir and subdir)

    perl : to replace most of the above with oneliner ;-)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s