Introduction to the command line

To kick things off, let's take a look at a typical command line to clarify some lingo so we're all on the same page about what we call the different parts of a typical command.

$ rm -f filename.txt

The $ is the so-called prompt followed by our actual command rf. The command has an option or flag -f followed by the argument filename.txt.

The basics — How to navigate the terminal

Before we move onto advanced concepts, let's take a look at the basics, so we're able to navigate, perform basic file operations and have a good understanding of the fundamentals.

Just like the finder on MacOS, the explorer on Windows or Nautilus on Linux, the CLI can be used to navigate the filesystem. To see what's inside our current directory, we can use the ls command and will get a list of all files in our current working directory:

command line
ls -la
-rw-r--r-- 1 ben staff 1.1K Nov 3 10:15 .config
-rw-r--r-- 1 ben staff 1.5K Nov 3 10:45 README.md
drwxr-xr-x 3 ben staff 196B Nov 8 07:27 cache
drwxr-xr-x 9 ben staff 288B Nov 9 07:03 components
drwxr-xr-x 5 ben staff 160B Nov 9 08:14 content
drwxr-xr-x 4 ben staff 128B Nov 8 07:27 scripts
drwxr-xr-x 6 ben staff 192B Nov 9 07:03 src
drwxr-xr-x 3 ben staff 296B Nov 8 07:27 store

As you can see we passed the -la option, which are actually two parameters and they are -l to show an extended list and -a to show all files, including hidden files (starting with a dot). The list option shows us a lot of info, like the permissions, file size and of course the file and folder names. It is generally a little easier to see all the files this way, than just using ls. The permissions block on the very left also tells us whether we're looking at a file or folder, indicated by the d for directory on the very left.

Changing directories

Now that we know where we are and where we can go, it's time to move around. The cd <folder> command allows us to 'change directory' to another, specified folder. To stay with the example above, we could change into the scripts folder by issuing cd scripts. In many shells the prompt also indicates the current directory we're in like ~/scripts $ ... but additionally, if we want to check where we are, we can issue the pwd command to print the current working directory and see where exactly we are.

command line
pwd
/Users/ben/dev/example-project

Because navigating can be a little tedious with longer paths, there are some shortcuts that can be used in combination with the cd command:

  • The .. parameter takes us up one folder in the directory tree (if available): cd ..
  • The ~ parameter will take us to the currently logged in user's home directory: cd ~
  • The dash parameter - will take us back to the last visited directory. This can be especially handy when juggling with long paths and needing to switch back to the previous directory.

What if I'm stuck?

While the commands above should set you up for success when navigating the filesystem from the command line, it's easy to get lost and need help. Luckily the command line comes with its very own manual, called man. You can call the man command followed by any other command, like the cd command and it will display a helpful manual with all the details, available parameters and more about the command. Here's an example:

command line
man cd
SYNOPSIS
builtin [-options] [args ...]

DESCRIPTION
Shell builtin commands are commands that can be executed within the running shell's process. Note that, in the case of csh(1) builtin commands, the command is executed in a subshell if it occurs as any component of a pipeline except the last.