Terminal Commands I Use On Linux for System Management - Part 2
Tags: linux, webdev, beginners, devops
Published: 2022-09-11 17:08:30

- [Introduction](#introduction)
- [Head](#head)
    - [What does head do?](#what-does-head-do)
    - [How to use it](#how-to-use-it)
- [Tail](#tail)
    - [What does tail do?](#what-does-tail-do)
    - [How to use tail](#how-to-use-tail)
- [More](#more)
    - [What does more do?](#what-does-more-do)
    - [How to use more](#how-to-use-more)
- [Less](#less)
    - [What does less do?](#what-does-less-do)
    - [How to use less](#how-to-use-less)
- [Grep](#grep)
    - [What does grep do?](#what-does-grep-do)
    - [How to use grep](#how-to-use-grep)
- [Whereis](#whereis)
    - [What does whereis do?](#what-does-whereis-do)
    - [How to use whereis](#how-to-use-whereis)
- [PWD](#pwd)
    - [What does pwd do?](#what-does-pwd-do)
    - [How to use pwd](#how-to-use-pwd)
- [whoami](#whoami)
    - [What does whoami do?](#what-does-whoami-do)
    - [How to use whoami](#how-to-use-whoami)
- [id](#id)
    - [What does id do?](#what-does-id-do)
    - [How to use id](#how-to-use-id)
- [login](#login)
    - [What does login do?](#what-does-login-do)
    - [How to use login](#how-to-use-login)

## Introduction

This article will cover ten additional tools I use when working on a remote (or local) system at a terminal.

The tools listed will mostly be targeted at reading files, be that text documents or more typically, log files that are mainly stored in the /var/log directory on most Linux / macOS / UNIX systems.

## 1. Head

### What does head do?

The `head` command is used to read from the start of a file and go forward a number of lines. It is useful if you just need to look at the contents of the beginning of a file, such as a file that contains a license key for example.

### How to use it

``` console
head -n 5 /var/log/dmesg
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfk9xj3l3v1ntw1wc80m.png)

To break down what the above did; the `head` command was used to read the first five lines (`-n 5` or `--lines 5`) for the file named `dmesg` in the `/var/log` directory.

If you don't specify the `-n` flag with the command, the default number of lines displayed will be *ten*.

## 2. Tail

### What does tail do?

The `tail` command is the opposite of head. Rather than showing a number of lines from the beginning of a file, tail starts from the bottom.

Tail is very useful for when you need to look at log files as the latest log entries are typically written at the bottom of a file.

### How to use tail

```console
tail -n 5 /var/log/ufw.log
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tjhi6z59mbwujg3tr9h.png)

As you can see, the `tail` command is very similar to the head command in terms of how it is run.

There is one switch that I would recommend using, which is `-f`. What this will do is to keep the file open on the screen and check it every so many seconds and show any new changes that are made to the file. This is useful for when you need to track new activity in a log file. For example, you may be working with an user to replicate an issue and you need to see what they do in the log file in as close to real-time as possible.

If you need to change the time interval for which the update occurs, use `-s`, followed by the number of seconds. For example, check for changes every two seconds:

```console
tail -n 5 -f -s 2 /var/log/ufw.log
```

To exit out of the the above tail command, press `ctrl+c`.

## 3. More

### What does more do?

The `more` command is a simple tool that allows you to read a file as pages. What that means is that when it reads the file, it will only output as many of the lines that will fit onto the screen or terminal window and pause to allow you to read it. When you have finished reading a page, you can press the space-bar to move onto the next page.

### How to use more

``` console
more /var/log/dmesg
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1mqszusa67z7bi58pe7.png)

Once the more command is running, the contents of `/var/log/dmesg` are shown. You can go to the next page by pressing space-bar or if you just want to show the next line, press enter.

To quit out and return to the command prompt, press `q`.

Now, there is one limitation of more. You can't go back a page, which brings us to...

## 4. Less

### What does less do?

The `less` command is basically the same as more but it allows you to go back a page, rather than just forwards. There are other things it does differently but adding the ability to go back is the main one.

Why is it called less? Basically, less is the opposite of more.

### How to use less

``` console
less /var/log/dmesg
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtxhxqq5wqazbcrivu8j.png)

Although some of the output is different, the functionality and navigation keys are the same as `more`.

To go back a page, press `b`. You can go back multiple pages, all the way back to the beginning of the file.

## 5. Grep

### What does grep do?

The `grep` command is typically used in conjunction with the output from another command (via a pipe (`|`)) to search for something in the output. For example, find *UEFI* in the dmesg log file.

### How to use grep

``` console
cat /var/log/dmesg | grep -n "UEFI"
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8lo1de38pdibfs2lbes.png)

Let us go over what just happened. The `cat` command was used to read the `/var/log/dmesg` log file. The output was then piped (`|`) to the `grep` command, which would then search for the phrase `UEFI` and show the contents of line for each match. The line number (`-n`) that a match was found on would be shown at the start of each line (`5:`).

I would suggest looking at the man page for `grep` as there are a lot of ways you can use it to find information, including using regex.

## 6. Whereis

### What does whereis do?

The `whereis` command is used to locate an application binary, source or man (manual) page for a command that is installed on the system.

The main purpose for `whereis` is to find where a command is stored or in some cases where all the installed locations of a command are. For example, you run `python` but the version it runs is different from what you expect. Using `whereis` can help you determine the location of the python binary you need to run.

### How to use whereis

``` console
whereis -b python
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ry2wfhvn8ff40430bwfu.png)

The output from the `whereis` command used shows only the locations for any binaries (`-b`) that start with the name `python`. Each binary that is found is shown with its full path and each located binary is separated by a space.

If you want to limit the search to just source files, use `-s` instead of `-b`. You can also limit the search for man pages by using `-m`. Alternatively, if you want to search for binaries and source files but not man pages, use `-bs`.

If you don't specify a limit, it will search for all types by default.

## 7. PWD

### What does pwd do?

The `pwd` command is used to simply show which directory you are currently in.

### How to use pwd

``` console
pwd
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpa0kjm9hxk05bt47yvp.png)

The output from the `pwd` command is the full path of the directory you are currently in.

## 8. whoami

### What does whoami do?

The whoami command is used to show you the username that you are currently active as at the terminal or console.

### How to use whoami

``` console
whoami
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3bkg1qmwxov6sww77pk.png)

The only output you will get is the username that you are currently logged in as.

## 09. id

### What does id do?

The `id` command provides user and group membership details about the current user that you are logged in as or another username that you specify.

The main use for this command is to show what groups a user is a member of. This helps to determine if they have group permissions to a file or folder.

### How to use id

Show the user and group information for the current user, including UID and GID numbers:

``` console
id
```

Show all the group (`G`) names (`n`) for the current user:

``` console
id -Gn
```

Show the user and group information for the root user, including its UID and GID numbers:

``` console
id root
```

Show all the group (`G`) names (`n`) for the root user:

``` console
id root -Gn
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qsm6z71rp0m41uuduum3.png)

## 10. login

### What does login do?

The `login` command allows you to login as a different user whilst you are still logged in with your current user.

The main reason you would use the `login` command is when you need to perform an action, such as read a file that only a particular user has permissions for and you don't want to modify the permissions for it.

The only prerequisite for the `login` command is that you must run it using the `sudo` command. As such you will need to have the right permissions to run `sudo`.

### How to use login

``` console
sudo login neil
```

![Output from above](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04w7fcycw50u0mmg8vgy.png)

As can be seen on the command prompt, the user neil is now logged in (`neil@pi4-server:~$`) and any commands that are run will be done using that user account.

To return to your account, type `exit` to logout.

Thank you for reading. Have a nice day!