Shell Commands Every Developer Must Know

Improve your productivity with these basic terminal commands

Erik van Baaren
May 27 · 5 min read
Image ©2020 by author

Are you still dependent on GUIs? You can significantly increase your productivity by learning a few basic shell commands. After learning these, learning more commands will come naturally — promised!

If these shell basics are well known to you, please skip this article and head to my follow-up article to become a command-line ninja!


pwd — return working directory name

Let’s start by knowing where you are once you opened your terminal:

$ pwd
/Users/erik

I’m in my home directory, where are you?


ls — List directory contents

Now let’s see what is in the current directory:

$ ls
AndroidStudioProjects VirtualBox VMs
Applications Desktop
Documents flutter
Downloads...

There’s a lot here because I’m a messy guy, so I truncated it a little.

The ls command has many convenient options; these three I use all the time:

  • -l: (The lowercase letter “ell’’.) List in long format. (See below.) A total sum for all the file sizes is output on a line before the long listing
  • -h: used with the -l option, this changes the file sizes into something more readable, like 10G, 42M. In my head, I translate this to ‘human-readable’
  • -a: Short for ‘all’; include ‘hidden’ directory entries whose names begin with a dot (.)

You can combine these any way you need, like this:

$ ls -lhatotal: 32drwxr-xr-x+ 85 erik staff 2.7K May 27 11:11 .
drwxr-xr-x 6 root admin 193B Sep 29 2019 ..
-rw-r--r-- 1 erik staff 2.1K Apr 15 11:06 .gitignore
-rw-r--r-- 1 erik staff 25M May 26 15:44 image.zip
...

What you see here are the access rights, the owner and group, the file size, the files last modified data, and the file name itself.

On the first two rows, you might notice something odd: two files named . and .. :

  • . is the current directory. It even has a size, which is dependant on the number of files in the directory
  • .. is the underlying directory, which the current directory is part of

These handy shortcuts can be used in every command. If you want to view the contents of the underlying directory, use ls ../. You can also repeat this, so if you want to go down two directories, you can use ls ../../.


cd — Change directory

Now that you know where you are and what directories there are, you want to navigate around. Let’s say your projects are stored in the Projects folder. You can go there by typing:

$ cd Projects

Most shells support auto-completion. Just try it by typing:

$ cd Pro<TAB key>

Your shell will auto-complete the word or, if multiple directories match what you typed, it will usually show you all the options.


cp — Copy

You may want to copy or duplicate some files. The cp command does just that: it copies a file. It does so by creating a new file and copying all the contents to that file. To copy file1.txt to a second file, e.g., to back it up, use:

$ cp file1.txt file1.txt.bak

To copy a file to another directory, you can use:

$ cp file1.txt ./backups/

The ./ means ‘the current directory,’ so ‘backups’ is located in the current working directory.


mv — Move

If you need to move a file, use the mv command. Note that moving a file is the same as renaming a file. In fact, there is no rename command. Using mv is not much different from cp. To rename/move a file:

$ mv file1.txt file1.txt.bak

To move a file to another directory:

$ mv file1.txt ./backups/


mkdir — Make directories

This command will simply create a directory. To create a directory to store your projects in, use:

$ mkdir Projects

There’s one convenient argument to this command, -p. When you use it, any intermediate directories that don’t exist yet will also be created. If you want to create your first project inside of “Projects” directly, you can do so with:

$ mkdir -p Projects/my_first_project


rmdir — Remove directories

The opposite of mkdir is rmdir, it removes a directory. It even allows the same -p argument.

$ rm Projects

To remove the Projects directory and the first project we just created:

$ rmdir -p Projects/my_first_project

Removing a directory will only work when that directory is empty. If you want to remove a directory, including its contents, see the rm command which is up next!


rm

Now we’re getting into dangerous territory. This command is very powerful and has the potential to destroy your file system. Use it with care.

Let’s start by destroying a file:

$ rm file1.txt

Poof! It’s gone. There’s no ‘Trash Can.’

Now let’s remove all files in our projects directory that end with .txt. You need a so-called glob pattern that matches multiple files:

$ rm Projects/*.txt

If you want to delete all files, use:

$ rm Projects/*

The glob pattern * matches everything.

If you want to err on the side of caution, use the -i option, which forces you to confirm the delete operation.

Now, for the grand finale, let’s delete an entire tree of directories and files. For this, you need the argument -r, short for recursive. rm -r will sometimes refuse to remove special files, or constantly ask for confirmation. You can use the force option -f to remove everything it can; no questions asked. Let’s remove all our projects:

$ rm -rf Projects

Poof! All your work — gone forever. Like I said, handle with care!


cat, less, tail, head — View the contents

You often want to quickly inspect the contents of a file. There are several ways to do this. Pick one that matches your use case:

  • cat — print everything on your screen
  • less — allows you to scroll through the file and even search inside it
  • tail — like cat, but only print the last 10 lines (by default)
  • head — like cat, but only shows the first 10 lines (by default)

Both tail and head take the -n <num>option, to change the number of lines it shows.


control+r — Find your previous command

It’s easy to forget, so wouldn’t it be nice to find that command you used yesterday or last week quickly? Well, you can! Just hit control+r on your keyboard and start typing the part of the command you do remember — this can be any part!

If what you found is not entirely what you were looking for, keep searching by repeatedly hitting control+r. It will keep giving you matches for your command history.


Further reading

You just learned the basic commands that help you navigate the shell. If this introduction whetted your appetite, I encourage you to keep learning and read my follow-up article called “This Will Make You a Command-Line Ninja.”


Thank you for reading. If you believe I missed something, please leave a comment!

Tech Explained

Understandable, practical and useful explanations of…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store