Must-Know Linux Commands

Must-Know Linux Commands

Other

A list of must-know Linux commands


Usually, the content on my YouTube Channel and Blog Post is about Kubernetes. However, I have been refreshing my Linux-related knowledge by reading "How Linux Works, 3rd Edition". One of the chapters is about important Linux commands. Thus, I thought I would provide everyone with a summary.

The sections below each refer to Linux commands. To follow along, you need access to a Linux-based environment such as an Operating System running on a Linux distribution, a VM, or container images.

If you don't have access to a Linux-based Operating System or similar, here is how you can spin up a container image of Ubuntu and enter that container image. For this to work as I have detailed below, you need to have Docker Desktop running.

docker run -ti --rm ubuntu /bin/bash

Video for this blog:

First: create a new directory

The mkdir command creates a new directory dir:

mkdir testdirone

mkdir testdirtwo

Second: Create a new file

Create a new file in your current directory with the following command:

touch test.txt

touch testone.txt

touch testtwo.txt

touch test.json

Third: List the files and directories in a directory

The following command allows you to list all of the files and directories in your current directory:

ls

You can also specify the path to a specific directory in which you would like to list the files:

ls ./testdirone

E.g. ls ./

“Use ls -l for a detailed (long) listing and ls -F to display file type information.” Source

Fourth: Move and Copy Files

cp copies files

To copy more than one file to a directory (folder) named dir, try something like this example, which copies the file created in step two:

cp ./test.txt ./testdirone

The mv (move) command works much like cp. However, instead of duplicating the file,  it renames the file to be e.g. either a different name or the same file in a different directory or both. For example, to rename file1 to file2, enter this:

Fifth: Move directory

With the `cd` command, it is possible to change directory. From your current directory, move into the `testdirone` created in the previous step:

cd testdirone

Sixth: View your current directory path

This is done with the following command:

pwd

Seventh: Remove a file -- as in, delete it!

The following command allows you to remove a specific file:

rm test.txt

Eighth: Remove an entire director

Be careful when doing this.

Similar to how you can remove a specific file, you can remove an entire directory:

rmdir ./path-to-the-directory

e.g.

rmdir testdirone

“rm -r dir to delete a directory and its contents, but be careful! This is one of the few commands that can do serious damage, especially if you run it as the superuser. The -r option specifies recursive delete to repeatedly delete everything inside dir. Don’t use the -r flag with globs such as a star (*). And above all, always double-check your command before you run it” (Source)

For example, the following command prints a list of files in the current directory:

echo *
  • at* expands to all filenames that start with at.
  • *at expands to all filenames that end with at.
  • *at* expands to all filenames that contain at.

Ninth: Look for specific files across a directory

If you want to check every file in /etc that contains the word root, you could use this command:

