10 Terminal Commands That Will Boost Your Productivity
Back in May, Nettuts+ ran a great article entitled ”7 Simple and Useful Command-Line Tips”; this was a great article for getting started with using the command line. But there’s a lot more you can learn about using a shell, and I’ll take you to the next level in this tutorial!
Getting Started
If you’re running Mac OS X, or your favourite flavour Linux, you’re all set. Just fire up the terminal, and keep going. If you’re on Windows, well, the default command set isn’t quite what a bash shell is. If you want some power, check out Microsoft PowerShell; however, the commands below won’t necessarily work there. You can get a bash shell on Windows, though:
- Install Cygwim, a Linux-like environment for Windows.
- Install msysgit; depending on the options you choose when installing, you’ll get a Git Bash that should work will all these commands.
- Try Windows’ subsystem for Unix-based applications. Although I haven’t tried it myself, I understand you can get a Unix shell with it.
All right, let’s hop in!
1. Touch

As a developer, one of your most common tasks is creating files. If you’re working from the command line, most of the time you’ll just pass the name of the file you want to create to your editor:
$ mate index.html $ mvim default.css
However, occasionally you’ll just want to create one or more files, without editing it. In this case, you’ll use the touch command:
$ touch index.html $ touch one.txt two.txt three.txt
It’s that easy. Actually, the touch command is for updating the access / modified date of a file; it’s just a nice side-effect that if the file doesn’t exist, it will create it.
2. Cat and Less

Well, since it’s all about files, there’s a good change you’ll want to see the contents of a file from the terminal sooner or later. There’s a few commands that will do this for you. First is cat; cat is short for “concatenate”, and this command does more than output file contents; however, that’s what we’ll look at here. It’s as simple as passing the command a file:
$ cat shoppingList.txt
However, if the file is large, the contents will all scroll past you and you’ll be left at the bottom. Granted, you can scroll back up, but that’s lame. How about using less?
$ less shoppingList.txt
Less is a much better way to inspect large files on the command line. You’ll get a screen-full of text at a time, but no more. You can move a line up or a line down with the k and j respectively, and move a window up or down with b and f. You can search for a pattern by typing /pattern. When you’re done, hit q to exit the less viewer.
3. Curl

Since you probably work with your fair share of frameworks libraries, you’ll often find yourself downloading these files as you work. Oh, I know: you can just download it from the web, navigate to the folder, uncompress it, and copy the pieces to your project, but doesn’t that sound like so much work? It’s much simpler to use the command line. To download files, you can use curl; proceed as follows:
$ curl -O http://www.domain.com/path/to/download.tar.gz
The -O flag tells curl to write the downloaded content to a file with the same name as the remote file. If you don’t supply this parameter, curl will probably just display the file in the commmand line (assuming it’s text).
Curl is a pretty extensive tool, so check out the man page (see below) if you think you’ll be using it a lot. Here’s a neat tip that uses the shell’s bracket expansion:
$ curl -0 http://www.domain.com/{one,two,three}.txt
Yeah, it’s that easy to download multiple files from one place at once. (Note that this isn’t curl functionality; it’s part of the shell, so you can use this notation in other commands; check this link out for more)
4. Tar and Gzip

