7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
Advertisement
  1. Code
  2. Roundups

16 Terminal Commands That Will Boost Your Productivity

Read Time: 15 mins

One of the most useful tools a web developer has is the command line. If you're reading this post, you're probably no stranger to the terminal. But you may not know all the special commands and utilities you can use to boost your productivity.

Getting Started

If you’re running macOS or your favourite flavour of 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:

  • Try Windows Subsystem for Linux—it installs a native Linux environment in Windows that you can access through a terminal window.
  • Or install Cygwin, a Linux-like environment for Windows.

All right, let’s hop in!

1. Create Files With touch

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:

However, occasionally you’ll just want to create one or more files, without editing them. In this case, you’ll use the touch command:

It’s that easy. Normally, the touch command is for updating the modified date of a file; it's just a nice side-effect that if the file doesn't exist, it will create it.

2. View Text Files With cat and less

cat and less

Well, since it’s all about files, there’s a good chance you’ll want to see the contents of a file from the terminal sooner or later. There are a few commands that will do this for you. The first is cat, which is short for “concatenate”. 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:

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 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 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. Download Files With curl

curl

Since you probably work with your fair share of frameworks and 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:

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 command 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:

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 out this link to learn more.

4. Expand Files With tar and gzip

tar and gzip

So, now you’re rocking command-line downloads; however, there’s a really good chance that many of the things you download will be archived and gzipped, having an extension of .tar.gz (or, alternately, .tgz). 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. Archives often 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:

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:

  • x: extracting, not archiving.
  • v: be verbose (give us some output about the action it’s performing)
  • z: unzips the file
  • f: expect an archive filename in this command

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 similar: do you want verbose output? How about gzipping? Of course, use 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:

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).

5. Change Permissions With chmod

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 is 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 is 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.

Here are some of the most commonly used permission values:

Value Meaning
0 no permissions
4 read only
5 read and execute
6 read and write
7 all permissions

And here are some common permissions for chmod that you will use again and again:

Command User Group Others When to Use
644 read and write read only read only a file that can be edited by its owner but read anywhere —for example a web page
755 read, write, and execute read and execute read and execute a script that can be edited by its owner but run but anyone—for example a PHP script
660 read and write read and write no permissions a data file that can be written to by specific programs—e.g. a cache or log file

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:

6. Compare Differences With diff and patch

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:

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:

And now you’re all updated.

7. Run as an Administrator With sudo

sudo

sudo isn’t really a command like the others, but it’s one you’ll need 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 they want 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 your user account password, and when you’ve provided that, the system will execute the command.

For example, installing a ruby gem requires super user permissions:

8. Get Help With man

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.

You’ll notice that the man pages are opened in less.

9. Power Down With shutdown and reboot

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:

10. Get Your Command-Line History With history, !!, and !$

history

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.

Granted, this is a terrible example, because I’m typing more characters to use the history than it would take to retype 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.

11. Preview Images With imgcat

Many times we save two images with the same name, but we can't identify which one is the right image. imgcat is the best solution to help you find the right image via the terminal. It renders the image directly in the terminal without needing a window manager or GUI!

imgcat screenshotimgcat screenshotimgcat screenshot

At the time of writing, there are a few clones of the imgcat tool available. One of the easiest to install is Daniel Gatis's version. On Ubuntu:

On Mac, you can use:

12. Chaining Multiple Commands With &&

You can run multiple commands on the terminal one after the other using the && command.

Here's an example of chaining multiple commands:

In this case, the Tailwind CSS package will be installed after Vue.js, and only if the first package installs correctly.

13. Open a File on Your Desktop With open

The open command can help you open a file in your directory via the terminal. It will open the file or folder in the appropriate system viewer, e.g. a text editor, image viewer, web browser, or file explorer.

You can also use open on a folder:

This will open the current folder in the system file explorer.

14. Find Files Fast With fzf: Fuzzy Text Finder

fzf is a fuzzy text finder. It's a command-line interactive Unix filter for finding any files, including command history, files, directories, bookmarks, git commits, and so on. The program is compatible with Linux, macOS, and Windows. It can also be integrated manually or in scripts by processing the command output using shell extensions. You can learn more about how to set up the fzf command in the online documentation.

Use the following command to open a fuzzy finder for the files contained in the current folder and all its subfolders.

15. Find Files With find

This command is used to locate files or directories, as the name suggests. Imagine you need to find a single file after logging into a server or opening a large folder with too many other files. The best answer for this problem is to use the find command. Or maybe you want to find all images, or all files modified after a certain date or above a certain file size. find can do all of this!

Below is the most basic example of using find:

It will list all the files and folders contained in this folder and in any subfolder.

16. Jump Between Folders With z

z is another handy command-line tool for boosting productivity. It helps you to easily navigate to directories that you've recently visited or that you've visited frequently. A combination of the two is used, with a concept known as "frecency". z keeps track of how often you visit directories and how long you spend there. It will then be able to estimate where you want to go when you write a short string, such as z src, which might take you to the src folder of a source project you've been working on.

If you are using the zsh shell, use the Zsh-z flavour, which gives you z-powered autocompletions for all shell commands. For example, if you install Zsh-z, type the following and press Tab twice:

Zsh-z will use its frecency logic to figure out what src folder you are referring to and will use that in the command.

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. There are many more commands that you can discover for yourself, so go explore!

This post has been updated with contributions from Ezekiel Lawson. Ezekiel is a front-end developer who focuses on writing clean, maintainable code with web technologies like JavaScript, Vue.js, HTML, and CSS.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Scroll to top
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.