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

Closing the terminal

3. Basic Commands

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

touch Used to create a file with name.

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

date Show the current date and time.

cal Display a calendar

cat 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 history command.

history Show commands previously entered (command history).

!! Run the previous command.

!40 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:

5.1 Command Names

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

which commandName

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]:

To save typing, you could join together the options:

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].

commandName –a –b -c arguments

is equivalent to

commandName --alpha --beta --charlie arguments

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].

Here the cal 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).

Here the cal 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].

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

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

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

By using the -k command-line option. you tell the man 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 ls 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 man 1 ls but because it’s in the first section we can type only man ls and we get the following.

Now looking at the synopsis section we can see that if you type ls 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 ls command itself and it will work.

7. Command Input and Output

Standard Data Streams can be redirected 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 piping.[1]

7.1 Redirecting Standard Output:

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

Or the short form, with no stream number:

After typing the command cat > output.txt and run that. the cat will wait for you to type your words and then when you finish press enter and then CTRL + C you will see the file in the desktop created output.txt [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].

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

7.2 Redirecting Standard Input:

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

Or the short form, with no stream number:

We can read standard input from output.txt and redirect the output to hello.txt

Let me open up another terminal I’m going to run the tty 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

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 SHIFT + BACKSLASH (\) on most keyboards[1].

Here is how you would pipe together commandOne and commandTwo:

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 date command. Now we can write the standard output of the date command to a text file called date.txt using redirection.

lör 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 cut command. First, we want to read the date that takes the file into the standard input for the command then the cut will cut up a file and give you specific columns. So if we look at date.txt 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 --delimiter 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 --fields 1 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 Sat in my case its lör :)

Let me show a better way to do that by using Pipe . 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 today.txt by typing the following:

8.1 Taking “Snapshots” of pipeline data using the tee command

Now just like a T-junction in a pipe of water, we can use the tee command to cause our data to flow in two directions. So the teecommand 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 tee command, and the command we give it a command-line argument a file.txt 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 file.txt or whatever file name you gave the teecommand. 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 today.txt by typing > today.txt in the end

9. Aliases

The alias 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 -p parameter.

You define aliases in your .bashr_aliases 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 .bashrc_aliases

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:

Let me show you in practice:

source 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 cat >> .bash_aliases and write your aliases, or use nano .bash_aliases 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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store