Linux
Linux
-$ who am I
-$ Who mom likes
-$ who
• Output
User Mgmt
• Parameters of who:
Parameter Description
• Output
Create User
● To add new user
○ $ sudo adduser veer
● To log in new user account
○ $ su -l veer
● To exit user use exit command or ctrl+D
Create user group
● How to see user group name?
• Output
Add user to root group
● Check user is root user or not.
○ $ su -l jack
○ $ sudo ls
○ $ exit
● Delete a user
$ sudo deluser jack --remove-home
• Output
File permissions
Introduction:
Our Team has one server and each team member has an account
to be used for daily routine job. Today we have two new
employees joining us and we need you to help them create their
accounts.
Assignment
Please finish these tasks:
Account 1
Username:Tom
Home directory:/home/Tom
User Tom uses zsh by default.
User Tom belongs to Rapidcode user group as well as dev user group.
Account 2
Username:Jerry
Home directory:/home/Jerry
User Jerry uses bash by default.
User Jerry belongs to Rapidcode user group as well as test user group
Linux
Directory, File operation and Environment variables
May 2020
Directory structure
• In Microsoft Windows, files are stored in folders on different data drives like C: D:
E:
• But, in Linux, files are ordered in a tree structure starting with the root
directory.
Tree like Directory structure
$ /etc/passwd
$ users/Rapidcode/notes
In Linux, . represents the current directory and .. represents the upper directory,
also called parent directory.
$ cd ..
To go to your home directory : $ cd ~
Directory commands
To remove a directory:
$rmdir dir_name
$rmdir –f dir_name
Directory commands
To start the Linux internal vim tutorial, Enter the following command to start it:
$ vimtutor
To Use head and tail to View a File: $ head –n fname, $tail –n fname
File permissions
To view the file permissions of files in system: $ ls –l
Listing Files
Use the command declare to create a variable named tmp: $ declare tmp
In fact, you don't need to declare a variable at first, you can create it when you want to
use it.
Use echo and $ to read the value of a variable($ is used to refer to the value of a
variable, don't forget to enter it.):
$ echo $tmp
Types of Variable
They can affect the processes ongoing or the programs that are executed in the
environment.
There are three commands related to the environment variables set, env, export. These
three commands are very similar. They are used to print environment variable
information. The range of variables involved varies though.
Environment Variable
Command Description
It can display all the variables of the
current shell, including its built-in
set environment variables, user-defined
variables and exported environment
variables.
It can display the environment variables
associated with the current user and
env
allow the command to run in the
specified environment.
It displays variables that are exported
from the shell as environment variables
export
and it can also export custom variables
as environment variables.
Files associated to Environment
Variable
Linux has two important files /etc/bashrc and /etc/profile. They store shell
variables and environment variables.
.profile is valid only for the current user. Environment variables written
in /etc/profile are valid for all users.
Environment Variable: PATH
Find the path and order for a command:
PATH saves the search paths of commands.
In every user's home directory, there is a configuration script that will run
automatically to initialize the environment . zsh configuration file is .zshrc and Bash
configuration file is .bashrc.
Time-related parameters:
Parameter Description
Last visit time (Last access
-atime
time)
The last time when the
-ctime contents of the file were
modified
The last time when the
-mtime file attributes were
modified
find
Take -mtime for example:
∙ -mtime n:The file that was modified on the day n days ago
∙ -mtime +n:List the files that were modified n days ago (not including the nth
day)
∙ -mtime –n:List the files that have been modified within n days (including the
day)
∙ -newer file :List all the files that are more recent than the existing file
named file
The most common file formats on Windows are * .zip, * .rar, * .7z.
Extension Description
*.zip The files are packaged and compressed using zip programs
*.rar The files are packaged and compressed using rar programs
*.7z The files are packaged and compressed using 7z programs
*.tar The files without compression are packaged using tar programs
*.gz The compressed files are packaged using gzip programs
*.xz The files are packaged and compressed using xz programs
*.bz2 The files are packaged and compressed using bzip2 programs
The files are packaged using tar programs and compressed using
*.tar.gz
gzip programs
The files are packaged using tar programs and compressed using
*.tar.xz
xz programs
The files are packaged using tar programs and compressed using
*tar.bz2
bzip2 programs
The files are packaged using tar programs and compressed using
*.tar.7z
7z programs
Using zip to package programs
$ cp -r /lib .
$ zip -r -q -o rapidcode.zip /home/rapidcode/lib
$ du -h rapidcode.zip
$ file rapidcode.zip
After packaging and compressing the directory, use du to view the size of the
compressed file (zip file).
Using zip to package programs
Using zip to compress programs
You can only use absolute path, otherwise it will not work.
Using zip to compress programs
Use du to view the default compression level, the minimum and the highest
compression levels, and also the sizes of uncompressed files:
$ du -h -d 0 *.zip ~ | sort
h, --human-readable
d, --max-depth(Display an entry for all files and directories depth.)
Creating an encrypted zip package
Warning: For zip, there are some compatibility issues between Windows and
Linux/UNIX, such as line break(a non-printable character signifying end of line),
which is CR+LF (Carriage-Return + Line-Feed) in Windows and LF in Linux. If you
open a text content on a Windows platform, which was edited on a Linux platform,
you can't see line breaks.
For the above problem, you need to add -l parameter to convert LF to CR+LF.
Using unzip to decompress the zip file
Use quiet mode to extract the file to a directory specified using -d parameter:
$ unzip -q rapidcode.zip -d ziptest
If you only intend to see the contents of the compressed package, you can use
the -l parameter. The files in the package will not be extracted:
$ unzip -l rapidcode.zip
rar is a compressed file format on Windows. On Linux, you can use rar and unrar to
create and extract *.rar files.
rar command has a lot of parameters. We are only going through some
basic operations.
Creating tar archive
Pay attention that the file name must follow the -f parameter.
Extracting and view tar archive
• Extracting a tar archive (tarball)
• Viewing an archive
-t parameter to view the contents of a tar archive but not decompress it:
*.tar.gz -z
*.tar.xz -J
*.tar.bz2 -j
Built in commands and external commands:
Built-in commands are actually part of the shell program, which contains
some Linux system commands. These commands are written in the
builtins of the bash source and are recognized by the shell program and
run in the shell program itself.
External commands are more practical programs in Linux. Since the functions of
these programs are usually strong, these contain a large amount of code.
External commands are installed outside Bash, usually placed in /bin, /usr/bin,
/sbin and /usr/sbin.
Built in commands and external commands:
For example, the results of these two command executions are different:
$ type exit
$ type service
Help commands
• help
• man
• info
In The above output, top left corner shows LS (1). LS indicates the name of the
manual and (1) indicates that the manual is in the first chapter. What is this
section or chapter? There are so many chapters in the man manual:
info: is a GNU project from the Free Software Foundation. It can have a more complete
display of GNU information. So you can get more information about commands.
$ info cat
Assignment
Create a file named test, then use zip and tar to package and compress it
respectively. Last step, you need to extract the archive to /home/rapidcode.
Linux
Execution Sequence control and pipeline
May 2020
Executing commands sequentially
If you need to use apt-get to install a software package and run the installed software
(or command tool) immediately but your host has not updated the software list, then
you may have to perform the following series of operations:
To enter all the commands at once and let the commands execute by themselves
in order , use ; to complete as follows:
Rapidcode
Cowsay performing ohch
Rapidcod
e
Rapidcod
e
What is pipeline
• A named pipe appears as a file, and generally has processes attached for IPC
(interprocess communication).
Executing ‘ls’ on etc
Let's try the pipeline to view /etc directory. We use ls to view the directory listing:
$ ls -al /etc
Abhi
Implementing Pipe
Use a pipeline here: $ ls -al /etc | less
Using cut command
To print some fields for each row - $ cut /etc/passwd -d ':' -f 1,6s
Print the characters after the first five characters (including the fifth one):
$ cut /etc/passwd -c 5-
Search all the text files containing “originally" in the /home directory and
display the line number:
$ grep -rnI “originally" ~
$ wc /etc/passwd
Count the number of folders in /etc:
$ ls -dl /etc/*/ | wc -l
wc : word count
The following are the options and usage provided by the command.
1. Match all lines that start with ‘hello’. E.g.: “hello there”
2. Count all lines that end with ‘done’. E.g.: “well done”
3. Match all lines that contain any of the letters ‘a’, ‘b’, ‘c’, ‘d’ or ‘e’.
4. Match all lines that do not contain a vowel
Linux
Simple text processing
May 2020
Text processing commands
• tr
• col
• join
• paste
Using tr command
The command tr can be used to delete word(s) in a piece of text or to convert
it.
Format:
$ tr [option]...SET1 [SET2]
Show the input text in uppercase or lowercase. The following commands show in
uppercase:
$ cat /etc/passwd | tr '[:lower:]' '[:upper:]'
or
$ cat /etc/passwd | tr '[a-z]' '[A-Z]'
For more uses of tr, use tr --help or man tr to fetch further information.
Executing tr command
Using col command
col: col can replace the Tab character with a number of space characters or reverse
the operation.
Format:
$ col [option]
Option Description
Example:
Use col -x to convert tabs in /etc/protocols to spaces, then use cat to see the file
content; and we will find that ^I is gone:
$ cat /etc/protocols | col -x | cat -A
Performing cat on protocol file
Executing col command
Using join command
join: The join utility performs an "equality join'' on the specified files and writes the
result to the standard output.
The "join field'' is the field in each file by which the files are compared.
Format:
$ join [option]... file1 file2
Format:
$ paste [option] file...
Option Description
Specify the merged
-d delimiter, which
defaults to tab.
Concatenate all of the
lines of each of the
-s
input files in
command-line order.
Executing paste command
Example:
$ echo hello > file1
$ echo rapidcode > file2
$ echo rapidcode.io > file3
$ paste -d ':' file1 file2 file3
$ paste -s file1 file2 file3
Assignment
In the "File Packing and Compression" section, we mentioned that some special
characters in text files on Windows/DOS and Linux/UNIX are not the same, such as
line break:
It is CR+LF (\r\n) on Windows, and LF (\n) on Linux/UNIX.
We can use the cat -A text to see the invisible special characters contained in the
text. On Linux, \n is shown as $, and \r\n is shown as ^M$ on Windows.
We want you to use the previously learned commands to complete the conversion
of the DOS text format to the UNIX text format.
Linux
Data Streaming & File descriptor
May 2020
Data Stream
A Linux stream is data traveling in a Linux shell from one process to another
through a pipe, or from one file to another as a redirect.
A Linux shell, such as Bash, receives input and sends output as sequences or
streams of characters
Standard streams
Linux shells use three standard I/O streams, each of which is associated with a
well-known file descriptor:
1. stdin is the standard input stream, which provides input to commands. It has
file descriptor 0
2. stdout is the standard output stream, which displays output from commands. It
has file descriptor 1.
3. stderr is the standard error stream, which displays error output from
commands. It has file descriptor 2.
Understanding Standard streams
Understanding File descriptor
When a process makes a successful request to open a file, the kernel returns a file
descriptor which points to an entry in the kernel's global file table.
The file table entry contains information such as the inode of the file, byte offset, and
the access restrictions for that data stream (read-only, write-only, etc.).
Data stream redirection
In the Linux command line interface, operators like pipe (|) and redirect (< and >)
(>>) control input and output streams.
The original output to the standard output is redirected to a file. Since the standard
output (/dev/stdout) is a file, there is no problem when we redirect the output to
another file.
Both the standard output and the standard error are directed to the screen
display. So, the output of the command we often see usually includes the standard
output and standard error results, such as the following:
Use the cat command to read two files at the same time, one of which exists and the
other does not exist:
$ cat test.c hello.c
Redirect the standard error to the standard output, and then redirect the
standard output to the file:
$ cat Documents/test.c hello.c >somefile 2>&1
Note that you should add & before the output redirect file descriptor, otherwise the
shell will be redirected to a file named 1.
Redirecting stdout & stderr
Using tee command
Sometimes, in addition to redirecting the output to a file, we also need to print the
information at the terminal. Then you can use the tee command:
$ echo 'hello rapidcode' | tee hello
Permanent redirection
Redirect operations seen above were temporary, which means that the redirection
was only valid for the current command. We can use exec to implement a
permanent redirect:
# Open a sub-shell
$ zsh
# The output of the command you execute later will be redirected to the file
until you exit the current shell, or cancel the exec's redirect (later we will tell
you how to operate)
$ ls
$ exit
$cat somefile
Performing permanent redirection
Creating a file descriptor
There are 9 file descriptors in the shell.
We can also use the file descriptors 3 to 8, but they are not open by default.
You can use the following command to view the file descriptors that are
open in the current shell process:
$ cd /dev/fd/;ls –Al
# Note that there should be no space between > and &. If there is a space,
result will be wrong.
$ echo "this is test" >&3
$ cat somefile
$ exit
Showing file descriptor
Closing a File descriptor
Use the following operation to turn 3 file descriptor off:
$ exec 3>&-
$ cd /dev/fd;ls -Al;cd -
Assignment
Understand the following code. This code does not work properly.
Please use what you learned in this section to analyze the underlying reason
and try to solve this problem.
Example:
Suppose that we have a text file, including two strings "rapidcode" and
"coderapid".
rapid*
Why?
∙ \: *Mark the next character as a special character or a literal character. *For example, "n"
matches the character "n". "\n" matches a line break. "\"" matches """ and "\(" matches "(".
∙ ^: Match the start position of the input string.
∙ $: Match the end position of the input string.
∙ {n}: n in {n} is a non-negative integer that means matching n times. For example, "o{2}" cannot
match "o" in "Bob", but it can match two "o" in "food".
∙ {n,}: n in {n,} is a non-negative integer that means matching at least n times. For example,
"o{2,}" cannot match "o" in "Bob", but it can match all "o" in "fooooood".
∙ {n,m}: m and n are non-negative integers, where n <= m. It can match at least n times and match
utmost m times. For example, "o{1,3}" will match the first three "o" in "fooooood". "O{0,1}" is
equivalent to "o?". Please note that there cannot be spaces between commas and two numbers.
∙ *: Match the previous subexpression zero or more times. For example, "zo*" can match "z",
"zo" and "zoo". * is equivalent to {0,}.
The Priority of Operators
The priority is decreasing from top to bottom, from left to right:
∙ \ : Escape character
∙ (), (?:), (?=), []
∙ *, +, ?, {n}, {n,}, {n,m} : Restrictions
∙ ^, $ : Location point
∙ | : The choice (also known as alternation or set union) operator matches either
the expression before the operator or the expression after the operator.
Introduction to ’grep’
grep is used to print the matching pattern string in the output text, which uses a
regular expression as a condition for pattern matching. grep supports three regular
expression engines respectively, with three parameters to specify the chosen
engine:
Parameter Description
POSIX extended
-E regular expression,
ERE
Perl regular
-P
expression, PCRE
• Position match
Find the line that starts with "rapidcode" in /etc/group:
$ grep 'rapidcode' /etc/group
$ grep '^rapidcode' /etc/group
• Restriction match
Match all strings that begin with 'z' and end with 'o':
$ echo 'zero\nzo\nzoo' | grep 'z.*o’
Match strings beginning with 'z', ending with 'o' and with an arbitrary character in
the middle:
$ echo 'zero\nzo\nzoo' | grep 'z.o’
Match strings that begin with 'z' and end with any number of 'o':
$ echo 'zero\nzo\nzoo' | grep 'zo*’
• Choice match
By default, grep is case-sensitive. The command below will match all
lowercase letters:
$ echo '1234\nabcd' | grep '[a-z]’
Exclude characters:
$ echo 'geek\ngood' | grep '[^o]’
Special
Description
Symbol
[:alnum:] Upper and lower case letters and digits (0-9, A-Z, a-z)
[:alpha:] Any English uppercase and lowercase letters (A-Z, a-z)
[:blank:] Blank key and [Tab]
Control buttons on the top of the keyboard, including
[:cntrl:]
CR, LF, Tab, Del and so on
[:digit:] Numeral digits (0-9)
All the keys except for blank key (for example, Space)
[:graph:]
and [Tab]
[:lower:] Lowercase letters (a-z)
[:print:] Characters that can be printed out
[:punct:] punctuation symbols (" ' ? ! ; : # $ ...)
[:upper:] uppercase letters ( A-Z)
[:space:] Symbols include blank keys, [Tab], CR and so on
[:xdigit:] Hexadecimal digits, including 0-9, A-F, a-f
grep:Using POSIX 'Extended Regular Expression'
Using Extended Regular Expression with grep requires adding the -E parameter,
or using egrep.
• Restriction match
Match only "zo":
$ echo 'zero\nzo\nzoo' | grep -E 'zo{1}’
Format:
# For example:
$ sed -i '1s/sad/happy/' test
#Replace the "sad" in the first line of the test with "happy"
Commonly used parameters of sed
Parameter Description
By default, each line of input is echoed to the
standard output after all of the commands have
-n
been applied to it. The -n option suppresses this
behavior.
Append the editing commands specified by the
-e
command argument to the list of commands.
Specify to execute the commands in
-f filename
the filename file.
Use extended regular expressions, which default to
-r
standard regular expressions.
[n1],[n2]command
[n1]~[step]command
# Some of these commands can be added to the scope of the role, such as:
$ sed -i 's/sad/happy/g' test # g represents the global scope
$ sed -i 's/sad/happy/4' test # 4 represents the 4th matching string
[n1],[n2] means all lines from n1 to n2. [n1]~[step] means all lines from n1
and the step size is step. command means the execution command. Here
are some commonly used execution commands:
The execution command sed
Command Description
• AWK is an excellent text processing tool, one of the most powerful data
processing engines available in Linux and Unix environments.
• Its name comes from its founders, Alfred Aho, Peter Jay Weinberger (Peter
Weberger) and Brian Wilson Kernighan (Brian Colin), first letters of their last
names
• It allows you to create short programs that read input files, sort data,
process data, perform calculations on input and generate reports, as well as
countless other functions.
gawk is the open source implementation of the awk interpreter for GNU Project.
Although the early GAWK release was an older AWK alternative, it was continually
updated to include NAWK's features.
mawk is also an interpreter for the awk programming language. The mawk
follows the AWK language defined by POSIX 1003.2 (draft 11.3) and contains some
features that are not mentioned in the AWK manual.
Basic concept of awk
You can see that as with many programming languages all the actions are in {}.
pattern is usually a "relational" or "regular expression" that represents the text
used to match the input and action is the action that will be executed after the
match has been made. In a complete awk operation, you may have only one of
them. If there is no pattern , the default is to match all the input text. If there is
no action, the default is to print the matching content to the screen.
Basic format of awk
awk [-F fs] [-v var=value] [-f prog-file | 'program text'] [file...]
In this operation, we have omitted the pattern. So, awk will match the
entire contents of the input text by default.
Operation experience of awk
# or
$ awk '{
> if(NR==1){
> OFS="\n"
> print $1, $2, $3
> } else {
> print}
> }' test
Operation experience of awk
# or
$ awk '
> BEGIN{
> FS="."
> OFS="\t"
> }{
> if(NR==2){
> print $1, $2, $3
> }}' test
Performing awk
Some built-in variable of awk
Name Description
If there are multiple files, only the first one is valid. If the input is from the
FILENAME
standard input, it is NULL.
$N N represents the field number. The maximum value is the value of the NF variable.