Linux Part 1: The Fundamentals

Hello readers. In this article, we’re going to be exploring and learning the fundamentals of this hugely popular, powerful, open-source operating system.

1. Some Important Definitions

Command: An instruction typed in the terminal and submitted to the shell for interpretation [1].

Shell: In computing, a shell is a computer program that exposes an operating system’s services to a human user or another program

Terminal: An interface that allows you to access the command line.

2. Opening and Closing the Terminal

Opening the terminal

CTRL + ALT + T

Closing the terminal

CTRL + D

3. Basic Commands

This command displays the files and directories located in your current directory.

Used to create a file with name.

Used to display lines of text/string that is passed as an argument.

Show the current date and time.

Display a calendar

It reads data from the file and gives its content as output.

4. Command History

The Bash shell keeps track of the most recent command you have used. You can recall these commands and even reuse them. A helpful build-in command that lets you explore and manage these previously issued commands is the command.

Show commands previously entered (command history).

Run the previous command.

Run the command that is on line 40 of the output from the history command. (replace “40” as needed).

5. Command Structure

Each command follows the same overarching structure:

commandName –options arguments

5.1 Command Names

must be a valid program on the Shell’s Path. To check this, use the command like so:

If a path is returned, then the commandName is valid and vice versa.

5.2 Options

You can specify options for each command to customize the command's behavior. These can be either “short-form” options or “long-form” options. Each command behaves differently so check the command’s manual (man) page for the specifics of each command’s behavior [1].

5.2.1 Short-form Options

Short-form options are where a letter defines an option. Each option is prepended by a dash “-“ like so[1]:

commandName –a –b –c args 

To save typing, you could join together the options:

commandName –abc args 

Both of these formats are equivalent.

5.2.2 Long-form Options

For some commands, there are long-form options defined to make options easier to identify. Long-form options are usually prepended by a double dash. Long-form options cannot be joined together like short-form options can[1].

is equivalent to

5.2.3 Command Line Arguments

Command line arguments are a type of input that commands operate on. Some commands can take an unlimited amount of inputs, some take a specific amount, and some take none at all. Consult the manual page for the specific command for more information[1].

cal 12 2020

Here the command has 2 command line arguments. The number 12 and the number 2020.

5.2.4 Arguments for Options

Sometimes, command options can also take their own arguments (inputs).

cal –A 1 –B 1 12 2020

Here the command has 2 options; A(After) and B(Before).
The A option has its own argument (1).
The B option has its own argument (1).
And the cal command has 2 command line arguments (12 and 2020).

“Linux is only free if your time has no value.”
Jamie Zawinski

6. Using the Manual

Like we saw each of the Linux commands is implemented slightly differently and they behave slightly differently. So other than memorizing a million different implementations of things how can we actually know how to use each of the Linux commands properly?[1]

Well, the answer is to use The Manual Pages known as Man pages for sure. Now Linux comes with an incredible set of manual pages that will detail the workings of pretty much everything on the system including commands[1].

In this chapter, we’re going to be covering how the manual is structured now because the manual is massive. Having an understanding of the structure behind it is going to make it so much easier to navigate because then you’ll know not only what is available in the manual but also where to look for it[1].

: Search the manual for pages matching <search term>.

: Open the man page called <page name> in section 5 of the manual. (replace <page name> and 5 as required)

: Open the man page called <page name> in section 1 of the manual.

By using the -k command-line option. you tell the command to search and list manual pages by considering input as a regular expression.

Now let me show you how to independently search for stuff using the manual and show you how to you would learn about a new command from scratch. Let us open the terminal and let's search for a command that lists a directory content, we do that by typing the following[1].

They all seem to say list directory contents but the one that I’m specifically looking for I’ve heard about online is the command. We can see that it’s a user command because it’s in the first section of the manual.

So we could open it using but because it’s in the first section we can type only and we get the following.

man ls

Now looking at the synopsis section we can see that if you type then you can have multiple optional options. We know they’re optional because they’re surrounded by square brackets and you can have multiple of them because they’ve got the ellipsis after it and then you have multiple optional filenames or paths to directories and can have multiple of those because we can see the dot dot dot. But the one thing that you might notice is because everything else is optional right, the only thing you actually need to enter is the command itself and it will work.

7. Command Input and Output

Photo by Ziyad Yehia [1]

Standard Data Streams can be and are identified using their stream number. Redirection of the standard output of one command to the standard input of another command is known as .[1]

7.1 Redirecting Standard Output:

is stream number . There are 2 methods to redirect standard output. The long-form, using the stream number:

commandName –options arguments 1> destination 

Or the short form, with no stream number:

commandName –options arguments > destination

