Learn How to Use the Command Line: Command Line Cheat Sheet

Opening file managers. Dragging and dropping files. Right-clicking. These activities are quite annoying. They’re even completely unnecessary. You can control your entire computer without a single mouse click, if you so desire. Read on to find out how.

In this article, we’ll learn how to speed up our development life by making use of some common commands.

Topics Covered
  • vim
  • ls
  • cd
  • mkdir
  • touch
  • rm
  • rmdir
  • man
  • pwd
  • exit
  • echo
  • cp
  • mv
  • cat
  • find
  • sudo

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient.

The first item on our list is a text editor we believe every developer should be familiar with: Vim. It’s not directly related to learning the command line, but being familiar with Vim will make the process so much easier for you.

Consider needing to edit a file, index.js. How might we do this without Vim? If you’re completely unfamiliar, you might open your file manager, right-click, and open it with your text editor. There’s nothing wrong with this.

Although it can be accomplished so much faster with Vim:

vim index.js

Calling vim index.js will open up the file directly in your terminal, allowing you to make changes to the file and save them pretty quickly. As with all great things, there is a learning curve to learning this text editor, though. Take a look at this meme for an example:

exiting vim meme

It’s a bit of an exaggeration. It’s really not that difficult to exit the editor, so long as you’re aware of how to. The meme is funny, nonetheless.

Take a look at this talk about Vim.

Note: Learning Vim is optional. However, we promise you wont regret it.

We leave you with this quote:

“Patience and perseverance have a magical effect before which difficulties disappear and obstacles vanish” – John Quincy Adams

ls - list directory contents

The ls command lists the current directories contents, if no flags are specified. List information about the FILEs (the current directory by default).

Let’s try it out. Open a terminal and type ls.

user@user:~$ ls

If you’re in a directory that has content (i.e. folders and files), you should see them displayed in your terminal.

There are a few flags you may use along with ls:

ls [flags]

  • ls -a - list all contents in the directory, including those that begin with ., which would be considered a hidden file.
  • ls -l - list the contents of a directory in a table format, displaying extra information, such as: content permissions, owner of content, size of content, and more.
  • ls -s - list content with the size displayed.
  • ls -R - list files recursively. Lists all files, folders, and subdirectories.
cd - change directory

The cd command allows us to change directories. For example, if we’re in a directory called code, and we have a project, weather-app, we can get to the project directory by typing:

user@user:~$ cd weather-app

If you need to go back to the previous directory, you can type cd .... Lets say we’re in the weather-app directory, which is in the code directory. To travel back to the code directory, we’d type:

user@user:~$ cd ..

We can go further back, if we’d like.

user@user:~$ cd code/weather-app/controllers
user@user:~/code/weather-app/controllers$ cd ../../..

So, we were in the code/weather-app/controllers folder. After running the command, cd ../../.., we end up back to where we started.

mkdir - make a directory

The mkdir command makes a new directory.

user@user:~$ mkdir <folder_name>

The command above will a new directory, with whatever name you’d like to give it.

touch - make a file

The touch command creates a new file. If we cd into the newly created folder above, we can add a file to it.

user@user:~$ cd folder
user@user:~/folder$  touch file.txt
rm - delete a file

To delete a file, simply run rm:

user@user:~$ rm file.txt
rmdir - delete a directory

To remove a directory, type rmdir:

user@user:~$ rmdir folder

Note: This only works on empty directories.

man - display the manual page of a command

The best place to find information about any of these commands is the man pages, or the manual pages.

user@user:~$ man rmdir
pwd - print working directory

pwd displays the current working directory.

user@user:~$ cd code/weather-app
user@user:~/code/weather-app$ pwd
exit - exit the terminal

To exit the terminal, simply type exit. There’s also a keyboard shortcut that you can check out below.

cat - concatenate files and print the standard output

cat displays the contents of file or folder.

Below, we’ll look into file.txt, assuming it has the text hello stored into it.

user@user:~$ cat file.txt
echo - print a message

To print a message to the console, you can run the following:

user@user:~$ echo hello

We can also add text to files from the command line. If we have an empty file, hello.txt, we can write to the document by typing:

user@user:~$ echo hello > hello.txt
user@user:~$ cat hello.txt

The above will overwrite the existing text, so keep this in mind. If we want to append text to the document, we can instead type:

user@user:~$ echo howdy >> hello.txt
user@user:~$ cat hello.txt
cp - copy content(s)

cp allows you to copy the contents of a file, or an entire directory.

Let’s add the text from the hello.txt file to a file called copy.txt:

user@user:~$ cp hello.txt copy.txt
user@user:~$ cat copy.txt
mv - move (rename) files

mv can be used to change the name of a file or foler, moving the contents into the newly created file or folder.

Let’s say we want to change the directory weather-app to weather. Rather than going into a file explorer, right-clicking, and hitting the rename button, we can accomplish the same result by typing

user@user:~/code$ mv weather-app weather

The weather-app directory is now called weather.

find - locate a file or folder, or a specific string

Looking for a particular file, folder, or a piece of text? find is the command you’re going to want to use.

lets use find to search in a directory for a specific file:

user@user:~/code$ find weather/ -name index.js
sudo - execute a command as another user

If you’ve used a unix-like operating system, you’ve probably been prompted to log in as a root user to install software. That’s where sudo comes in. It allows you to run commands as a super user.

We might want to update our system. In order to do so, we’d need to run the following command:

user@user:~$ sudo apt-get update
[sudo] password for user:

Keyboard shortcuts

  • ctrl+alt t - Open a new terminal
  • ctrl l - clear the console
  • ctrl d - exit the terminal
  • ctrl p - display the last entered command

While we gave you some guidance on how to use these commands, they’re far more powerful than what this article covers. Don’t forget to check out the man pages for each of these commands. There are also lots of other commands for you to explore as well, you’ll find them in the books listed below.

If you want to become a command-line super user, check out the books below.

The Linux Command Line, 2nd Edition

Bash Pocket Reference

comments powered by Disqus

Related Posts

Solving a Common Interview Question: the Two Sum Algorithm in JavaScript

Imagine you’re at a lively party, and everyone is carrying a specific number on their back. The host announces a game – find two people whose numbers add up to the magic number, and you win a prize!

Read more

Remove Duplicates from Arrays and Strings in JavaScript

Removing duplicates is a common problem in programming that can arise in various contexts, such as cleaning up data or ensuring unique entries.

Read more

Remote First: 5 Websites for Remote Job Opportunities

Would you prefer to work in an office, or while sitting at a beach somewhere in Thailand (i.e. remotely)? Okay, maybe there’s no beach in this scenario, but there’s definitely silence, and maybe your cat.

Read more