To view processes dynamically, use top. (Shotts, 2019) It displays which processes are using up your resources the most – the ones using the most resources is always at the top of the list.
An example:
mislav@mislavovo-racunalo:~$ top
top - 20:34:02 up 96 days, 2:55, 1 user, load average: 1.11, 0.79, 0.62
15544 mislav 20 0 11276 3644 3060 R 6.7 0.0 0:00.01 top
...
I won’t go into the details of the column names or the meaning of each element of the summary statistics here. I will leave that for review, when we cover all of the relevant concepts. For now, just remember that top is a command that displays the processes with the most resource-intensive processes closer to the top of the process list.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 137-139
The ps command is used to display the processes which are currently running. By running ps without any options, you get the processes associated with the current Terminal, as so:
mislav@mislavovo-racunalo:~/Linux_folder$ ps
PID TTY TIME CMD
10653 pts/1 00:00:00 bash
31210 pts/1 00:00:00 ps
PID is process ID. TTY is the controlling terminal associated with the process – this field is irrelevant. TIME is processor time the process is taking up – the more processor time a process takes up, the more demanding it is. CMD is the command with which we started the process; it’s the name of the process, with arguments, if arguments exist. (Shotts, 2019)
ps x gives us all the processes that our current user is running. An example:
The new column STAT is short for state. State is the process state – it indicates what the process is doing. The first capital S means that process is sleeping, and that means the process is waiting for input. There are other process states, which you can learn more about by typing man ps and navigating to a section titled PROCESS STATE CODES.
ps aux gives us the processes of all users. Its sample output is:
mislav@mislavovo-racunalo:~/Linux_folder$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
As you can see here, we can see which user started each process (the column USER). We have a couple of additional columns – %CPU signifies how much CPU (in percentages) the process are using, %MEM is the percentage of RAM the process are using, VSZ is the virtual memory size (think of virtual memory as being as large as your hard drive and RAM size combined), RSS (Resident Set Size) is the amount of RAM the process is using in kilobytes, and START is the time when the process had started.
This is how you can take a look at the processes running in your computer.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 135-137
There exist two kinds of processes. Ones that execute in the background and ones that execute in the foreground. (Ward, 2014)
Background processes are useful for situations where the program is going to be running for a long time or you want to be able to use your Terminal while the program is running. We have run all of our commands so far in the foreground – meaning we didn’t get back our Terminal until the command stopped executing. All of the commands we have used so far have relatively short running time so there was no need for running them in the background.
To give you an example of a program I could run in the background, let’s look at Mendeley (a program I use for citations). I am currently running it in the foreground and it looks like this:
See how at the end I don’t have another mislav@mislavovo-racunaloprompt? That’s because Mendeley is running in the foreground. If I wanted to run it in the background, I would write an & at the end of the command to start Mendeley and I would get something like this:
And I have my prompt back. Mendeley is now started in the background. The number 27961 is Mendeley’s process ID which I can use to manipulate it.
This is for you to know that you can run processes both in the background and in the foreground and to know what & is used for. Also, remember that every process has its process ID (PID), no matter if it is started in the foreground or the background.
Hope you found this useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 32-33
Today we are going to talk about identification, authentication and authorization. We will explain it on an example of me getting into a club.
Let’s say I want to get into a club. I approach the bouncer, and he asks me who I am. I answer with my full name. That is identification – I provided my identity. Then the bouncer asks me to prove my identity (either via my identity card or whatever else). That is called authentication. Then, if my identity card is valid and the bouncer verifies who I am, then the bouncer checks “the list” and sees if I am on the list – that is authorization. Authorization is checking if a particular user has particular rights – in this case, if I am allowed to enter the club. I enter the club and thus this LinkedIn article is nearing its end.
These mechanisms exist within the operating system and are important for security. Think of operating system as the club bouncer and of me as someone trying to mangle around with the processes running on my computer. We talked about user IDs associated with the process IDs. So, for example, if I am a user with ID 4 trying to modify a process started by user with ID 5, I would not be authorized to do so and the operating system would not allow me to do so. I don’t think it’s important for you to know the details of how this is done exactly, but just to know that this is done so that there is some measure of security within the system. This may not matter much if you are the only user on your computer, but imagine if there are multiple users – then it starts to matter.
Let’s introduce some concepts. A process is your operating systems container of a running program. Every application you run has its own process. Every process has its own memory and must not access other processes memory.
That being said, each process has its own unique process ID (also known as PID). Each process can be uniquely identified via its process ID.
Each process has an user associated with it – the user which instantiated the process. That user has an ID, and that users ID is remembered. Why is it remembered? So that only the user who initiated the process can affect its execution – start it, stop it etc.
There are in fact more than one user IDs, but in reality, nothing bad will happen if you simplify it and treat every process as having only one user ID. (Ward, 2014)
Hope you learned something new!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 162-163
Moving on, we will learn about processes. We will talk about commands related to process monitoring and management and concepts relevant to truly understand process-related activities.
This is probably my favorite part, not only because it is fundamental to understanding the Linux operating system and operating systems in general, but because this was challenging to explain – to cull out what isn’t essential, but still give you the bigger picture. I hope that you will find it a joyful read.
You can customize the prompt in your command line. The prompt is the thing that is displayed left of your cursor when you first start a Terminal. My prompt is mislav@mislavovo-racunalo:workingDirectory, but it could be something else.
I won’t go into details here, because I don’t care about fancy – I care about day-to-day Linux usage, and customizing the prompt doesn’t fit that topic. You can find more about it here: (“How To Customize Bash Prompt In Linux,” n.d.) and (Shotts, 2019).
Let’s continue our discussion of vi and learn how to search for and replace strings (which are just consecutive characters) within a file, as well as how to manage multiple files. This is modeled after (Shotts, 2019).
First, let’s search for the word Money. To do that, type / (while in command mode (which is default)) and then type Money. Then press Enter (on your keyboard). That will find the first match. To go to the next match, press n.
Great. Let’s now talk about global search-and-replace. What does this “global” search-and-replace mean? It means, in essence, we are looking at the entire file. If this weren’t the case, then only the first occurrence of the string we are looking for in each line (if it exists) would be replaced and other occurrences on the same line would be skipped over.
Here is the syntax for global search-and-replace (I remember it by heart):
:%s/<originalString>/<replacementString>/g
Let’s break this down – : signifies the beginning of a command, % says “search everything from the first line to the last line”, s specifies the search-and-replace operation, originalString is the original string we are searching for and replacementString is the string we are replacing originalString with. g means global.
Let’s write the following command and then press Enter:
:%s/Money/Mustard/g
Now we have:
Mustard is how we transfer wealth
Some stuff Abba Mustard
Mustard Mustard
Mustard Mustard
It's the Mustard
In the Rich Man's World
~
…
A sidenote: If you wanted to confirm every single replacement (by typing y), then you would add c to the end of the above command, so it would be:
:%s/Money/Mustard/gc
Let’s now talk about how to manage multiple files. First, write your changes (:w). In order to open up another file within vi, type
:e <filename>
and press Enter. I have written :e ab.txt. ab.txt is in the same folder as aba.txt and contains the following lines:
AB
Ab
aB
ab
To follow along, feel free to create the ab.txt file via a text editor with a graphical user interface and then type :e ab.txt. vi will automatically switch to the newly opened file.
To switch between the two files (aba.txt and ab.txt) type :bn and press Enter. Running :bn once gets you to aba.txt.
Now let’s talk about how to copy contents from one file to another. I use the following procedure:
Place yourself in the file you want to copy the line from and press yy (to copy the line your cursor is on)
Type :bn to switch to the other file (and press Enter)
Type p or P depending on where you want to paste the line (p pastes it below the current line, P pastes it above the current line; remember to press Enter)
To insert an entire file in another file, do this:
Place your cursor on the desired location in the file you want to insert another file
Type in :r <fileToInsert> where <fileToInsert> is the filename which to insert
Press Enter
Let’s combine these two steps. Let’s first copy the line “Mustard is how we transfer wealth” from aba.txt to ab.txt, below the first line. Do it using the above instructions. Then you can switch to aba.txt. Remember to write the changes with :w – otherwise you get an error, or you can use the exclamation mark (as suggested by vi) to forcibly do change the file back to aba.txt.
Then, let’s copy the entire ab.txt file to aba.txt file at the end. Use the instructions above.
In the end, you get this in aba.txt:
Mustard is how we transfer wealth
Some stuff Abba Mustard
Mustard Mustard
Mustard Mustard
It's the Mustard
In the Rich Man's World
AB
Mustard is how we transfer wealth
Ab
aB
ab
~
…
Now, write :wq to write changes and quit vi at the same time and press Enter.
There you have it. You now know how to search, conduct global search-and-replace and manage multiple files in vi. This concludes our vi journey.
Thank you for reading!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 176-184
In this tutorial, we will be learning how to use the vi editor. As I said in one of the earlier posts, on some computers, there is no alternative other than to use vi. So you must know the basic stuff. Let’s see what the basics are. I modeled this after (Shotts, 2019), but I pruned it even more. This is intended as a follow-along guide – follow the instructions in this article and gain a basic understanding of vi.
I have a file called aba.txt. Let me first show you the contents of the file, so you can replicate it on your computer:
I could also open multiple files at the same time by typing something like:
vi file1.txt file2.txt
When I open up aba.txt with vi, I get something like this:
Abba Money
Money Money
It's the Money
In the Rich Man's World
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"aba.txt" 4 lines, 62 characters
The first thing to know about vi is how to quit it. To quit vi, type :q (with the colon in front) and press Enter (Enter on your keyboard). That will exit vi.
Let’s reopen vi.
vi aba.txt
OK. The first thing to realize about vi is that it has two modes – command mode and insert mode. By default, you are in command mode. That means that any characters you type are not going to be interpreted as symbols to be inserted, but rather as commands. Also, note that cursor movement is done in the command mode. You can move your cursor with the arrows on your keyboard. But don’t move your cursor just yet. To enter insert mode, press i on your keyboard. Do it now.
You can type in something. A word of caution: Backspace won’t be delete the characters you mistype. I typed in “Some stuff “. To exit insert mode, press ESC (escape on your keyboard).
My file looks like this now:
Some stuff Abba Money
Money Money
It's the Money
In the Rich Man's World
~
~
~
~
…
where … denotes the continuation of the file.
Since we pressed Escape, now we are in the command mode again. Now we have to write the changes to the file. To write changes we just made to the file, type in :w and press Enter.
Now let’s learn how to append to a file. While in command mode (and from this point on it is assumed you are in the command mode when a particular instruction is given for you to do, unless explicitly stated otherwise) type capital A (on your keyboard – I assume this from this point on). That will allow you to append after the line where the cursor is currently located and it automatically puts you in insert mode. If you haven’t moved your cursor around, you will append just below the first line. So let’s add the line “Money is how we transfer wealth”. Feel free to press Enter on your keyboard, and write “It is an IOU”. Then press Escape to exit the insert mode. Your file should look like this now:
Some stuff Abba Money
Money is how we transfer wealth
It is an IOU
Money Money
It's the Money
In the Rich Man's World
~
~
...
So A (capital A) appends to the file by adding lines after the line where the cursor is located.
Now let’s talk about inserting blank lines and undoing our changes. If you want to insert a blank line below the current line, press o (lowercase o). If you want to insert a blank line above the current line, press O (capital O). Note that both of these commands put you in insert mode, so you will need to exit them by pressing ESC. Press o now. Uh-oh. Let’s say you didn’t want this. First, exit the insert mode by pressing ESC. Now, press u (lowercase u). It will undo the change you did. Try it with both o and O and then undo the changes.
Now let’s learn how to delete characters. You do this by pressing x. Let’s do an exercise. First, place your cursor on the end of the line which says “It is an IOU”. Press x. Press x again. Press x until you delete the entire line. Note that no matter how much you press x, the line won’t “merge” with the previous line. That’s OK. We will cover how to do that later.
Your file should look like this:
Some stuff Abba Money
Money is how we transfer wealth
Money Money
It's the Money
In the Rich Man's World
~
...
Let us now discuss deleting stuff other than just characters. Here is a list on how to delete various chunks of text:
dd – deletes the current line
d$ – deletes from the current cursor location to the end of the current line
dG – deletes from the current line to the end of the file
Now, a revelation – the d command (the first letter in the above commands) doesn’t just delete text, it “cuts” text. You can paste it with p. Try placing your cursor on the line “Some stuff Abba Money”, type dd, then type p. That should cut and paste the line. But what just happened? Well, the line “Some stuff Abba Money” is below the line “Money is how we transfer wealth”. Why is that so? p pastes the line below the line where the cursor is, while P (capital P) pastes the line on the line above the cursor.
Now, let’s talk copying (also called yanking):
yy – copies the current line
y$ – copies from the current cursor location to the end of the current line
yG – copies from the current line to the end of the file
Try this: type yy while your cursor is on the line “Money Money”, then press P (capital P). To remind ourselves, this pastes the copied line above the line the cursor is currently in. Great! More money! We are rich!
Here is how our file looks like now:
Money is how we transfer wealth
Some stuff Abba Money
Money Money
Money Money
It's the Money
In the Rich Man's World
~
~
...
Now one last touch – let’s remove the blank line in the middle of our file. We do that by placing our cursor on that line and typing capital J.
Aaah! Perfect! Now let’s write the changes to our file. Type :w and press Enter. Then type :q and press Enter to quit vi. A caveat: if you type :w <newFilename> where <newFilename> is the new filename you want for your file, a new file will be created, but in vi you will still be editing the old file.
Voila! You now know the very basics of vi. Now, there are some things we didn’t cover, such as multiple files and search-and-replace, but we will cover that in the next article. These are the very basics. If you wanted to search and replace some word with some other word, you would have to do it manually and you would have to have separate Terminals for each of the files to be opened in vi. Then, you would manually search and replace. That is tedious, but, you have the basics.
Feel free to skip the next article, if you wanted to learn only the essentials of vi. The next article covers multiple files in vi, search and search-and-replace functionality in vi. It will make you a more efficient vi-er, but if you just wanted to know the very rudimentary basics, off you can go.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 165-176