If you aspire to be a developer, sys-admin, or some other profession that relies on computers outside of word documents and excel spreadsheets then you ought to be comfortable with a command line.

Software and IT professionals love their command-lines as it is much more powerful than any GUI could ever be. This article aims to get you started from no prior knowledge.

If this is completely new material for you, I recommend that you keep a Finder/Windows Explorer/etc. window open to understand what’s going on in familiar environments.

The difference between shells and terminals

A terminal is a program with a menu bar and icon in your application bar etc. that provides a window into a shell. For the purpose of this article, a ‘shell’ is a low-level program that executes commands. The shell we’ll be discussing in this article is the de facto industry standard; ‘Bash’ (Bourne-Again SHell), although shells like Zsh (my personal favourite) and Fish are often used among professionals.

Examples of Terminals to run Bash in:

  • Windows: All Windows computers ship with a proprietary terminal called ‘cmd’, and recent releases of Windows also ship with the ‘Windows Powershell’. Neither of these can run Bash easily, and the content of this article does not apply to these. Author’s opinion: Both of these terminals/shells are crude and immature, although cmd much more so than Powershell. You can install other terminals on Windows as well, with the most popular one being Cygwin. To get Cygwin up and running quickly (its own installer is definitely non-trivial for beginners) I suggest you install Git and thereby also Git-Bash.

  • Mac: All Macs ship with a pretty good terminal application called ‘Terminal’. There is also a free and more powerful terminal called ‘iTerm2’. Using just Terminal is fine for the purposes of this article.

  • Linux: ‘gnome-terminal’, ‘guake’, ‘xterm’, ‘konsole’, ‘terminator’, ‘termite’… If you’re running Linux you probably have a favourite already. Basically any Linux terminal will work for the purposes of this article.

cd: Navigate around in the file system

To navigate between directories you use cd <path to directory> (“Change directory”)

cd Documents

will take you to the Documents directory inside your current directory. To navigate to nested directories in a single command separate the directory names with a forward-slash.

cd Documents/Important/Invoices

Special paths:

  • / (forward-slash): On Macs and Linux computers (Unix-like operating systems) the / path is called ‘root’, and is the highest directory you can go to. All directories and files exist somewhere below /. Windows is a bit different because it exposes disk drives as separate entities, and therefore / may not make much intuitive sense.

  • ~ (tilde): The Home directory of the current user. Most of the files and directories you’re familiar will exist here. E.g. Music, Documents, Downloads etc.

  • . (single full stop): The current directory. It may not be obvious just yet why this is useful, but it will with time (you are encouraged to look it up if you’re curious). For now just remember the difference between . and the .. special path.

  • .. (two full stops): The directory above the current one. If you keep running cd .. you end up at /.

  • - (dash): The previous directory you cd-ed into.

# Navigate to the home directory of the current user
cd ~
# An even easier shortcut for 'cd ~'
cd

# Navigate to the highest level directory
cd /

# Navigate to Documents within your home directory
cd ~/Documents

# Navigate to current directory (~/Documents - does nothing)
cd .

# Navigate up one from Documents, i.e. your home directory
cd ..

# Navigates back to ~/Documents
cd -

ls: See the contents of the current directory

The ls (“List”) command is useful for seeing what’s in the current directory.

ls
README.md   package.json    package-lock.json   public/   static/

Depending on your configuration files and directories may be coloured differently.

echo: Print to screen

You can use the echo command to print to the terminal. On its own this isn’t very interesting, but it is very powerful when combined with other commands and variables.

# Read the USER environment variable
echo "My name is $USER"
My name is kinbiko

> and »: Create file with command output

We can take the above command, and redirect the output to a file using >.

echo "My name is $USER" > echo-example.txt

This won’t print the output anymore as we’ve told Bash to redirect output to the file echo-example.txt. Open the file in a text editor to verify that this is indeed the case.

On the other hand, the >> command will append to the file you specify, whereas > will overwrite it entirely.

echo "My name is $USER" >> echo-example.txt

Now, the echo-example.txt file will contain the same line twice.

Try the original command again, and verify that the file only has one line.

echo "My name is $USER" > echo-example.txt

rm: Remove file

Let’s remove the file we just created in the above example.

ls # Verify that you see the file
rm echo-example.txt
ls # Verify that the file is gone

There, gone. This is a very powerful command, and won’t simply move things to your Rubbish Bin or similar if your operating system supports this feature. The file is gone forever. Make sure you only run rm on files you are absolutely sure you want to get rid off.

mkdir: Create a directory

To create a directory use the mkdir (“make directory”) command.

mkdir repos
ls # Verify that a directory called 'repos' has been created

rmdir: Remove directory

To remove a directory we use rmdir (“Remove directory”). Let’s remove the directory we created above.

ls # Verify that you see a directory called 'repos'
rmdir repos
ls # Verify that this directory is no longer there

Note, this command does not work if the directory is not empty. This is a measure to prevent you from shooting yourself in the foot. rmdir, like rm are powerful commands. Once deleted, you won’t get it back.

There is of course a way of deleting a directory along with all its contents, but this is a ridiculously powerful command (to the extent that it’s the centre of many geeky jokes and memes) and therefore will not be covered in this beginner’s article.

cp: Copy file

To copy a file in Bash we use the cp command with two arguments: The first is the path to the file we want to copy, and the second is the path to the file want to create as a copy.

echo "test" > example.txt # Create a file called example.txt
cp example ../example-in-directory-above.txt

ls # Should only see one example file here, the original `example.txt`

cd ..
ls # Should only see one example file here, the copied `example-in-directory-above.txt`

Verify that these files do in fact have the same contents.

mv: Move and rename file or directory

In Bash moving files and renaming files are the same thing. Intuitively, this can be thought of as ‘editing the path of a file’. Moving files is done with the mv (“Move”) command, which similar to cp takes two arguments: the first being the path to the file to move, and the second is the new path the file should have.

echo "example" > example.txt # Create an example file to move
mv example.txt ~/test.txt # Move the example file to the home directory, and rename it to test.txt

mv works on both files and directories.

mkdir example-dir
mv example-dir ~/test-dir # Move the example directory to the home directory, and rename it to test-dir

clear: Remove old output and commands from your terminal window

This one is one of the commands I run the most often. I like working with a clean screen so that the outputs I see on my screen is relevant to the current context I’m working wtih. To achieve this I run clear between each contextually different sets of commands that I run.

cd repos
mkdir awesome-repo
cd awesome-repo
echo "# Awesome Repo" > README.md
# more work...

clear
cd # Return to home directory
cd Downloads
rm img3514.jpeg # Delete a picture I had in my Downloads directory

Conclusion

As you get more used to the command line you’ll pick up more and more commands and Bash tricks. Perhaps you also find new ways of doing things that you prefer over the ones listed here. That being said, you should now know your basic commands and learning new commands should be easier in the future.