He answereth and saith unto them, He that hath two coats, let him impart to him that hath none; and he that hath meat, let him do likewise.
Luke 3:11
Linux Operating System
The first version of Linux appeared more than thirty years ago. Over the years, this operating system has established itself as a leader in server use. I will share my experience with Linux by presenting useful commands.
After installing Linux, there is a super user called “root” by default. You need to set a password during the installation process. If you have not installed Linux and do not know the password, you can try simply by pressing “Enter” in the password field (if not set) or with the following default passwords:
root, toor, 1234.
There are two main files related to system user authentication: /etc/passwd and /etc/shadow. The User’s passwords are stored as hashed version in the files. They are hashed by the crypt function. There is no known mechanism to revert hashed password to the original value, even you know the key.
For the examples in this article, I used a Linux emulator written in JavaScript. You can safely practice the examples shown here at JS/Linux.
Who am I, who are the others, where am I?
The administrator
After successfully logging in, you can see the following line of text and a blinking cursor at the end. This is the command line interface or simply CLI. It is a program that allows users to type text commands instructing the computer to do specific tasks. Now you find out which user you are logged in as. You can check almost every command how it is used and what options (input parameters) it can accept by writing –help after the name.
Who am I or “whoami” to see myself
[root@localhost ~]# whoami root [root@localhost ~]# whoami --help BusyBox v1.24.2 (2018-08-19 13:59:49 CEST) multi-call binary. Usage: whoami Print the user name associated with the current effective user id [root@localhost ~]#
Root is the administrator of the entire system! Root has all privileges over the system. Root can create new users, folders and files. Root can remove them, can change permissions etc.
See who others are logged in the system “who”
The “root” user is logged in from console “:0” means. The user “user1” is logged in from external machine as you can see the IP address.
[root@localhost ~]# who root :0 2022-04-05 09:12 (:0) user1 pts/0 2022-04-05 08:10 (192.168.0.111)
You can do more with the command “who”. You can see when the system is booted “who -b”, how many users are logged in “who -q”.
[root@localhost ~]# who -b
system boot 2022-04-05 08:00
[root@localhost ~]# who -q
root user1
# users=2
Now you can find where are you.
The “hostname” command
[root@localhost ~]# hostname localhost
In order to be more accurate in the location, we need to find the directory in which we are.
Print working directory command “pwd”
[root@localhost ~]# pwd /root
Most of this information is stored in the prompt “[root@localhost ~]#”. The first part shows the current user “root”, the second part is the delimiter “@”, the third part is the host, the fourth part marked with a sign “~” means that you are in your own directory.
The standard users
Installing Linux creates a number of “Standard Users”, each with its own working directory. The user id (UID) is the user unique identification number and group id (GID) in this table is the primary group for the user.
| User | UID | GID | Home Directory | Shell |
|---|---|---|---|---|
| root | 0 | 0 | /root |
/bin/bash |
| bin | 1 | 1 | /bin |
/sbin/nologin |
| daemon | 2 | 2 | /sbin |
/sbin/nologin |
| adm | 3 | 4 | /var/adm |
/sbin/nologin |
| lp | 4 | 7 | /var/spool/lpd |
/sbin/nologin |
Sample of Standard Users
How to reveal all users using the /etc/passwd file?
There are at least several methods to open a file and I will present a simple method.
Linux has a group of commands classified as file content commands.
The “less” command belongs to content commands group
The “less” command allows user to view file (or stdin) one screenful at a time. This means that the command will display the contents of a file until the screen fills up, then it will stop and wait for the user to press a key to continue.
Syntax:
less [-OPTIONS] [FILE]…
Let’s open the file /etc/passwd using “less” command.
[root@localhost ~]# less /etc/passwd
root:x:0:0:root:/root:/bin/sh daemon:x:1:1:daemon:/usr/sbin:/bin/false bin:x:2:2:bin:/bin:/bin/false sys:x:3:3:sys:/dev:/bin/false sync:x:4:100:sync:/bin:/bin/sync mail:x:8:8:mail:/var/spool/mail:/bin/false www-data:x:33:33:www-data:/var/www:/bin/false operator:x:37:37:Operator:/var:/bin/false nobody:x:99:99:nobody:/home:/bin/false ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ /etc/passwd
However, the file consists of lines that are not enough to fill the entire screen and the “less” command completes the blank lines starting with the “~” sign. In this mode, the command expects a keystroke to be pressed or other (internal) command be entered. You can browse screen by screen (applies to large files) with the “pgup” and “pgdn” keys, jump to start/end of file with keys “home” and “end”, move trough the rows with the arrows, search for text by pressing “?”, type the searched text and press Enter. Exiting the “less” command mode is by pressing the “q” key.
Each line in the file has seven fields delimited by colons that contain the following information:
- The user name.
- Encrypted password (x means that the password is stored in the /etc/shadow file).
- User ID number (UID).
- User’s group ID number (GID).
- Full name of the user (GECOS).
- User home directory.
- Login shell (defaults to /bin/sh).
Our system has users – root, daemon, bin, sys, sync, mail, www-data, operator, nobody.
The lists
The useful command “ls”, short for the word “list”
ls lists the files in the current working directory. If another directory is specified, then ls will list the files there, and in fact the user may specify any list of files and directories to be listed.
[root@localhost ~]# ls bench.py hello.c hello.js readme.txt rv128test.bin
If the directory you work in has a large number of files, it will be difficult for you to find a specific one. There is a very convenient combination of the command “ls” with the key “Tab”. If I type “ls” and “h” and click “Tab” once, Linux will complete the line with the file name. If I press “Tab” a second time, the command will be executed only for these files starting with the letter “h”.
[root@localhost ~]# ls hello. hello.c hello.js
The “ls” command has many useful options that you can use. I will give an example of several of them.
ls -l (long format, displaying Unix file types, permissions, number of hard links, owner, group, size, last-modified date and filename);
ls -h (output the file sizes in human readable format);
ls -a (show all files).
I often use the three options in combination.
[root@localhost ~]# ls -lha total 60 drwxr-xr-x 5 root root 323 Sep 18 2019 . drwxrwxrwx 19 root root 457 Apr 17 15:49 .. -rw------- 1 root root 54 Sep 9 2018 .Xauthority drwx------ 2 root root 94 Sep 9 2018 .dillo drwx------ 3 root root 62 Sep 9 2018 .fltk drwx------ 2 root root 202 Sep 9 2018 .fluxbox -rw------- 1 root root 54 Sep 9 2018 .serverauth.46 -rwxr-xr-x 1 root root 28 Sep 9 2018 .xsession -rw-r--r-- 1 root root 113 Sep 9 2018 bench.py -rw-r--r-- 1 root root 185 Sep 9 2018 hello.c -rw-r--r-- 1 root root 22 Sep 18 2019 hello.js -rw-r--r-- 1 root root 238 Sep 18 2019 readme.txt -rw-r--r-- 1 root root 8.1K Sep 9 2018 rv128test.bin
“total” is the disk usage of the listed files in blocks (1024 bytes or 512 bytes).
There are several columns in the output data.
The first column represents in symbolic notation the type of the data piece written at the disk and its permissions.
– first character shows the type:
-
-
- “d” for a directory;
- “-” for a file;
- “l” for a special file type symbolic link, sometimes called a soft link. A symbolic link is a shortcut to another location in the file system;
- “p” for named pipes – special files that can exist anywhere in the file system. They can be created with the command “mkfifo”;
- “s” for a socket, which is a special file used for inter-process communication. It enables communication between two processes.
-
– next nine characters shows the permissions on the file or directory as follows:
-
-
- two to four, represent the rights granted to “owner”;
- five to seven, represent the rights granted to “owner group”;
- eight to ten represent the rights granted to “other users”, not owner nor in the group.
-
Permissions for files/directories are represented by the following letters:
-
-
- “r” refers to the read permission;
- “w” refers to the write permission;
- “x” refers to the execute permission.
-
The second column represents number of hard links (excluding symbolic links).
The third and the fourth columns represents owner name and owner group.
The fifth column shows file/directory size in bytes (Kilobytes, Megabytes, Gigabytes etc. if ls -h option is used).
The next three columns are the timestamp and represents time of last modification of the file/directory. A timestamp is considered to be recent if it is less than six months old, and is not dated in the future.
Notice that the parent directory is listed in recent form:
drwxrwxrwx 19 root root 457 Apr 17 15:49 ..
If a timestamp dated today is not listed in recent form, the timestamp is in the future, which means you probably have clock skew problems which may break programs like make that rely on file timestamps.
The last column is for file/directory name. Note that there are names as single dot and two dots. They represents the current and the parent directories. Also a single dot before the name means this file/directory is hidden from the conventional “ls” command. You can use “ls -a” to see hidden files/directories. Each filename can be separated by “.” and this dot separates the name and the file extension. In most cases the extension represents the file type. The file type means how the operating system treats this file. Can it be executed directly or need additional application to open it.
As you may have noticed, Linux command interpreter colors file and directory names in a different color. Here are the meanings of the different colors:
- White – File.
- Blue – Directory.
- Green – Executable or recognized data file (we will discuss this files later).
- Cyan (Sky Blue) – Symbolic link file.
- Yellow with black background – Device (mounted CD-ROM, Hard disk drives (HDD), Network-attached storages (NAS), Secure Digital card (SD card) or Solid-state Drives (SSD)) usually in /dev directory (Device files (e.g., /dev/null, /dev/disk0, /dev/sda1, /dev/tty, /dev/random)) or /mnt directory, which is called temporarily mounted filesystems.
- Magenta (Pink) – Graphic image file.
- Red – Archive file (we will discuss this files later).
- Red with black background – Broken link.
Let’s look at a “more serious” directory and pay attention to the colors of the names.
[root@localhost ~]# ls /etc -lha total 156 drwxr-xr-x 13 root root 808 Sep 9 2018 . drwxrwxrwx 19 root root 457 May 8 17:14 .. drwxr-xr-x 2 root root 450 Sep 9 2018 ImageMagick-7 drwxr-xr-x 4 root root 114 Sep 9 2018 X11 -rw-r--r-- 1 root root 1.1K Sep 9 2018 dhcpcd.conf -rw-r--r-- 1 root root 42 Sep 9 2018 dhcpcd.duid drwxr-xr-x 2 root root 132 Sep 9 2018 dillo lrwxrwxrwx 1 root root 17 Aug 19 2018 dropbear -> /var/run/dropbear drwxr-xr-x 3 root root 62 Sep 9 2018 fltk drwxr-xr-x 3 root root 118 Sep 9 2018 fonts -rw-r--r-- 1 root root 199 Sep 9 2018 fstab -rw-r--r-- 1 root root 318 Sep 9 2018 group -rw-r--r-- 1 root root 10 Sep 9 2018 hostname -rw-r--r-- 1 root root 40 Sep 9 2018 hosts drwxr-xr-x 2 root root 210 Sep 9 2018 init.d -rw-r--r-- 1 root root 1.0K Sep 9 2018 inittab -rw-r--r-- 1 root root 21 Sep 9 2018 issue drwxr-xr-x 2 root root 204 Aug 19 2018 joe -rw-r--r-- 1 root root 16.0K Sep 9 2018 ld.so.cache lrwxrwxrwx 1 root root 12 Jan 9 2017 mtab -> /proc/mounts drwxr-xr-x 6 root root 173 Aug 19 2018 network -rw-r--r-- 1 root root 230 Aug 19 2018 nsswitch.conf -rw-r--r-- 1 root root 116 Sep 9 2018 os-release -rw-r--r-- 1 root root 334 Jan 9 2017 passwd -rw-r--r-- 1 root root 379 May 24 2017 profile drwxr-xr-x 2 root root 62 Jan 9 2017 profile.d -rw-r--r-- 1 root root 2.7K Jan 9 2017 protocols lrwxrwxrwx 1 root root 18 Jan 9 2017 resolv.conf -> ../tmp/resolv.conf -rwxr-xr-x 1 root root 3.6K Aug 19 2018 screenrc -rw-r--r-- 1 root root 10.6K Jan 9 2017 services -rw------- 1 root root 243 Sep 9 2018 shadow -rw-r--r-- 1 root root 1.4K Aug 19 2018 slsh.rc drwxr-xr-x 6 root root 152 Aug 19 2018 ssl drwxr-xr-x 4 root root 110 Sep 9 2018 udev
The directory /etc contains host-specific system-wide static configuration files and may not contain binaries!
Let’s see again our (root user) directory.
[root@localhost ~]# ls -lha total 60 drwxr-xr-x 5 root root 323 Sep 18 2019 . drwxrwxrwx 19 root root 457 Apr 17 15:49 .. -rw------- 1 root root 54 Sep 9 2018 .Xauthority drwx------ 2 root root 94 Sep 9 2018 .dillo drwx------ 3 root root 62 Sep 9 2018 .fltk drwx------ 2 root root 202 Sep 9 2018 .fluxbox -rw------- 1 root root 54 Sep 9 2018 .serverauth.46 -rwxr-xr-x 1 root root 28 Sep 9 2018 .xsession -rw-r--r-- 1 root root 113 Sep 9 2018 bench.py -rw-r--r-- 1 root root 185 Sep 9 2018 hello.c -rw-r--r-- 1 root root 22 Sep 18 2019 hello.js -rw-r--r-- 1 root root 238 Sep 18 2019 readme.txt -rw-r--r-- 1 root root 8.1K Sep 9 2018 rv128test.bin
The following file extensions meanings:
- py – Python programming language.
- c – C programming language.
- js – JavaScript programming language.
- txt – Regular text document.
- bin – Binary file.
Renaming, hiding, unhiding and moving files with the “mv” command
The “mv” command (move) is used to move or rename files. The syntax for the “mv” command is:
Usage: mv [-fin] SOURCE DEST
or: mv [-fin] SOURCE… DIRECTORY
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY
-f Don’t prompt before overwriting;
-i Interactive, prompt before overwrite;
-n Don’t overwrite an existing file.
To change the name of the file readme.txt to readme.text we need to use the command in this order:
[root@localhost ~]# mv readme.txt readme.text [root@localhost ~]# ls -lh total 28 -rw-r--r-- 1 root root 113 Sep 9 2018 bench.py -rw-r--r-- 1 root root 185 Sep 9 2018 hello.c -rw-r--r-- 1 root root 22 Sep 18 2019 hello.js -rw-r--r-- 1 root root 238 Sep 18 2019 readme.text -rw-r--r-- 1 root root 8.1K Sep 9 2018 rv128test.bin
In the same way we can move a file, just select another directory and type the same file name. For example, move bench.py to the root directory “/”.
[root@localhost ~]# mv bench.py /bench.py [root@localhost ~]# ls / bench.py dev home lib64 media opt root sbin tmp var bin etc lib linuxrc mnt proc run sys usr
Let’s get it back.
[root@localhost ~]# mv /bench.py bench.py [root@localhost ~]# ls bench.py hello.c hello.js readme.text rv128test.bin
Hiding and unhiding is easy-peasy, just add or remove dot at the beginning of the file.
[root@localhost ~]# mv bench.py .bench.py [root@localhost ~]# ls hello.c hello.js readme.text rv128test.bin [root@localhost ~]# mv .bench.py bench.py [root@localhost ~]# ls bench.py hello.c hello.js readme.text rv128test.bin
There is a Filesystem Hierarchy Standard (FHS). The FHS defines the structure of the file system and stands that all files and directories appear under the root directory “/” (do not mess with “~” directory). If the data is stored on physically remote device (NAS or virtual device or even a filesystem on remote server) it follows the same standard. The root directory “/” also called main directory consist of many files and subdirectories. The directory responsible for the system devices is /dev directory. The “/dev” directory is called a Standard mount point. The “/dev” directory consists of files that represent devices that are attached to the local system. These are not regular files that a user can change. These files are special devices files.
The standard input and output (stdin and stdout)
The standard input is a stream (first from the three standard streams around a computer program and its environment) from which a program reads its input data. The program requests data transfers by use of the read operation. Not all programs require stream input. For example, ls command (which display file names contained in a directory) may take command-line arguments, but perform their operations without any stream data input. Originally Input and Output operations happened via a physically connected system console (input via keyboard, output via monitor), but standard streams abstract this.
The cat – a commonly used command for working with standard input/output streams
Cat is short for concatenate. With this command we can display the contents of one or more files on the screen or save the entered characters from the standard input stream (the keyboard) to a file. It takes the output from one command and feeds it to the next as input. The combined use of the “cat” command with the following operators and special characters provides great convenience for manipulating flows.
- input flow redirection “<” operator;
- output flow redirection “>” operator;
- pipe “|” operator, which chains commands together;
- single wildcard “?” special character, which means “any single character at this place”;
- sequence wildcard “*” special character, which means “any sequence of characters, including no characters from this place”.
The syntax of “cat” command is simple:
cat [OPTION] [FILE]
Let’s see the contents of the file readme.text.
[root@localhost ~]# cat readme.text Some tests: - Compile hello.c: gcc hello.c -o hello ./hello - Compute 1000 digits of pi: tinypi 1000 pi.txt cat pi.txt - Run the 128 bit version of riscvemu: riscvemu128 -m 16 rv128test.bin - Run QuickJS: qjs hello.js
Let’s call “cat” without a file name. Then write some text and press enter. You can see that each keyboard input (the standard input) is displayed on the screen (the standard output). To break input press “ctrl + c”.
[root@localhost ~]# cat Hello World Hello World Test Test ^C
Using “cat” in this way does not make much sense. Let’s create our own file with the name “hello.txt” and content “Hello World”. Call the command “cat” together with the output flow redirection operator “>” and type the file name “hello.txt” and press Enter. After that input prompt opens and you can type “Hello World” and press Enter. Now press ctrl + d to save file and exit. Executing this way, “cat” redirects the input to a file.
[root@localhost ~]# cat > hello.txt Hello World
Let’s see the result.
[root@localhost ~]# ls bench.py hello.c hello.js hello.txt readme.text rv128test.bin [root@localhost ~]# cat hello.txt Hello World
We can use “cat” to display contents of multiple files by using repeated sequence of file names or in this case using “*” wildcard special character.
[root@localhost ~]# cat hello.c hello.js hello.txt
/* This C source can be compiled with:
gcc -o hello hello.c
*/
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello World\n");
return 0;
}
print("Hello World");
Hello World
[root@localhost ~]# cat hello.*
/* This C source can be compiled with:
gcc -o hello hello.c
*/
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello World\n");
return 0;
}
print("Hello World");
Hello World
How to append an additional content to the end of a file.
Use “cat” command, output redirection operator twice “>>” and the file name. Type your text, press Enter and ctrl + d.
[root@localhost ~]# cat >> hello.txt Today is a beautiful day
How to merge several files into one file using “cat” command.
[root@localhost ~]# cat hello.c hello.js hello.txt > hello_merged.txt
[root@localhost ~]# cat hello_merged.txt
/* This C source can be compiled with:
gcc -o hello hello.c
*/
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello World\n");
return 0;
}
print("Hello World");
Hello World
Today is a beautiful day
Let’s look at another similar command to view the contents of a file.
The “tail” command, as the name suggests this command displays the contents of a file from the tail (end of file)
By default, the last ten lines are displayed. Let’s test with our file hello_merged.txt, outputting only the last two lines.
[root@localhost ~]# tail -n 2 hello_merged.txt Hello World Today is a beautiful day
The -n option specifies the number of lines to be displayed on the screen.
Here are some more interesting options:
-f Print data as file grows;
-c [+]N[kbm] Print last N bytes;
-n +N[kbm] Start on Nth line and print the rest;
-s SECONDS Wait SECONDS between reads with -f.
Let’s see the last four bytes of the file. We assume that one character is encoded with one byte!
[root@localhost ~]# tail -c 4 hello_merged.txt day [root@localhost ~]#
Do you think something went wrong?!
We set the parameter to display the last four characters (bytes), and we got a “day” of only three characters.
In fact, everything is fine. Remember when you wrote “Today is a beautiful day” and pressed Enter. Enter actually adds another byte, that for a newline. Let’s run the same command again, showing only one byte.
[root@localhost ~]# tail -c 1 hello_merged.txt [root@localhost ~]#
Another advantage of the “tail” command is that it can be combined with many other Linux commands. This process is done by piping, as mentioned above. The use of piping is achieved with the symbol “|”. We will combine “tail” with the “sort” command.
The “sort” command sorts a file in a specific order
Here are some options for the “sort” command:
[root@localhost ~]# tail -n 2 hello_merged.txt | sort -r Today is a beautiful day Hello World
[root@localhost ~]# tail -n 2 hello_merged.txt | sort -r -z Hello World Today is a beautiful day
After reviewing the capabilities of the “cat” command, we can delete the file hello_merged.txt. We will use the command “rm”, short for remove. It is used to remove regular files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets.
[root@localhost ~]# rm hello_merged.txt [root@localhost ~]# ls bench.py hello.c hello.js hello.txt readme.text rv128test.bin
Some tricks
I will now share with you several tricks that system administrators often use.
Many events occur during the operation of each operating system. Starting the kernel, starting/stopping processes, starting applications, creating or deleting files, records in databases, etc. Important information about these activities is recorded in logs. In case of an unexpected event (process blocking, loss of files or records in the database or even unauthorized intrusion), the system administrator can immediately review the log files to determine the cause of this event.
The Linux operating system has a special folder for recording changes that have occurred during operation.
The directory /var/log
/var/log/boot.log is a repository of all information related to booting and any messages logged during startup.
/var/log/syslog or /var/log/messages: records for system-related information. This log stores all activity data across the global system.
/var/log/auth.log or /var/log/secure: store authentication logs, including both successful and failed logins and authentication methods.
There are other sub directories of /log but I will not dwell on them.
Due to the fact that for the examples in this article I use a Linux emulator, there are no logs in all system.
The very useful “find” command
Trick one – quickly find old files
We will use the “find” command to find in the entire file system instead of log files for text files older than thirty days.
[root@localhost ~]# find / -name "*.txt" -mtime +30 /usr/lib/perl5/5.22.2/Unicode/Collate/allkeys.txt /usr/lib/perl5/5.22.2/Unicode/Collate/keys.txt /usr/lib/perl5/5.22.2/unicore/SpecialCasing.txt /usr/lib/perl5/5.22.2/unicore/Blocks.txt /usr/lib/perl5/5.22.2/unicore/NamedSequences.txt /usr/lib/python2.7/LICENSE.txt /usr/lib/xorg/protocol.txt /usr/share/X11/Xcms.txt
Let’s review the Python language license agreement.
[root@localhost ~]# cat /usr/lib/python2.7/LICENSE.txt A. HISTORY OF THE SOFTWARE ========================== Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands as a successor of a language called ABC. Guido remains Python's principal author, although it includes many contributions from others. In 1995, Guido continued his work on Python at the Corporation for National Research Initiatives (CNRI, see http://www.cnri.reston.va.us) in Reston, Virginia where he released several versions of the software. In May 2000, Guido and the Python core development team... .......................................................... ...By clicking on the "ACCEPT" button where indicated, or by copying, installing or otherwise using Python 1.6.1, Licensee agrees to be bound by the terms and conditions of this License Agreement. ACCEPT CWI LICENSE AGREEMENT FOR PYTHON 0.9.0 THROUGH 1.2 -------------------------------------------------- Copyright (c) 1991 - 1995, Stichting Mathematisch Centrum Amsterdam, The Netherlands. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
It’s actually a lot longer but I cut it short… who likes to read license agreements!
Let’s just look for a specific word in this license agreement. For example, the word “software” came to my attention several times while reviewing the text.
The other very useful command “grep”
Trick two – quickly find a string in a file
The “grep” command is used to search text and strings in a given file. The command searches the given file for lines containing a match to the given strings or words.
The name, “grep”, derives from the command used to perform a similar operation, using the Unix/Linux text editor ed:g/re/pThe grep utilities are a family that includes grep, egrep, and fgrep for searching files. For most use cases, fgrep is sufficient due to speed and only looking into strings and words.
Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN…/-f FILE [FILE]…
[root@localhost ~]# grep -i -n "software" /usr/lib/python2.7/LICENSE.txt
1:A. HISTORY OF THE SOFTWARE
12:software.
17:Corporation, see http://www.zope.com). In 2001, the Python Software
49: other software that is released under the GPL; the others don't.
63:PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
66:1. This LICENSE AGREEMENT is between the Python Software Foundation
68:otherwise using this software ("Python") in source or binary form and
77:2011, 2012, 2013, 2014, 2015 Python Software Foundation; All Rights Reserved"
120:this software in source or binary form and its associated
121:documentation ("the Software").
127:otherwise use the Software alone or in any derivative version,
129:Software, alone or in any derivative version prepared by Licensee.
131:3. BeOpen is making the Software available to Licensee on an "AS IS"
135:FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE WILL NOT
139:SOFTWARE FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS
140:AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY
157:7. By copying, installing or otherwise using the software, Licensee
168:("Licensee") accessing and otherwise using Python 1.6.1 software in
239:Permission to use, copy, modify, and distribute this software and its
245:distribution of the software without specific, written prior
249:THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
254:OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Only lines that contain the word “software”, whether uppercase or lowercase, are displayed.
I added two options to the command:
-n Add ‘line_no:’ prefix
-i Ignore case
Let’s assume that this licensing agreement does not satisfy us and we will write our own. The easiest way is to delete the file, but we will write a new one, we must at least remember the location of the file.
Trick three – еxtract important information from a large file to a small file
If you have to view or edit a large text or log file through the less command or the Vim editor, you are very likely to have difficulty opening the file. For example, the CPU reaches a high load level and Linux will “kill” the process. Downloading the file is also not an appropriate solution because it can lead to delay (some log files can be Gigabytes). The solution is to use the available Linux resources.
You can use the “grep” command and the “>” data redirection symbol.
Redirection is the capability to redirect the STDOUT data stream of a program to a file instead of to the default target of the display. The “greater than” ( > ) character, aka “gt”, is the syntactical symbol for redirection of STDOUT.
You have a large file /usr/lib/python2.7/LICENSE.txt and want to fetch data for software related term.
[root@localhost ~]# grep -i "software" /usr/lib/python2.7/LICENSE.txt > software_information.txt
Wait until data is fetched and file is created. Now you can open your new file.
[root@localhost ~]# less software_information.txt
press “q” to exit less command tool.
Trick four – quickly clear the contents of a file
As mentioned above, Linux has an output flow redirection “>” operator. The file is opened for writing by the shell. Opening the file for writing truncates it. The stream redirection operator > empties /usr/lib/python2.7/LICENSE.txt but we also do not have left operand to fulfill the file.
[root@localhost ~]# > /usr/lib/python2.7/LICENSE.txt [root@localhost ~]# cat /usr/lib/python2.7/LICENSE.txt [root@localhost ~]#
It is the same as echo ” > /usr/lib/python2.7/LICENSE.txt
The “echo” command produces an empty string. The reason why omitting the “echo” command produces the same result.
Let’s also check the file size.
[root@localhost ~]# ls /usr/lib/python2.7/LICENSE.txt -lh -rw-r--r-- 1 root root 0 May 22 18:38 /usr/lib/python2.7/LICENSE.txt [root@localhost ~]#
Naturally zero bytes.
Trick five – bulk delete old files
Let’s see all the text files older than thirty days again.
[root@localhost ~]# find / -name "*.txt" -mtime +30 /usr/lib/perl5/5.22.2/Unicode/Collate/allkeys.txt /usr/lib/perl5/5.22.2/Unicode/Collate/keys.txt /usr/lib/perl5/5.22.2/unicore/SpecialCasing.txt /usr/lib/perl5/5.22.2/unicore/Blocks.txt /usr/lib/perl5/5.22.2/unicore/NamedSequences.txt /usr/lib/python2.7/LICENSE.txt /usr/lib/xorg/protocol.txt /usr/share/X11/Xcms.txt
We will delete all text files in the perl5 directory and its subdirectories, because the “find” command is recursive by default. Use the “-maxdepth” option to control how deep into your directories you want the command searches. Be careful when using find to delete files!
Pearl is a programming language and we will not use it in this article, so I hope to work without its text files.
[root@localhost ~]# find /usr/lib/perl5 -type f -name "*.txt" -mtime +30 -delete
Our emulator version of the “find” command does not support the delete action and these files will remain.
The permissions
As I mentioned above, the administrator has rights over the entire system, including changing the access rights to the files.
The command to change permissions is “chmode”, short for “change mode”
First you need to understand the “chmode” syntax.
-
- u: User, meaning the owner of the file.
- g: Group, meaning members of the group the file belongs to.
- o: Others, meaning people not governed by the
uandgpermissions. - a: All, meaning all of the above.
- –: Minus sign. Removes the permission.
- +: Plus sign. Grants the permission.
- =: Equals sign. Set a permission and remove others.
Let’s add write permissions for the group of the file readme.text.
[root@localhost ~]# chmod g=w readme.text [root@localhost ~]# ls re* -lha -rw--w-r-- 1 root root 238 Sep 18 2019 readme.text
As you can see, the permissions for the group have changed. Each of the root group can save to the file.
I note a useful opportunity to use the asterisk. Putting an asterisk “*” somewhere in the name means “any sequence of characters, including no characters” in that place.
By this way you can set permissions for multiple files. Let’s enable all users to run our files named hello.
[root@localhost ~]# chmod o+x hello.* [root@localhost ~]# ls he*.* -lha -rw-r--r-x 1 root root 185 Sep 9 2018 hello.c -rw-r--r-x 1 root root 22 Sep 18 2019 hello.js
There is another traditional Linux/Unix notation to represent permissions. It is called numeric notation.
| Symbolic notation | Numeric notation | English |
|---|---|---|
---------- |
0000 | no permissions |
-rwx------ |
0700 | read, write, & execute only for owner |
-rwxrwx--- |
0770 | read, write, & execute for owner and group |
-rwxrwxrwx |
0777 | read, write, & execute for owner, group and others |
---x--x--x |
0111 | execute |
--w--w--w- |
0222 | write |
--wx-wx-wx |
0333 | write & execute |
-r--r--r-- |
0444 | read |
-r-xr-xr-x |
0555 | read & execute |
-rw-rw-rw- |
0666 | read & write |
-rwxr----- |
0740 | owner can read, write, & execute; group can only read; others have no permissions |
Examples from the symbolic notation section given in octal notation
Moving around
You can move trough the directories using the “cd” command, which stands for “change directory”
Now we move to the parent directory, to directory “var” and after that to the main directory.
[root@localhost ~]# cd .. [root@localhost ]# cd var [root@localhost var]# ls cache db lib lock log run spool tmp www [root@localhost var]# cd / [root@localhost ]# ls bin etc lib linuxrc mnt proc run sys usr dev home lib64 media opt root sbin tmp var
In Linux, the file structure has a tree structure, based on the main directory “/” in the bottom.
The entire file system is located on media (HDD, SSD, SD cards, etc.) that have a certain capacity. If the free space on the media is significantly reduced, this will complicate the operation of the operating system.
The “df” command stands for “disk filesystem”, it is used to get a full summary of available and used disk space usage of the file system on the Linux system
Conventional use of “df” will extract information about the used space in blocks (as noted above for the ls command), which is not very easy to understand.
[root@localhost ]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/root 1048576 215568 833008 21% / devtmpfs 125948 0 125948 0% /dev tmpfs 125988 8 125980 0% /run
Let’s use “df” with the human-readable format option. The designations are K – Kilobytes, M – Megabytes, G – Gigabytes, etc.
[root@localhost ]# df -h Filesystem Sizе Used Available Use% Mounted on /dev/root 1.0G 210.5M 813.5M 21% / devtmpfs 123.0M 0 123.0M 0% /dev tmpfs 123.0M 8.0K 123.0M 0% /run
Let’s see the possibilities.
[root@localhost ]# df --help BusyBox v1.24.2 (2018-08-19 13:59:49 CEST) multi-call binary. Usage: df [-PkmhT] [FILESYSTEM]... Print filesystem usage statistics -P POSIX output format -k 1024-byte blocks (default) -m 1M-byte blocks -h Human readable (e.g. 1K 243M 2G) -T Print filesystem type
Let’s go back to our own directory and do some tests. We will create a new “test” directory, enter it and run the built-in tool for calculating the value of the number Pi with arbitrary accuracy. We saw it above in the contents of the file readme.text.
[root@localhost ]# cd ~ [root@localhost ~]# mkdir -m777 test [root@localhost ~]# ls -lha total 64 drwxr-xr-x 6 root root 344 Sep 18 2019 . drwxrwxrwx 19 root root 457 May 2 13:24 .. -rw------- 1 root root 54 Sep 9 2018 .Xauthority drwx------ 2 root root 94 Sep 9 2018 .dillo drwx------ 3 root root 62 Sep 9 2018 .fltk drwx------ 2 root root 202 Sep 9 2018 .fluxbox -rw------- 1 root root 54 Sep 9 2018 .serverauth.46 -rwxr-xr-x 1 root root 28 Sep 9 2018 .xsession -rw-r--r-- 1 root root 113 Sep 9 2018 bench.py -rw-r--r-- 1 root root 185 Sep 9 2018 hello.c -rw-r--r-- 1 root root 22 Sep 18 2019 hello.js -rw-r--r-- 1 root root 238 Sep 18 2019 readme.text -rw-r--r-- 1 root root 8.1K Sep 9 2018 rv128test.bin drwxrwxrwx 2 root root 37 May 2 14:04 test
Our test directory has been created and everyone has the permissions to read, write and execute files in it.
To make a directory you can use the command “mkdir” which stands for “make directory”
With “mkdir”, you can also set permissions and create many directories at once. To set permissions, add flag “-m” and the desired permissions after it. The syntax of “-m” (mode) is the same as with the “chmod” command. To make multiple directories, use the curly brackets {} with “mkdir” and put the directory names, separated by a comma.
Let’s go to the directory and start the tool for calculating Pi with different lengths of the fractional part. We will save the result in the sequential files, will add a new line at the end of each file and then will concatenate them into a new file Pi.txt.
[root@localhost test]# tinypi 10 Pi10.txt [root@localhost test]# tinypi 100 Pi100.txt [root@localhost test]# tinypi 1000 Pi1000.txt [root@localhost test]# tinypi 10000 Pi10000.txt [root@localhost test]# echo -e "\n" | tee -a Pi10.txt Pi100.txt Pi1000.txt Pi10000.txt
We used the echo command in combination with the tee command attached to several files at once. Combining several actions at once is one of the great advantages of Linux over other operating systems.
The “echo” command is used to display line of text/string or a backslash-escaped characters (as we used “\n” and these are special characters defined in ANSI C standard) that are passed as an argument .
This command is mostly used in shell scripts and batch files to output status text to the screen or a file. We used it to display a new line and the option “-e” means to expand escape sequences.
The “tee” command reads from the standard input and writes to both standard output and one or more files at the same time.
The option “-a” means append data to the file. Note that if using in multi file mode, you must put spaces between the file names!
We used “tee” in combination with the command “echo” through piping.
The next test will be to collect all the data in one file. We will use the “cat” command again.
[root@localhost test]# cat Pi10.txt Pi100.txt Pi1000.txt Pi10000.txt > Pi.txt [root@localhost test]# ls -lha total 44 drwxrwxrwx 2 root root 166 May 2 15:43 . drwxr-xr-x 6 root root 344 Sep 18 2019 .. -rw-r--r-- 1 root root 10.9K May 2 16:22 Pi.txt -rw-r--r-- 1 root root 54 May 2 16:20 Pi10.txt -rw-r--r-- 1 root root 104 May 2 16:20 Pi100.txt -rw-r--r-- 1 root root 1004 May 2 16:20 Pi1000.txt -rw-r--r-- 1 root root 9.8K May 2 16:20 Pi10000.txt
Let’s look at the new file Pi.txt.
[root@localhost test]# cat Pi.txt 3.14159265358979323846264338327950288419716939937510 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317 253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482 133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530 921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705 392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420 199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619 311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111 959092164201989 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559 64462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001 13305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907 02179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034 41815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349 04287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752 83479131515574857242454150695950829533116861727855889075098381754637464939319255060400927701671139009848824012858361603563707660104710181942955596198946767837449448255379774726847104 04753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570 67498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501 41441973568548161361157352552133475741849468438523323907394143334547762416862518983569485562099219222184272550254256887671790494601653466804988627232791786085784383827967976681454100 95388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009 94657640789512694683983525957098258226205224894077267194782684826014769909026401363944374553050682034962524517493996514314298091906592509372216964615157098583874105978859597729754989 30161753928468138268683868942774155991855925245953959431049972524680845987273644695848653836736222626099124608051243884390451244136549762780797715691435997700129616089441694868555848 40635342207222582848864815845602850601684273945226746767889525213852254995466672782398645659611635488623057745649803559363456817432411251507606947945109659609402522887971089314566913 68672287489405601015033086179286809208747609178249385890097149096759852613655497818931297848216829989487226588048575640142704775551323796414515237462343645428584447952658678210511413 54735739523113427166102135969536231442952484937187110145765403590279934403742007310578539062198387447808478489683321445713868751943506430218453191048481005370614680674919278191197939 95206141966342875444064374512371819217999839101591956181467514269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672218256259966 15014215030680384477345492026054146659252014974428507325186660021324340881907104863317346496514539057962685610055081066587969981635747363840525714591028970641401109712062804390397595 15677157700420337869936007230558763176359421873125147120532928191826186125867321579198414848829164470609575270695722091756711672291098169091528017350671274858322287183520935396572512 10835791513698820914442100675103346711031412671113699086585163983150197016515116851714376576183515565088490998985998238734552833163550764791853589322618548963213293308985706420467525 90709154814165498594616371802709819943099244889575712828905923233260972997120844335732654893823911932597463667305836041428138830320382490375898524374417029132765618093773444030707469 21120191302033038019762110110044929321516084244485963766983895228684783123552658213144957685726243344189303968642624341077322697802807318915441101044682325271620105265227211166039666 55730925471105578537634668206531098965269186205647693125705863566201855810072936065987648611791045334885034611365768675324944166803962657978771855608455296541266540853061434443185867 69751456614068007002378776591344017127494704205622305389945613140711270004078547332699390814546646458807972708266830634328587856983052358089330657574067954571637752542021149557615814 00250126228594130216471550979259230990796547376125517656751357517829666454779174501129961489030463994713296210734043751895735961458901938971311179042978285647503203198691514028708085 99048010941214722131794764777262241425485454033215718530614228813758504306332175182979866223717215916077166925474873898665494945011465406284336639379003976926567214638530673609657120 91807638327166416274888800786925602902284721040317211860820419000422966171196377921337575114959501566049631862947265473642523081770367515906735023507283540567040386743513622224771589 15049530984448933309634087807693259939780541934144737744184263129860809988868741326047215695162396586457302163159819319516735381297416772947867242292465436680098067692823828068996400 48243540370141631496589794092432378969070697794223625082216889573837986230015937764716512289357860158816175578297352334460428151262720373431465319777741603199066554187639792933441952 15413418994854447345673831624993419131814809277771038638773431772075456545322077709212019051660962804909263601975988281613323166636528619326686336062735676303544776280350450777235547 10585954870279081435624014517180624643626794561275318134078330336254232783944975382437205835311477119926063813346776879695970309833913077109870408591337464144282277263465947047458784 77872019277152807317679077071572134447306057007334924369311383504931631284042512192565179806941135280131470130478164378851852909285452011658393419656213491434159562586586557055269049 65209858033850722426482939728584783163057777560688876446248246857926039535277348030480290058760758251047470916439613626760449256274204208320856611906254543372131535958450687724602901 61876679524061634252257719542916299193064553779914037340432875262888963995879475729174642635745525407909145135711136941091193932519107602082520261879853188770584297259167781314969900 90192116971737278476847268608490033770242429165130050051683233643503895170298939223345172201381280696501178440874519601212285993716231301711444846409038906449544400619869075485160263 27505298349187407866808818338510228334508504860825039302133219715518430635455007668282949304137765527939751754613953984683393638304746119966538581538420568533862186725233402830871123 28278921250771262946322956398989893582116745627010218356462201349671518819097303811980049734072396103685406643193950979019069963955245300545058068550195673022921913933918568034490398 20595510022635353619204199474553859381023439554495977837790237421617271117236434354394782218185286240851400666044332588856986705431547069657474585503323233421073015459405165537906866 27333799585115625784322988273723198987571415957811196358330059408730681216028764962867446047746491599505497374256269010490377819868359381465741268049256487985561453723478673303904688 38343634655379498641927056387293174872332083760112302991136793862708943879936201629515413371424892830722012690147546684765357616477379467520049075715552781965362132392640616013635815 59074220202031872776052772190055614842555187925303435139844253223415762336106425063904975008656271095359194658975141310348227693062474353632569160781547818115284366795706110861533150 44521274739245449454236828860613408414863776700961207151249140430272538607648236341433462351897576645216413767969031495019108575984423919862916421939949072362346468441173940326591840 44378051333894525742399508296591228508555821572503107125701266830240292952522011872676756220415420516184163484756516999811614101002996078386909291603028840026910414079288621507842451 67090870006992821206604183718065355672525325675328612910424877618258297651579598470356222629348600341587229805349896502262917487882027342092222453398562647669149055628425039127577102 84027998066365825488926488025456610172967026640765590429099456815065265305371829412703369313785178609040708667114965583434347693385781711386455873678123014587687126603489139095620099 39361031029161615288138437909904231747336394804575931493140529763475748119356709110137751721008031559024853090669203767192203322909433467685142214477379393751703443661991040337511173 54719185504644902636551281622882446257591633303910722538374218214088350865739177150968288747826569959957449066175834413752239709683408005355984917541738188399944697486762655165827658 48358845314277568790029095170283529716344562129640435231176006651012412006597558512761785838292041974844236080071930457618932349229279650198751872127267507981255470958904556357921221 03334669749923563025494780249011419521238281530911407907386025152274299581807247162591668545133312394804947079119153267343028244186041426363954800044800267049624820179289647669758318 32713142517029692348896276684403232609275249603579964692565049368183609003238092934595889706953653494060340216654437558900456328822505452556405644824651518754711962184439658253375438 85690941130315095261793780029741207665147939425902989695946995565761218656196733786236256125216320862869222103274889218654364802296780705765615144632046927906821207388377814233562823 60896320806822246801224826117718589638140918390367367222088832151375560037279839400415297002878307667094447456013455641725437090697939612257142989467154357846878861444581231459357198 49225284716050492212424701412147805734551050080190869960330276347870810817545011930714122339086639383395294257869050764310063835198343893415961318543475464955697810382930971646514384 07007073604112373599843452251610507027056235266012764848308407611830130527932054274628654036036745328651057065874882256981579367897669742205750596834408697350201410206723585020072452 256326513410559240190274216248439140359989535394590944070469120914093870012645600162374288021092764579310657922955249887275846101264836999892256959688159205600101655256375678
Speaking of the number Pi, here is a project that is interesting from a mathematical and computer point of view. Finding the 100-trillionth digit of Pi.
It looks a little scary, doesn’t it?! It is good that we have divided it into paragraphs to distinguish the individual calculations for Pi. If we try to look for a sequence of numbers now, it will be difficult to find it.
Don’t worry, the “wc” command comes to the rescue!
Counting the words in a file
The “wc” command (short for word count) count lines, words, and bytes for each FILE (or stdin).
Usage: wc [-clwL] [FILE]…
There are just several options:
Let’s see the practical work of wc on our file with the calculations of Pi.
[root@localhost test]# wc Pi.txt 8 4 11166 Pi.txt
It is used to find out number of lines, word count, byte and characters count in the files specified in the [FILE] arguments.
By default the “wc” command displays four-columnar output with the following values:
the number of lines the number of words the number of characters the file name
In fact, the Pi.txt file contains eight lines (\n characters), four words (each Pi value is one line and continuous), and eleven thousand one hundred and sixty-six characters. We know that the last word is the longest, but let’s do some calculations and check:
3. two characters;
10000 characters (digits) after the decimal point;
2 + 10000 = 10002 characters.
[root@localhost test]# wc Pi.txt -L 10002 Pi.txt
Save disk space by compressing files
Saving and optimizing files is a very important issue nowadays. Mankind is constantly spewing information – servers, desktops and laptops, smartphones… now even light bulbs have a processor and memory.
Let’s do some simple calculations for the occupied space from the files in the test directory.
[root@localhost test]# ls -l total 36 -rw-r--r-- 1 root root 11166 May 29 15:01 Pi.txt -rw-r--r-- 1 root root 54 May 29 15:00 Pi10.txt -rw-r--r-- 1 root root 104 May 29 15:00 Pi100.txt -rw-r--r-- 1 root root 1004 May 29 15:00 Pi1000.txt -rw-r--r-- 1 root root 10004 May 29 15:00 Pi10000.txt
11166 + 54 + 104 + 1004 + 10004 = 22332 Bytes used for all files. Which is 22332 Bytes = 22.332 KB (in decimal notation).
We can combine all the files into one and reduce its size by compressing the data. Archiving is the process of compressing the contents of a file using a specific algorithm. Each algorithm is evaluated according to characteristics such as complexity, degree of compression, speed of action, etc. Of course, every backup program must also offer the reverse unzip function.
The command “tar”, a tool for packing and unpacking files
This useful tool has a capabilities for create, extract, or list files from a tar file.
Usage: tar -[cxthvO] [-X FILE] [-T FILE] [-f TARFILE] [-C DIR] [FILE]…
[root@localhost test]# tar -cvf test_files.tar *.* Pi.txt Pi10.txt Pi100.txt Pi1000.txt Pi10000.txt
Now let’s unite and compress all the files into one file. To create a compressed gzip archive file we use the option “z” which stand for Gnu Zipped Archive. An archive file with an extension .gz is compressed by the standard GNU zip (gzip) compression algorithm. It may contains a single compressed or multiple compressed files. Gnu Zipped Archive is primarily used on Unix/Linux operating systems for file compression.
Example for Gnu Zipped Archive tar -cvzf test_files.tar.gz *.*
The bz2 feature takes more time to create an archive but compresses better than gzip archive. To create a highly compressed tar file use the option j.
Example for bz2 archive tar -cvjf test_files.tar.bz2 *.*
Executing files in Linux
As I mentioned above, Linux executables are highlighted in green. The permission “x” is set on the corresponding file. The execution permission can be granted to the owner, the group and the others.
In order to be executed directly by the operating system, the file must also be in a suitable (executable) form. Let’s look at the example with the hello.c file written in the C programming language.
[root@localhost test]# cd ..
[root@localhost ~]# cat hello.c
/* This C source can be compiled with:
gcc -o hello hello.c
*/
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello World\n");
return 0;
}
As the comment suggests, this is an output C file, and in order to be executed by the operating system, it must first be compiled. Compilation is done with a special program called a compiler. Read more about the “gcc” compiler.
Let’s compile hello.c and create an executable hello file.
[root@localhost ~]# gcc -o hello hello.c [root@localhost ~]# ls bench.py hello hello.c rv128test.bin hello.js readme.txt test
A new hello file has been created that we can execute from the command line. The syntax is as follows:
[root@localhost ~]# ./hello Hello World
Let’s look at and run the other hello file. It is written in another very popular Python language. Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine. Python is different from the compiled languages, such as C and C++, as Python code is not required to be built and linked like code for these languages.
[root@localhost ~]# cat bench.py
def fib(n):
if n < 2:
return n
return fib(n-2) + fib(n-1)
n = 25
print "fib(", n, ")= ", fib(n)
This program calculates the sum of the first 25 Fibonacci numbers.
[root@localhost ~]# python bench.py fib( 25 )= 75025
Download files in Linux environment
The operating system would be incomplete if there was no connection to the external environment – the Internet. Of course, the developers have tried to provide many ways to communicate with the Internet. There is a wide range of commands and applications for working with protocols, connecting to servers and clients.
The holy grail of Linux – the Wget tool
GNU Wget is a free software package for retrieving files using the protocols HTTP, HTTPS, FTP and FTPS. Wget is a command line tool, which can work in the background mode. This allows user to start a retrieval and disconnect from the system and Wget will download the file or many files. Wget can follow links in HTML, XHTML, and CSS pages, supports proxy servers, uses the passive FTP downloading by default, supports IP version 6. The progress of downloading file is traced by a progress bar.
We will use Wget to download a game from the popular GitHub file repository. The game contains two important files and is written in C language.
Number-shifting-Game-in-c-programming-Language
The header file getchInLinux.h and the source file myGame.c must be downloaded from GitHub.
C header files are files that typically contain function prototypes and class declarations. They rarely have a real body code of a function. To use most C features, you must include the appropriate header files in the #include directive.
C source file is the actual main program which must be compiled to executable file. The source file can also include functions and classes.
Click on the game name link above to open the GitHub repository.
Find the file name getchInLinux.h and click on it. A page with the contents of the file opens. On the right side of the screen there is a Raw button, click on it and you will open the raw view of the header file. Copy the address from the browser’s address bar.
[root@localhost ~]# wget -O getchInLinux.h https://raw.githubusercontent.com/jayprakashkumar1/Number-Shifting-Game-for-Linux-Environment-/master/getchInLinux.h Connecting to raw.githubusercontent.com (185.199.108.133:443) getchInLinux.h 100% |*************************************************************************************************************************| 947 0:00:00 ETA
Repeat the same procedure for the file myGame.c
[root@localhost ~]# wget -O myGame.c https://raw.githubusercontent.com/jayprakashkumar1/Number-Shifting-Game-for-Linux-Environment-/master/myGame.c Connecting to raw.githubusercontent.com (185.199.110.133:443) myGame.c 100% |******************************************************************************************************************| 7573 0:00:00 ETA
Let’s compile the file myGame.c and check the result.
[root@localhost ~]# gcc -o myGame myGame.c [root@localhost ~]# ls bench.py getchInLinux.h hello.c hello.js myGame myGame.c readme.txt rv128test.bin test [root@localhost ~]# ./myGame
Enjoy the game!
Who runs all this?
The Linux operating system consists of many files, folders, modules, applications and processes. In general, we can describe the entire operating system in three main parts:
– The Linux kernel. It is the main monolithic part of the operating system. Drivers and kernel extensions run in the system kernel, with full access to available hardware. Most of the graphical environments in Linux do not work in the kernel, unlike Microsoft Windows for example. The Linux kernel is adapted to work with a variety of devices such as tablets, smartphones, etc. Some mobile operating systems, such as Android and Maemo (Nokia), use modified versions of the Linux kernel.
– The Linux command interpreter (“shell”) is a user interface for accessing services of the respective operating system. In general, operating system shells use either a command-line interface or a graphical user interface. Shell is a program that accepts commands from the user and launches programs associated with those commands, or in other words it is a software component that provides an interface to the user. The shell provides access to kernel services of the operating system. The most popular shells are bash, tcsh, ash, csh, sh, zs.
– The Linux distribution. Linux or GNU/Linux is the common name for all operating systems based on the Linux kernel and system tools and libraries, usually from the GNU project. Most of these operating systems are called Linux distributions, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, which is controversial. Different Linux distributions target different user groups. They can differ in the graphical interface they use, the application programs that come with the installation, the system tools they use (such as package managers), and many other indicators. The most popular Linux distributions are Mint, Debian, Ubuntu, CentOS, Red Hat Enterprise Linux.
The system command “uname”
Now I want find my Linux kernel version.
[root@localhost ]# uname -r 4.15.0-00049-ga3b1e7a-dirty
my Linux kernel version is 4.15.0-00049, where:
- 4 : Kernel version.
- 15 : Major revision.
- 0 : Minor revision.
- 00049 : Patch level or number.
- ga3b1e7a-dirty : Linux distribution/kernel specific additional information.
In fact, the command “uname” has many more options. It can print information about the whole system. If you want all available information about the system type “uname -a”, which stands for all.
[root@localhost ~]# uname -a Linux localhost 4.15.0-00049-ga3b1e7a-dirty #11 Thu Nov 8 20:30:26 CET 2018 riscv64 GNU/Linux
- Linux – the kernel name.
- localhost – the hostname.
- #11 Thu Nov 8 20:30:26 CET 2018 – the kernel version.
- riscv64 – the machine (hardware) type.
- GNU/Linux – the operating system name.
Shell special parameters
I can find also the current shell.
[root@localhost ]# echo $0 sh
If we extract the contents of the variable “$0”, the type of command shell is displayed on the screen.
The shell uses several variables as special. Their use is allowed only as a call but not to set by the user. This special variables are also referred as Bash shell variables.
- ($0) Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands (see Shell Scripts), $0 is set to the name of that file. If Bash is started with the -c option (see Invoking Bash), then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the filename used to invoke Bash, as given by argument zero.
Learn more about shell special parameters.
Linux configuration files
Into the “/etc” folder there are number of files noted as primary source of configuration wherever applicable. These standardized configuration files are standardizations of the best distribution-specific configuration files previously used. Here is a little overview over these common configuration files systemd supports on all distributions.
- /etc/hostname: the host name for the system.
- /etc/os-release file consist of standardization of the various distribution ID files like /etc/fedora-release and similar. Every Linux distribution introduced their own file here.
Learn more about Linux configuration files.
Now I will find the Linux distribution by reveal the os-release file.
[root@localhost ]# cat /etc/os-release NAME=Buildroot VERSION=2016.08-git-svn30683 ID=buildroot VERSION_ID=2016.08-git PRETTY_NAME="Buildroot 2016.08-git"
Hardware specifications
There is a directory “proc” – process information pseudo-filesystem. The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures.
- /proc/cpuinfo is a collection of CPU and system architecture dependent items, for each supported architecture a different list.
- /proc/meminfo – this file reports statistics about memory usage on the system.
- /proc/devices is a text listing of major numbers and device groups.
- /proc/diskstats – this file contains disk I/O statistics for each disk device.
Now let us try to get our central processor information using the cat command and the cpuinfo file.
[root@localhost etc]# cat /proc/cpuinfo hart : 0 isa : rv64acdfimsu mmu : sv48
rv64acdfimsu is a Web-based RISC-V Emulator.
Paging is a system by which a piece of hardware (commonly referred to as the Memory Management Unit or MMU) translates virtual addresses into physical addresses. Our emulator uses Sv48 for 48-bit virtual addresses.
Let’s take a look at the available memory from the meminfo file.
[root@localhost etc]# cat /proc/meminfo MemTotal: 251980 kB MemFree: 246376 kB MemAvailable: 244140 kB Buffers: 0 kB Cached: 1192 kB SwapCached: 0 kB Active: 1532 kB Inactive: 228 kB Active(anon): 572 kB Inactive(anon): 4 kB Active(file): 960 kB Inactive(file): 224 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 0 kB SwapFree: 0 kB Dirty: 0 kB Writeback: 0 kB AnonPages: 588 kB Mapped: 1108 kB Shmem: 8 kB Slab: 2828 kB SReclaimable: 348 kB SUnreclaim: 2480 kB KernelStack: 160 kB PageTables: 48 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 125988 kB Committed_AS: 1452 kB VmallocTotal: 67108863 kB VmallocUsed: 0 kB VmallocChunk: 0 kB
The total memory (RAM) is 251980 Kilobytes = 246.07 Megabytes. It is a good value for an emulator.
List of devices in the file devices.
[root@localhost etc]# cat /proc/devices Character devices: 1 mem 2 pty 3 ttyp 4 /dev/vc/0 4 tty 5 /dev/tty 5 /dev/console 5 /dev/ptmx 7 vcs 10 misc 13 input 29 fb 128 ptm 136 pts 229 hvc 254 virtio-portsdev Block devices: 254 virtblk 259 blkext
The “ps” command – a simple handy tool
PS stands for process show and show list of processes with “ps” command. It has simple usage:
Usage: ps [-o COL1,COL2=HEADER]
PID USER COMMAND
1 root {init} /bin/sh /sbin/init
2 root [kthreadd]
3 root [kworker/0:0]
4 root [kworker/0:0H]
5 root [kworker/u2:0]
6 root [mm_percpu_wq]
7 root [ksoftirqd/0]
8 root [kdevtmpfs]
9 root [netns]
10 root [oom_reaper]
11 root [writeback]
12 root [crypto]
13 root [kblockd]
14 root [kswapd0]
15 root [kworker/0:1]
32 root [khvcd]
42 root dhcpcd
47 root sh -l
58 root [kworker/u2:1]
82 root ps
Little explanation:
PID – Process Identification (unique process number in the operation system). A smaller number means that the process was started earlier.
USER – The owner of the process (user or other process which started this process).
COMMAND – The command that started the process. If it is in square brackets, it means that the command was executed without its arguments or cannot be found.
Monitoring processes and system resource usage on Linux with the command “top”
Linux has a handy tool for real-time monitoring of the entire system. Top command provide a view of process activity in real time. Read the status of all processes from the folder /proc on specified intervals and display a screenful of them.
Usage: top [-b] [-nCOUNT] [-dSECONDS]
It is one of the most useful tools in a sysadmin’s toolbox, and it comes pre-installed on every distribution. Let’s try it.
[root@localhost ]# top
Mem: 5688K used, 246292K free, 8K shrd, 0K buff, 1184K cached
CPU: 0% usr 0% sys 0% nic 99% idle 0% io 0% irq 0% sirq
Load average: 0.12 0.10 0.09 1/20 357
PID PPID USER STAT VSZ %VSZ %CPU COMMAND
340 47 root R 2112 1% 1% top
47 1 root S 2112 1% 0% sh -l
1 0 root S 2048 1% 0% {init} /bin/sh /sbin/init
42 1 root S 1800 1% 0% dhcpcd
7 2 root SW 0 0% 0% [ksoftirqd/0]
58 2 root IW 0 0% 0% [kworker/u2:1]
8 2 root SW 0 0% 0% [kdevtmpfs]
15 2 root IW 0 0% 0% [kworker/0:1]
2 0 root SW 0 0% 0% [kthreadd]
3 2 root IW 0 0% 0% [kworker/0:0]
4 2 root IW< 0 0% 0% [kworker/0:0H]
5 2 root IW 0 0% 0% [kworker/u2:0]
6 2 root IW< 0 0% 0% [mm_percpu_wq]
9 2 root IW< 0 0% 0% [netns]
10 2 root SW 0 0% 0% [oom_reaper]
11 2 root IW< 0 0% 0% [writeback]
12 2 root IW< 0 0% 0% [crypto]
13 2 root IW< 0 0% 0% [kblockd]
14 2 root SW 0 0% 0% [kswapd0]
The “Top” command screen is divided into two paragraphs. The first paragraph contains concise information about the system.
The “Top” command screen is divided into two paragraphs.
Paragraph first – concise information about the system
Row one:
– used memory;
– free memory;
– shared memory;
– buffer memory (records to disks);
– cached memory (disk cache with the most frequently used disk locations).
Row two contains the breakdown of CPU load by processes:
– percentage use of the processor by user processes;
– percentage CPU usage by system processes;
– “nice” value for determining the importance of the process. High value processes take precedence over execution;
– idling;
– input/output operations;
Row three:
– average load for one minute, for five minutes and for fifteen minutes.
Paragraph second – the tasks paragraph
Here is the title line:
– PID – unique number (identifier) of the process;
– PPID – a unique number of the parenting process;
– USER – the user who started the process;
– STAT – state of the process.
– VSZ – Virtual Memory Size. It includes all memory that the process can access, including memory that is swapped out, memory that is allocated, but not used, and memory that is from shared libraries.
There are several process conditions:
– Runnable (R):
- A process in this state is currently running or is in a queue waiting to be executed by the microprocessor.
– Interruptible sleep (S):
- A process in this state is waiting for some event to happen or end.
– Uninterruptible sleep (D):
- A process in this state is waiting for some input / output operation to complete.
– Stopped or Traced (T):
- Such a process is stopped by a process control signal (by Ctrl + Z keys or by the user) or because it has been traced.
– Stopped by debugger during trace (t).
– Dead process (X).
– Parked process (P).
– Zombie (Z):
- This is a terminated process whose structure is still stored in memory. The reason may be that this process is called by another process (parental) and while the parental is active it needs information about the heirs.
Press “q” to exit the “top” command.
Comments