The most useful terminal commands (Linux, Mac, and UNIX)
Tags: linux, beginners, codenewbie, opensource
Published: 2021-02-03 13:02:27

In this beginner tutorial, I'll review the most common and useful commands that you can run in almost any UNIX-like environment, including Linux and Mac OS X.

<!--more-->

## `pwd` - print working directory

`pwd` allows you to output the current directory you’re in. It provides a short and easy answer to the question "Where am I?". It’s very useful to run the `pwd` command at the beginning of your terminal session. Here’s an example of running `pwd` right after logging into the server as a root.

```bash
root@scw-tender-lewin:~# pwd
/root
```

## `ls` - list files and directories

To view the content of the directory, you can use the `ls` (*list*) command. The basic usage goes like this:

```shell
root@scw-tender-lewin:/# ls
bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv       sys  usr  vmlinuz
boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  swapfile  tmp  var  vmlinuz.old
```

If you want to check out the content of a specific directory, i.e. `var`, you can do it by adding its path right after the `ls`:

```shell
root@scw-tender-lewin:/# ls /var
backups  cache  crash  lib  local  lock  log  mail  opt  run  snap  spool  tmp
```

There are also a couple of useful flags that will change the format of the output of the `ls` command.

### Long list view

The `-l` flag will do the long list view, which shows additional information like permissions, owner, size, date, etc.:

```shell
root@scw-tender-lewin:/# ls -l
total 4194396
drwxr-xr-x   2 root root       4096 May 15 06:46 bin
drwxr-xr-x   4 root root       4096 Sep  3 06:15 boot
drwxr-xr-x  16 root root       3680 May 20 06:35 dev
drwxrwxr-x  93 root root       4096 Sep  3 06:15 etc
drwxr-xr-x   3 root root       4096 Mar  6 09:14 home
lrwxrwxrwx   1 root root         34 Sep  3 06:15 initrd.img -> boot/initrd.img-4.15.0-115-generic
lrwxrwxrwx   1 root root         34 Sep  3 06:15 initrd.img.old -> boot/initrd.img-4.15.0-112-generic
drwxr-xr-x  23 root root       4096 Mar  6 09:16 lib
drwxr-xr-x   2 root root       4096 Jul  8 06:20 lib64
drwx------   2 root root      16384 Mar  5  2019 lost+found
drwxr-xr-x   2 root root       4096 Feb  4  2019 media
drwxr-xr-x   2 root root       4096 Feb  4  2019 mnt
drwxr-xr-x   3 root root       4096 Mar  6 09:17 opt
dr-xr-xr-x 189 root root          0 May  4 06:16 proc
drwx------   8 root root       4096 Mar 19 08:13 root
drwxr-xr-x  28 root root       1120 Sep  3 07:21 run
drwxr-xr-x   2 root root      12288 Jul  8 06:20 sbin
drwxr-xr-x   2 root root       4096 Mar  6 09:14 snap
drwxr-xr-x   2 root root       4096 Feb  4  2019 srv
-rw-------   1 root root 4294967296 Mar  6 10:10 swapfile
dr-xr-xr-x  13 root root          0 May  4 06:16 sys
drwxrwxrwt  10 root root       4096 Sep  3 07:22 tmp
drwxrwxr-x  10 root root       4096 Mar  5  2019 usr
drwxr-xr-x  13 root root       4096 Mar  5  2019 var
lrwxrwxrwx   1 root root         31 Sep  3 06:15 vmlinuz -> boot/vmlinuz-4.15.0-115-generic
lrwxrwxrwx   1 root root         31 Sep  3 06:15 vmlinuz.old -> boot/vmlinuz-4.15.0-112-generic
```

By the way, if you want to check some specific information about a single file, you can add it’s name the same way we did with the directory. Here’s how you could check the long list view on the `swapfile`:

```sh
root@scw-tender-lewin:/# ls -l swapfile
-rw------- 1 root root 4294967296 Mar  6 10:10 swapfile
```

### Hidden files and folders

To view the hidden files and folders you should add the `-a` flag to the `ls` command:

```shell
root@scw-tender-lewin:/# ls -a
.   bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv       sys  usr  vmlinuz
..  boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  swapfile  tmp  var  vmlinuz.old

```

This way, we always get at least two additional records: `.` and `..` . A single dot stands for the current directory and the double dot is used to access the directory one level above the current.

### Sorting the output of the `ls` command

The `-S` flag is used to sort the results by size in ascending order.

```shell
root@scw-tender-lewin:/# ls -S
swapfile    sbin  boot  home  lib64  mnt  root  srv  usr  dev  initrd.img      vmlinuz      proc
lost+found  bin   etc   lib   media  opt  snap  tmp  var  run  initrd.img.old  vmlinuz.old  sys
```

### Combining the flags

Quite often you want to achieve a complex behavior. For example, you might want to get both the long list view with `-l`, the human-readable size output with `-h` and sort the results with the `-S` flag.

To achieve this, you can just combine all flags into one. The order of the flags doesn’t matter here and `-lhS` is equivalent to `-hSl`, so you can use any sequence you like:

```shell
root@scw-tender-lewin:/# ls -lhS
total 4.1G
-rw-------   1 root root 4.0G Mar  6 10:10 swapfile
drwx------   2 root root  16K Mar  5  2019 lost+found
drwxr-xr-x   2 root root  12K Jul  8 06:20 sbin
drwxr-xr-x  23 root root 4.0K Sep  3 06:15 .
drwxr-xr-x  23 root root 4.0K Sep  3 06:15 ..
drwxr-xr-x   2 root root 4.0K May 15 06:46 bin
drwxr-xr-x   4 root root 4.0K Sep  3 06:15 boot
drwxrwxr-x  93 root root 4.0K Sep  3 06:15 etc
drwxr-xr-x   3 root root 4.0K Mar  6 09:14 home
drwxr-xr-x  23 root root 4.0K Mar  6 09:16 lib
drwxr-xr-x   2 root root 4.0K Jul  8 06:20 lib64
drwxr-xr-x   2 root root 4.0K Feb  4  2019 media
drwxr-xr-x   2 root root 4.0K Feb  4  2019 mnt
drwxr-xr-x   3 root root 4.0K Mar  6 09:17 opt
drwx------   8 root root 4.0K Mar 19 08:13 root
drwxr-xr-x   2 root root 4.0K Mar  6 09:14 snap
drwxr-xr-x   2 root root 4.0K Feb  4  2019 srv
drwxrwxrwt  10 root root 4.0K Sep  3 07:22 tmp
drwxrwxr-x  10 root root 4.0K Mar  5  2019 usr
drwxr-xr-x  13 root root 4.0K Mar  5  2019 var
drwxr-xr-x  16 root root 3.6K May 20 06:35 dev
drwxr-xr-x  28 root root 1.1K Sep  3 07:21 run
lrwxrwxrwx   1 root root   34 Sep  3 06:15 initrd.img -> boot/initrd.img-4.15.0-115-generic
lrwxrwxrwx   1 root root   34 Sep  3 06:15 initrd.img.old -> boot/initrd.img-4.15.0-112-generic
lrwxrwxrwx   1 root root   31 Sep  3 06:15 vmlinuz -> boot/vmlinuz-4.15.0-115-generic
lrwxrwxrwx   1 root root   31 Sep  3 06:15 vmlinuz.old -> boot/vmlinuz-4.15.0-112-generic
dr-xr-xr-x 191 root root    0 May  4 06:16 proc
dr-xr-xr-x  13 root root    0 Sep  3 08:25 sys
```

## `cd` - change directory

So, after you’ve learned everything about the content in the directory, you probably want to go somewhere else. The `cd` command will take a directory name as a single argument and move you there. The `cd` command only prints the output if there’s an error, so you might want to immediately use the `ls` command to look around:

```shell
root@scw-tender-lewin:/# cd var
root@scw-tender-lewin:/var# ls -a
.  ..  backups  cache  crash  lib  local  lock  log  mail  opt  run  snap  spool  tmp
```

### Moving into the parent folder

To move one folder above you should use the `..` as a directory name. To move two or more levels above you can type `cd ../..` and so on.

If you’ve tried to navigate to the directory that doesn’t exist, you’ll see the following message:

```shell
root@scw-tender-lewin:/var# cd local42
-bash: cd: local42: No such file or directory
root@scw-tender-lewin:/var# cd ..
root@scw-tender-lewin:/# ls
bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv       sys  usr  vmlinuz
boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  swapfile  tmp  var  vmlinuz.old
```

### Going home

If you use the `~` symbol as a directory name, you’ll be moved into your home folder. It’s a useful shortcut as it doesn’t matter where you are at the moment and you might now know the exact path to the home directory at some point.

```shell
root@scw-tender-lewin:/# cd ~
root@scw-tender-lewin:~# pwd
/root
```

## `touch` - create a file

The `touch` command allows you to create an empty file. So, here’s the plan:

1. Navigate to our home directory with `cd ~`
2. Make sure it’s empty with `ls`
3. Create a new file using `touch` and providing a filename right after it
4. Run the `ls` command once again to see that the newly created file exists

```shell
root@scw-tender-lewin:/var/log# cd ~
root@scw-tender-lewin:~# ls
root@scw-tender-lewin:~# touch hello.txt
root@scw-tender-lewin:~# ls
hello.txt
```

## `mkdir` - create a directory

To create a directory in Linux you should use the `mkdir` command. It works almost the same way as `touch` and expects the directory name.

```shell
root@scw-tender-lewin:~# mkdir internal
root@scw-tender-lewin:~# ls
hello.txt  internal
```

## `man` - display a manual / get help

And finally, the superpower! The `man` command will give you a short manual on any command, even itself. If you want to do something but have forgotten an important flag or the command syntax, just type `man` followed by the command name to get an instant manual. Here’s an example:

```shell
root@scw-tender-lewin:~# man ls
NAME
     ls -- list directory contents

SYNOPSIS
     ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1%] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information.
     For each operand that names a file of type directory, ls displays the names of files contained within that directory, as well as any
     requested, associated information.

     If no operands are given, the contents of the current directory are displayed.  If more than one operand is given, non-directory operands
     are displayed first; directory and non-directory operands are sorted separately and in lexicographical order.

     The following options are available:
...
```

## Conclusion

In this article, we’ve reviewed the easiest to use and straightforward Linux, macOS, and *nix system commands like `ls`, `pwd`, `cd`, `touch`, `mkdir`, and `man`.

If you're just starting your journey into the software development world, you might be intimidated by the looks of the all-mighty terminal or command-line.

But as you see, it's quite easy and straightforward if you start at the right spot.