So, now you’re rocking command line downloads; however, there’s a really good chance that most of the things you download will be archived and gzipped, having an extension of .tar.gz (or, alternately, .tgz). So, what do you do with that? Let’s take a step back for a second and understand what exactly “archived and gzipped” means. You’re probably familiar with archives. You’ve seen .zip files; they’re one incarnation of archives. Basically, an archive is just a single file that wraps more than one file together. Often archives compress the files, so that the final file is smaller than the original ones together. However, you can still get a bit smaller by compressing the archive … and that’s where gzipping comes in. Gzipping is a form of compression.
So, back to that download. It’s been tarred (archived) and gzipped. You could unzip it and then un-tar it, but we’re all about fewer keystrokes here, right? Here’s what you’d do:
$ tar xvzf download.tar.gz
Wait, what? Here’s the breakdown: tar is the command we’re running; xvzf are the flags we’re using (usually, you’d have a dash in front, but that’s optional here). The flags are as follows:
xlet’starknow we’re extracting, not archiving.vlet’starknow we want it to be verbose (give us some output about the action it’s performing).zlet’starknow that the file we’re working with has been gzipped (so it unzips the file).flet’starknow we’re going to pass it the name of the archive file.
If you want to create one of these gzipped archives, it’s as simple as replacing the x flag with a c (to create an archive). The v and z flags are options: do you want output? how about gzipping? Of course, leave f; you’ll have to give the file name for the new archive (otherwise, it will all be output to the command line). After that, you’ll pass the command all the files you want to put in the archive:
$ tar cvzf archive.tar.gz index.html css js auth.php $ tar cvzf archive.tar.gx *.txt
Just for completeness, I’ll mention that you can gzip archives (or other files) individually; when you do so, gzip replaces the original file with the gzipped version. To un-gzip, add the -d flag (think decompress.
$ gzip something.txt $ gzip -d something.txt.gz
5. Chmod

Another thing you’ll do often as a web developer is change file permissions. There are three permissions you can set, and there are three classes that can receive those permissions. The permissions are read, write, and execute; the classes are user, group, and others. The user usually the owner of the file, the user that created the file. It’s possible to have groups of users, and the group class determines the permissions for the users in the group that can access the file. Predictably, the others class includes everyone else. Only the user (owner of the file) and the super user can change file permissions. Oh, and everything you’ve just read goes for directories as well.
So, how can we set these permissions? The command here chmod (change mode). There are two ways to do it. First, you can do it with octal notation; this is a bit cryptic, but once you figure it out, it’s faster. Basically, execute gets 1 ‘point’, write gets 2, and read gets 4. You can add these up to give multiple permissions: read+write = 6, read+write+execute = 7, etc. So for each class, you’ll get this number, and line them up to get a three digit number for User, Group, and Others. For example, 764 will give user all permissions, give group read and write ability, and give others permission to read. For a better explanation, check out the Wikipedia article.
If you have a hard time remembering the octal notation, you might find symbolic notation easier (although it takes a few more keystrokes). In this case, you’ll use the initial ‘u’, ‘g’, and ‘o’ for user, group, and others respectively (and ‘a’ for all classes). Then, you’ll use ‘r’, ‘w’, and ‘x’ for read, write, and execute. Finally, you’ll use the operators ’+’, ‘-‘, and ’=’ to add, subtract, and absolutely set permissions. Here’s how you’ll use these symbols: class, operator, permissions. For example, u+rwx adds all permissions to the user class; go-x removes executable permission from group and others; a=rw sets all classes to read and write only.
To use all this theory on the command line, you’ll start with the command (chmod), followed by the permissions, followed by the files or directories:
$ chmod 760 someScript.sh $ chmod u=rwx g+r o-x dataFolder
6. Diff and Patch

If you’ve used version control like Git or Subversion, you know how helpful such a system is when you want to share a project with other developers, or just keep track of versions. But what if you want to send a friend some updates to a single file? Or what if another developer has emailed you the new version of a file that you’ve edited since you received the last copy? Sometimes, full-blown version control is too much, but you still need something small. Well, the command line has you covered. You’ll want to use the diff command. Before you make changes to a file, copy the file so you have the original. After you update, run diff; if you don’t send the output to a file, it will just be output to the command line, so include a > with the name for your patch file:
$ cp originalFile newFile $ vim newFile #edit newFile $ diff originalFile newFile 1c1 < This is a sentence. --- > This is a short sentence. $ diff originalFile newFile > changes.patch
As you can see, the diff is just a simple text file that uses a syntax the diff and patch command will understand. Patch? Well, that’s the command that goes hand in hand with diff. If you’ve received a patch file, you’ll update the original as follows:
patch originalFile2 changes.patch
And now you’re all updated.
7. Sudo

Sudo isn’t really a command like the others, but it’s one you’ll find a need for as you venture deeper into the command line world. Here’s the scenario: there are some things that regular users just shouldn’t be able to do on the command line; it’s not hard to do irrevocable damage. The only user who has the right to do anything he or she wants is the super user, or root user. However, it’s not really safe to be logged in as the super user, because of all that power. Instead, you can use the sudo (super user do) command to give you root permissions for a single command. You’ll be asked for you user account password, and when you’re provided that, the system will execute the command.
For example, installing a ruby gem requires super user permissions:
$ gem install heroku
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /Users/andrew/.gem/ruby/1.9.1/cache/heroku-1.9.13.gem
$ sudo gem install heroku
Password:
Successfully installed heroku-1.9.13
8. Man

Most of the commands you’ll use in a bash shell are pretty flexible, and have a lot of hidden talents. If you suspect a command might do what you want, or you just want to see some general instruction on using a command, it’s time to hit the manuals, or man pages, as they’re called. Just type man followed by the command you’re curious about.
$ man ln
You’ll notice that the man pages are opened in less.
9. Shutdown

When you’re done for the day, you can even turn your computer off from the command line. The command in the spotlight is shutdown, and you’ll need to use sudo to execute it. You’ll have to give the command a flag or two; the most common ones are -h to halt the system (shut it down), -r to reboot, and -s to put it to sleep. Next, you’ll pass the time it should happen, either as now, +numberOfminutes, or yymmddhhmm. Finally, you can pass a message to be shown to users when the deed is about to be done. If I wanted to put my computer to sleep in half-an-hour, I’d run this:
$ sudo shutdown -s +30
10. History, !!, and !$

Since the command line is all about efficiency, it’s supposed to be easy to repeat commands. There are a few ways to do this. First, you can use the history command to get a numbered list of many of your recent commands. Then, to execute one of them, just type an exclamation mark and the history number.
$ history ... 563 chmod 777 one.txt 564 ls -l 565 ls 566 cat one.txt ... $ !565
Granted, this is a terrible example, because I’m typing more characters to use the history than it would take to re-type the command. But once you’re combining commands to create long strings, this will be faster.
It’s even quicker to access the last command and last argument you used. For the latest command, use !!; the usual use case given for this is adding sudo to the front of a command. For the latest argument, use !$; with this, moving into a new folder is probably the common example. In both these cases, the shell will print out the full command so you can see what you’re really executing.
$ gem install datamapper
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /Users/andrew/.gem/ruby/1.9.1/cache/datamapper-1.0.0.gem
$ sudo !!
sudo gem install datamapper
Password:
Successfully installed datamapper-1.0.0
$ mkdir lib
$ cd !$
cd lib
Conclusion
If you’re as passionate about productivity as I am, the idea of using the command line as much as possible should resonate with you. What I’ve shown you here is just a sampling of the built in commands … then, there are many more than you can install yourself (look at something like the homebrew package manager, for example). But maybe you’re already proficient on the command line; if so, can you share another great command with the rest of us? Hit the comments!


RoyalSlider – Touch-Enable ... only $12.00 
This is basic bash skills, dude. The things that ‘ve work all day long hehe
Readers should read more about bash, hope they not hesitate.
Cheers
Something is seriously wrong with all of the images here on nettuts. Most of them are not being displayed or do not exist (red cross & alt atribute).
Yes. It the same for me and it happens for the whole Tuts network (phototus, psdtuts, etc. )
my desktop browser renders these images fine(ff, chrome) – but my mobile safari(ipad, iphone) never ever render nettuts.com images…
This goes the same with all my rss “apps” – nettuts images will not show.
Hi guys,
I’d like to try and help troubleshoot your image problems. Would you send me an email at ryan@envato.com so I can help out? Cheers.
If you have cygwin (not cygwim) installed, you don’t really need msysgit – install the unix git in cygwin (There is a nice graphical installer included), so you dont need to run two separate consoles :)
adding on the history item. add “set -o vi” to your .profile and then at any time you can hit esc then the letter k to cycle through your commands. also hit esc then k then /{query string} then enter to search for a previous command.
eric :/ls
eric :ls -lart
perfect woork thanks a lot
Perfect, my skills are not so good with terminal but this is a good start!
I think I have to start from first. I am not so skilled to use these command line like you..
It’s the very thing i want to catch. Thanks!
Thanks for the tutorial, Andrew! Great extension of my article from May that you mention in the intro.
Very useful :)
Also, for those having issues with images on the TutsPlus sites, try clearing your browser cache, and your DNS cache (or switching to a DNS service like Google’s or OpenDNS). The problem seems to be from the image URLs changing.
Hope that helps ;)
This was ridiculously useful. Thanks.
I really love the idea but it seems kind of devious ( is that the right word? ), maybe it’s just a matter of practice but for me it’s not profitable at all. Do many of you use the terminal on a daily basis? Does it really work faster once you get the hang of it? Is it worth the investment of learning?
Yep. The terminal is really worth learning. I work on a Linux system a lot and using the terminal is a lot faster for working. Its not really that hard. You don’t need to know some of the commands as some are not very useful or needed. It is worth the investment of learning. Anyways, using git or svn or mercurial should be done on the terminal.
Yes and no. I agree that command line work isn’t optimal in terms of usability, but to a lot of us who administer Linux servers/VPSes it’s just a normal thing.
When you customize the command line to really suit your needs, it’s the best thing in the world and I could not live without it for one simple reason – it’s usually 10x faster to do things using the command line than using some graphical interfaces, FTPing to servers, etc.
Instead of using curl to download files, I recommend wget (which is included in every Linux distribution or can be installed easily).
Nice tutorial for beginners.
Thanks for sharing.
Thanks for great tips. Two other commands that I really find helpful are wget (for downloading files) and scp (for copying files from server to server).
Goooood : )
Something is really wrong with your network. I can’t see any of the images inside the articles, neither in a browser, nor in the RSS client.
I push the up key for history, its really fast and is something I would recommend over your method in this post.
The other thing Im surprise you didnt cover is login to a site using ssh and the basic commands for using this interface as I think this is what most web devs get stuck with when starting in the console.
Good post though, its always helpful to see what other people do on the command line.
This is definitely more for developers than designers, even those designers with good front end dev skills will struggle to find this an attractive alternative to more long winded routes. Even though command line is the best way there’s a good reason why GUI’s have been developed!
Saying that I used Putty for a while and it was great once I got the most used commands to stick in my brain.
For users who can get their heads around it this could be a real eye opener.
A real eye-opener comes when you discover the magic of some of the command-line tools. One example from my practice – I manage quite a few Drupal-based sites. When installing new modules, you have two options:
a) Navigate to Drupal.org, find the module, download it to your computer, unzip, send it via ftp to you server (takes a long time in case of larger modules with hundreds of files), navigate the rather cumbersome Drupal’s module interface and activate. Time – at least several minutes.
OR
b) Install drush comman line tool. Then you just do the following:
drush dl module_name – downloads the module, automatically to the proper folder
drush en module_name – enables the module
Time – about 10 seconds.
And now, take into account that you usually use at least 10 different modules for more complex Drupal sites. Command line easily means time savings measured in hours.
Thanks for an extremely useful tutorial!
I always avoid the command line through fear that I’ll hit ENTER and my computer will burst into flames, but it is something I’d love to be comfortable with one day and this tutorial is a great start.
Quite often, command-line tutorials tell you what to put where, but for someone like me who is a complete noob, it’s great to have it broken down into it’s most basic parts as you’ve done here.
Thanks again!
This isn’t working ???
on the “shudown” thing, I would never suggest anyone ever do that from the command line.
When in console mode, it is too easy to forget what machine you are currently “ssh”ed into. You could very easily shut down a remote server completely by accident (I have done this more than once, and each time, it was a hair-raising experience).
If it’s speed you want, then in Linux, hit Alt+F2 to pop up a command dialog, then type “poweroff” in the dialog.
you should have a look at “mollyguard” ;) If you do a shutdown in an ssh session, it will prompt you for the hostname and only shut down if it matches the hostname of the machine you’re sshed into
If you don’t fear the CLI, you should also know: grep, find, awk, sed.
Combined with IO redirections, shell control structures and substitutions, their use really really boost your productivity.
Chech out these slides for a short view http://www.n0on3.net/bash-shell-scripting/
display current error:
tail -f error_log
Really useful and well explained, especially the chmod…never realized that it can be verbose as well.
In my experience it will definitely output the contents of the file, no matter what sort of file it is. This can be a little startling (or worse) if you do it with a non-text file.
Thanks for the comments, everyone!
I’ll add another here in the comments that I recently discovered:
pushdandpopd. They work just like push and pop for arrays, and they’re for moving back and forth between directories easily. If you’re in Dir1, use pushd and pass it the path to Dir2 to change to that directory. When you want to go back to Dir1, just use popd, parameterless. You can do this for multiple folders, and it just push them on the end of an “array” of directories and pops them off as you need them, first in, first out:As you can see, both commands show you the current state of the “array.”
Thank you for the article. The part on patch and diff might be useful for me. I also frequently use control-p and control-n to move back and forth between commands without leaving the home keys. Also control-r to search for recently used commands is probably the most useful command I use from the terminal. I find myself also using control-L to clear the terminal window.
You can dowload files with wget commant too:
wget http://www.domain.com/path/to/download.tar.gz
Thank you for the article. Really useful and well explained!
Its really fast and is something I would recommend over your method in this post.
Thanks for the titorial.
These are really useful commands to know. I’m working more and more from the command line since I started using a mac, so this is an awesome resource for me
mary
How did you configure your prompt like that
Good point! It looks a lot cleaner than the standard you get with OSX. Any chance you can show us your $PS1?
Thanks Andrew for sharing these useful commands. I found this sudo command to be very useful for me.
And one command i use more often is the lpr command which is used to print files. You can specify the printer name by using -p options if the printer you want to use is different than the default one.
lol very basic but very useful commands :)
One thing worth mentioning is to use the -p flag when using tar if you need to preserve permissions.
From Article:
“It’s that easy. Actually, the touch command is for updating the access / modified date of a file; it’s just a nice side-effect that if the file doesn’t exist, it will create it.”
Me: That’s a really nice side-effect. :-D
Learning to use the command line better, it definitely resonates with me. Enjoyable read. Thanks!
Why don’t you guys do something like a code tuts+, a programing tuts site, where there will be tutorials like
C++, Java, Python, etc…
I know its to much to ask, but it would be a great addition to the Tuts Plus education network.
If there’s already something like this in you network please ignore me.
I think the title of this article is a bit misleading – a more apt title would be, “10 terminal commands you should know” or “10 terminal commands to get you started”. For example, sudo doesn’t increase productivity, except insofar as if you try to do something like ‘vim /etc/hosts’ without sudo you’ll be there all day trying to edit the file; either you need it or you don’t.
One example of something that increases productivity is:
mkdir -p /path/to/directory/you/want/to/create
where the -p argument allows you to create all directories up to and including the last one… so that you don’t have to make one directory at a time.
Another excellent productivity booster is the find command. Let’s say I have a directory containing ‘test1.php’, ‘green_test.php’ and ‘another_file.php’ and I want to chmod just those files containing the word ‘test’. Well then I can do something like this:
find . -name “*test*” -print | xargs chmod 0777
which will print out the path of each matched file and execute a chmod on each file that was found.
Another useful command is sed, which I will use in conjunction with find to do a recursive find and replace… beats starting up Dreamweaver to do a find and replace on all files in a folder, eh?
find . -type f | xargs sed “s/needle/replacement/g”
where the g means we do a global replace, as opposed to just replacing the first match of needle with replacement.
A big time saver, which seems to be overlooked is word movement in terminal. I use the following:
Esc-F – go forward to end of current word
Esc-B – go backwards to beginning of current word
Ctrl-W – delete full word backwards
Ctrl-A – go back to beginning of line
Ctrl-E – go forward to end of line
These aren’t strictly commands but it’s painful hitting the left arrow key all the way back to the beginning of a command to fix a typo.
This article would have been a lot better if it was ‘an introduction to Mac OSX terminal’, with a more structured approach to teaching (from A to B to C….) as there isn’t anything in it you can’t salvage from a quick search of ‘download files from command line’ (wget, curl) or ‘change file permissions in terminal’ (chmod).
Another great time saver when working with your history is Control+R. This lets you start typing and will search your command history for any commands that have the string you typed. This can save tons of time when you need to run the same long command repeatedly. For example, if you commonly view the contents of a file which is many directories deep, say
“less /some/really/confusing/path/you/never/remember/myFavoriteLog.txt”
You can just press Ctrl+r and the type “myFav”
And the full command will be auto-completed for you!
Knew some of these, but others were new to me.
Great list!
Nice Tutorial… you would be dead if your on a linux or mac and dunno any of these.. :)
Knew some of these
very nice topics
It is a matter of joy that this site topics,tips really acceptable