grep root /etc/*

“Two of the most important grep options are -i (for case-insensitive matches) and -v (which inverts the search—that is, prints all lines that don’t match). There is also a more powerful variant called egrep (which is just a synonym for grep -E).

Here’s an example of sending the output of a grep command to less:

grep ie /usr/share/dict/words | less

Source

Tenth: Edit a file

For this, you need to use a text editor. I started off with nano, which is a super user-friendly text editor, and now I am using vim a lot.

Move back to your original directory:

cd ..
nano testone.txt

nano testtwo.txt

Note that if you don't have nano installed, you can install it on Ubuntu with the following commands (it should come out of the box on Ubuntu and Debian based operating systems):

sudo apt update
sudo apt install nano

Eleventh: View differences between two files

To see the differences between two text files, use diff:

diff testone.txt testtwo.txt

Twelveth: Locate Files within directories

Sometimes you are working with hundred of files in just one directory or within sub-directories. The following command will help you find a specific file:

find testdirone

Thirteenth: Display the first and the last lines of a file

“head /etc/passwd shows the first 10 lines of the password file, and tail /etc/passwd shows the last 10 lines.

To change the number of lines to display, use the -n option, where n is the number of lines you want to see (for example, head -5 /etc/passwd). To print lines starting at line n, use tail +n.”

Alternatively, try:

head -1 testone.txt

Fourteenth: Change the password of your account

To change the password of your account, use the passwd command.

Fifteenth: Let’s look at the terminal variable

There are shell variables and environment variables.

This is a shell variable:

STUFF=blah

Once we export it, it becomes an environment variable:

export STUFF

“The main difference between environment and shell variables is that the operating system passes all of your shell’s environment variables to programs that the shell runs, whereas shell variables cannot be accessed in the commands that you run.” Source

Sixteenth: How do you know what you don’t know?

If you are looking for a command related to “sort” use the following command:

man -k sort

“Some time ago, the GNU Project decided that it didn’t like manual pages very much and switched to another format called info (or texinfo). Often this documentation goes further than a typical manual page does, but it can be more complex. To access an info manual, use info with the command name:

info command

Seventeenth: Know which processes are running on your system

To quickly check your system for the different processes that are running on it, use the following command:

ps
PID TTY STAT TIME COMMAND
520 p0  S    0:00 -bash
545  ?  S    3:59 /usr/X11R6/bin/ctwm -W
548  ?  S    0:10 xclock -geometry -0-0
2159 pd  SW   0:00 /usr/bin/vi lib/addresses
31956 p3  R    0:00 ps

The fields are as follows (Source):

  1. PID The process ID.
  2. TTY The terminal device where the process is running. More about this later.
  3. STAT The process status—that is, what the process is doing and where its memory resides. For example, S means sleeping and R means running. (See the ps(1) manual page for a description of all the symbols.)
  4. TIME The amount of CPU time in minutes and seconds that the process has used so far. In other words, the total amount of time that the process has spent running instructions on the processor. Remember that because processes don’t run constantly, this is different from the time since the process started (or “wall-clock time”).
  5. COMMAND This one might seem obvious as the command used to run the program, but be aware that a process can change this field from its original value. Furthermore, the shell can perform glob expansion, and this field will reflect the expanded command instead of what you enter at the prompt.

Eighteenth: The Linux directory structure explained (Source):

  1. /bin Contains ready-to-run programs (also known as executables), including most of the basic Unix commands such as ls and cp. Most of the programs in /bin are in binary format, having been created by a C compiler, but some are shell scripts in modern systems.
  2. /dev Contains device files. You’ll learn more about these in Chapter 3.
  3. /etc This core system configuration directory (pronounced EHT-see) contains the user password, boot, device, networking, and other setup files.
  4. /home Holds home (personal) directories for regular users. Most Unix installations conform to this standard.
  5. /lib An abbreviation for library, this directory holds library files containing code that executables can use. There are two types of libraries: static and shared. The /lib directory should contain only shared libraries, but other lib directories, such as /usr/lib, contain both varieties as well as other auxiliary files. (We’ll discuss shared libraries in more detail in Chapter 15.)
  6. /proc Provides system statistics through a browsable directory-and-file interface. Much of the /proc subdirectory structure on Linux is unique, but many other Unix variants have similar features. The /proc directory contains information about currently running processes as well as some kernel parameters.
  7. /run Contains runtime data specific to the system, including certain process IDs, socket files, status records, and, in many cases, system logging. This is a relatively recent addition to the root directory; in older systems, you can find it in /var/run. On newer systems, /var/run is a symbolic link to /run.
  8. /sys This directory is similar to /proc in that it provides a device and system interface. You’ll read more about /sys in Chapter 3.
  9. /sbin The place for system executables. Programs in /sbin directories relate to system management, so regular users usually do not have /sbin components in their command paths. Many of the utilities found here don’t work if not run as root.
  10. /tmp A storage area for smaller, temporary files that you don’t care much about. Any user may read to and write from /tmp, but the user may not have permission to access another user’s files there. Many programs use this directory as a workspace. If something is extremely important, don’t put it in /tmp because most distributions clear /tmp when the machine boots and some even remove its old files periodically. Also, don’t let /tmp fill up with garbage because its space is usually shared with something critical (the rest of /, for example).
  11. /usr Although pronounced “user,” this subdirectory has no user files. Instead, it contains a large directory hierarchy, including the bulk of the Linux system. Many of the directory names in /usr are the same as those in the root directory (like /usr/bin and /usr/lib), and they hold the same type of files. (The reason that the root directory does not contain the complete system is primarily historic—in the past, it was to keep space requirements low for the root.)
  12. /var The variable subdirectory, where programs record information that can change over the course of time. System logging, user tracking, caches, and other files that system programs create and manage are here. (You’ll notice a /var/tmp directory here, but the system doesn’t wipe it on boot.)
  13. /boot Contains kernel boot loader files. These files pertain only to the very first stage of the Linux startup procedure, so you won’t find information about how Linux starts up its services in this directory. See Chapter 5 for more about this.
  14. /media A base attachment point for removable media such as flash drives that is found in many distributions.
  15. /opt This may contain additional third-party software. Many systems don’t use /opt.

Nineteenth: Find the sudo logs

It is useful to know what commands have been run with higher privileges before. The following command provides a log in most systems:

journalctl SYSLOG_IDENTIFIER=sudo

Again, if you are using the container image of Ubuntu, journalctl is likely not installed and you will have to install it first:

apt-get install systemd

Like other tools, journalctl is quite comprehensive and you can do a lot with it. I suggest you to look in detail at the different options.

Summarising

I am aware that this is just a very very small list of the Terminal commands that are super useful to know to navigate a Linux-based system from the Terminal. What commands did I miss?

Let me know in the comments on YouTube if you would like to see more tutorials such as this one.