After typing the command and run that. the will wait for you to type your words and then when you finish press and then you will see the file in the desktop created [1].

if you use the same command again and type something else then the file will be overridden because redirection by default will remove everything in a file before writing to it. The fancy word for that is called truncation. We can fix that by using redirection without truncation by typing this command instead[1].

cat >> output.txt
output.txt

So this process when using double arrows is called appending to a file.

7.2 Redirecting Standard Input:

is stream number . There are 2 methods to redirect standard Input. The long-form, using the stream number:

commandName –options arguments 0< input_source 

Or the short form, with no stream number:

commandName –options arguments < input_source

We can read standard input from and redirect the output to

Let me open up another terminal I’m going to run the command, which will actually tell us where this terminal is located.

Now what I’m going to do is I’m going to redirect the content from hello.txt and I'm going to write it to another terminal, just like this

We can see that the data has been passed from one terminal to another. Isn't that awesome? :)

8. Piping

Photo by Ziyad Yehia [1]

Piping is the connection of the standard output of one command to the standard input of another command. Piping using the pipe character which is accessed by pressing on most keyboards[1].

Here is how you would pipe together commandOne and commandTwo:

 commandOne –options arguments | commandTwo –options arguments

Notice how both commands can have their own options and command line arguments as usual. This piping can go on for as long as is required with as many commands as is required.

So let’s take for example the command. Now we can write the standard output of the command to a text file called using redirection.

is a shortcut for Lördag, its Swedish word and means Saturday in English. :)

Now let us cut out just the day of the week. One way to do it would be to use the command. First, we want to read the date that takes the file into the standard input for the command then the will cut up a file and give you specific columns. So if we look at above you can imagine that the file is broken up into columns with a space between each column, the day of the week in this case its Saturday is the first column or the first field as it's known in the command. Now we want to get only the first field. To that we need to give it a and delimiter tells you what divides the columns, so we need to tell it that space is divide or delimit the columns because spaces are what divided up these specific columns in the file. So we need to type and put space into it. Then we need to say which field we want by typing and then press enter to get the result. Let me show you in the terminal how we type it and get the first field which is in my case its :)

cut command

Let me show a better way to do that by using . So instead we can pipe the standard output of the data command directly into the standard input of the cut command.

We can save the result in a by typing the following:

date | cut --delimiter=" " --fields=1 > today.txt

8.1 Taking “Snapshots” of pipeline data using the command

Photo from Wikipedia

Now just like a T-junction in a pipe of water, we can use the command to cause our data to flow in two directions. So the command allows us to pass the data on to the rest of the pipe but it also allows us to keep it in a file. So you can see here that we’ve got a command on the left that is trying to pipe its data across into another command on the right, in the middle there we pipe it through the command, and the command we give it a command-line argument a that could be any file name that you want. And what happens is the data does pass all the way through from the standard output of one command to the standard input of the other. But it also falls down. You can imagine it falling down from your command line, like into a file called or whatever file name you gave the command. So that’s how the command works in theory, let’s go ahead and see it work in practice. [1]

Instead of showing the day of the week to the terminal, we can save it in a file called by typing in the end

9. Aliases

The command is another useful build-in shell command. A command alias allows you to create an alias name for common commands (along with their parameters) to help keep your typing to a minumum[2]

To see a list of the active aliases, use the command along with the parameter.

$ alias -p

You define aliases in your file in your home directory. If it does not exist, you need to create it. Note that the preceding period (.) must be included and there should be no file extension (such as .txt, or .pdf).

Here is how you define an alias in

alias aliasName=”THING YOU WANT TO ALIAS”

Notice that there are no spaces between the equals sign (=) and the aliasName and the quotes (“). The quotes can be double quote (“) or single quotes (‘).

Let’s take an example:

alias calmagic='cal –A 1 –B 1 2 2021'

Let me show you in practice:

is a bash shell built-in command that executes the content of the file passed as an argument, in the current shell. If you want to add more aliases to the .bash_aliases then just type and write your aliases, or use to edit the file and add some aliases.

That’s it, well done for reaching the end of the first part.

Acknowledgment

Special thanks to Ziyad Yehia for his amazing course on Udemy that helped me a lot with building this article. Take a look at his course/explanation here. https://www.udemy.com/course/linux-mastery/

References

[1]: Ziyad Yehia . Linux Mastery: Master the Linux Command Line in 11.5 Hours https://www.udemy.com/course/linux-mastery/

[2]: Richard Blum, Christine Bresnahan: Linux Command Line and Shell Scripting Bible, 4th Edition.

— Maher;

Part two (The Linux File System) and more can be found in Ziyad Yehia course in Udemy, I recommend you to test it.