Basic Unix Commands

You can significantly increase your productivity and understanding of your operating system, by learning a few basic shell commands. These commands work in Bash, the most common shell on Linux and MacOS. Alternative shells are mostly compatible with Bash, so they should work everywhere.

If these basics are well known to you, please skip to the next sections to learn some more advanced commands and shell scripting.

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 -lha
total: 32
drwxr-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 on the Bash command line. 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, e.g. to the backups folder in the current working directory:

$ mv file1.txt backups/

mkdir:  Make directory

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

$ mkdir Projects

There's one very 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 a directory called Projects, you can do so in one sweep with:

$ mkdir -p Projects/my_first_project

rmdir: Remove directory

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: remove stuff

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' as you might have grown accustomed to in other OSes.

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 is  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 from your command history.


If you liked this page, please share it with a fellow learner: