DEV Community

Cover image for Terminal Commands I Use On Linux for System Management - Part 2
dev_neil_a
dev_neil_a

Posted on • Updated on

Terminal Commands I Use On Linux for System Management - Part 2

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

head -n 5 /var/log/dmesg
Enter fullscreen mode Exit fullscreen mode

Output from above

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

tail -n 5 /var/log/ufw.log
Enter fullscreen mode Exit fullscreen mode

Output from above

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:

tail -n 5 -f -s 2 /var/log/ufw.log
Enter fullscreen mode Exit fullscreen mode

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

more /var/log/dmesg
Enter fullscreen mode Exit fullscreen mode

Output from above

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

less /var/log/dmesg
Enter fullscreen mode Exit fullscreen mode

Output from above

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

cat /var/log/dmesg | grep -n "UEFI"
Enter fullscreen mode Exit fullscreen mode

Output from above

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

whereis -b python
Enter fullscreen mode Exit fullscreen mode

Output from above

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

pwd
Enter fullscreen mode Exit fullscreen mode

Output from above

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

whoami
Enter fullscreen mode Exit fullscreen mode

Output from above

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:

id
Enter fullscreen mode Exit fullscreen mode

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

id -Gn
Enter fullscreen mode Exit fullscreen mode

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

id root
Enter fullscreen mode Exit fullscreen mode

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

id root -Gn
Enter fullscreen mode Exit fullscreen mode

Output from above

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

sudo login neil
Enter fullscreen mode Exit fullscreen mode

Output from above

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!

Top comments (0)