Quiz 1
Quiz 1
Type 📒 Lecture
Date @December 22, 2021
Lecture # 1
Week # 1
Requirements
An .iso image of the operating system we want
A Hypervisor
Source: https://www.vmware.com/topics/glossary/content/hypervisor.html
Oracle VirtualBox
Steps
Download the Ubuntu .iso file from https://ubuntu.com/download/desktop
Install them
Adjust the RAM and Storage as per your liking, make sure to keep the minimum specs
Uhh it’s just Next, Next, Next .... Next ... Restart
When you install it correctly and get it up and running, you might see something like this ...
Lecture # 2
Week # 1
Terminal in Ubuntu
clear
pwd
By default, you are placed in the home directory of the currently logged in user
ls
ps
uname
exit
Or you can also press Ctrl + D to exit out of the terminal session
like, ls -al
man <command-name>
For example
man ls
Filesystem in Linux
To change directory
cd <location>
Examples
cd without any arguments will take us to the home directory of the currently logged in user
/usr hierarchy
/usr/bin → User commands
/usr/lib → Libraries
/var hierarchy
/var/cache → Application cache data
Lecture # 3
Week # 1
Typical output of ls -l
File types
- → Regular file
d → Directory
l → Symbolic link
c → Character file
b → Block file
s → Socket file
p → Named pipe
Permission string
mkdir <folder-name>
chmod g-w <folder-name> to remove the write permission from the group
Similarly, chmod g-x <folder-name> to remove the execute permission from the group
To add permission, chmod g+w <folder-name> to give write permission to the group
u → User
g → Group
o → Others
- → To remove permission
+ → To add permission
r → Read
w → Write
x → Execute
touch command
Used to modify the timestamp of a file or folder
cp command
mv command
mv <file-name> <new-path> to move a file to a new path
rm command
rm <file-name> to remove a file
Alias
We can also set an alias for long commands, for example ...
whoami
less <file-name>
file <file-name>
Some commands
chmod → Change permissions of a file
mv → Rename/Move a file
rm → Remove a file
Lecture # 1
Week # 2
ls
Recursive listing
ls -l will display the files & folders in the current directly, in a list
If we add the name of a folder after ls -l , it will display the files & folders of that folder
Press q to exit
cat <file-name> → To read the entire text file, and dump them on the terminal window
wc <file-name> → Displays the number of lines (or newlines), words and bytes in a file
apropos <keyword> → Takes in the keyword and returns a list of commands that contain the keyword in their name or in
their description
Can pass a command name as optional parameter and it’ll display it’s help
→ Open a file (which is similar to a webpage) that lists all the commands, and we can move the cursor to any
info
Use the < button to go back, how do you press the < button? Shift + ,
Aliases are a nickname given to a command, which may want for our convenience
alias <name>='<your-command>'
# Example
alias ll='ls -al'
unalias <name>
# Example
unalias ll
Multiple arguments
Second argument
Links
Hard links
Symbolic links
ln -s <source> <destination>
Example
ln <source> <destination>
Example
Notice the inode number for the hardlink is the same as the file
Size of files
ls -s lists the files with file sizes
In-memory filesystem
/proc
/sys
These contain the info about the system, so no rm -rf -ing around as root
A few commands
free → To display the system memory stats, like used memory, free memory
Lecture # 2
Week # 2
echo
Example:
echo $HOME
$HOME
$HOSTNAME
$PWD
$PATH
Process control
Use of & to run a job in the background
fg
$hell variables 1
coproc
jobs
top
kill
1 → failure
2 → misuse
i → interactive mode
echo
it's a start
echo $USERNAME
echo $USER
echo $PWD
echo $HOME
echo $HOSTNAME
echo $PATH
We can enclose the shell variable (marked as $) in double quotes, and it'll replace the value
However, if we use single quote around the shell variable, the shell variable is printed as it is
printenv
env
$hell variables 2
printenv HOME
set
setallows you to change the values of shell options and set the positional parameters, or to display the names and
values of shell variables. (Source)
\<alias>
# Example
\ls
\date
$hell variables 3
Linux Process Management
Type 📒 Lecture
Date @December 22, 2021
Lecture # 3
Week # 2
sleep command
It is a delay for a specified amount of time
Example
# The following command will make the prompt sleep for 5 seconds
sleep 5
Coprocess
The shell command → coproc
In simple words, this command is used to run a process without actually losing control of the prompt
Usage
coproc sleep 20
When the above mentioned process is completed, the prompt will output a message saying Done
We can run the ps command to view the running status of the aforementioned command
kill
Usage
kill -9 <PID>
Usage
sleep 30 &
fg
jobs
Example
top command
It’s like your Windows Task Manager, but in the Linux terminal
Pressing Ctrl + Z while top is open suspends the process, then you can do your work and come back to it by using the
fg command
$-
echo $-
The output of this shell variable would tell us about the bash shell we are currently using
To know what the output means, type man bash and match the flags
history
The following command displays out all the commands that have been run on the current shell, chronologically
history
The following command will run the n -th command that has been run, make sure to put n from the list of commands
output from the above command
n is integer
!n
The following command will run the previous command that was executed on the bash shell
!!
Brace Expansion
So the output of echo $- had a B flag, it refers to Brace Expansion
Example
echo a{b,c,d}e
echo {a..z}
echo {A..C}{g..k}
# The * is a wildcard character, it expands to all the files in the current dir
echo *
Similarly ...
# The following command will return all the files folders that start with D
echo D*
Exit codes
0 → All good, no error
When a process running in another place (like in another shell) was killed from somewhere else (killed from not
the same shell)
If the exit code is out of range, bash takes the modulo of the exit code w.r.t. 256
Example
# Create a new bash subshell and exit with the status code 300
bash -c "exit 300"
Lecture # 1
Lecture
https://youtu.be/Lcx9UsS7y8Y
URL
https://21f1003586.notion.site/Combining-commands-and-files-
Notion URL
2476c1091a704743840ae8b76ab078c9
Week # 3
The subsequent commands after command-n will not run if the previous command resulted in an error
The subsequent commands after command-n will not run if the previous command resulted in a success
(<command>)
We can run any command enclosed within parentheses to execute them in a subshell, and returned back the result
stdin (0)
It is a pointer to a stream that is coming from the keyboard (or the user input)
stdout (1)
Points to the screen where the output is made
stderr (2)
Points to the screen where the output is made
When we type this command, the cat command is supposed to receive the input from a file that is listed in the command
line, but instead, we left that intentionally blank
So, the cat command, instead, reads the content from the stdin , i.e. the keyboard
Lecture # 2
Week # 3
Redirect the output of the command to stdout , which is the display in this case
Redirections 1
command < file1
Any command which takes input from the keyboard, now takes input from file1
Redirections 2
By default, the stderr will output to the display
List the files of /usr/bin directory, but use the less command to scroll at ease
ls /usr/bin | less
/dev/null
Redirections 3
The stderr is written to /dev/null , which gets warped to another dimension
The tee command splits the output into 2 streams, one stream is written to the file1 another, one to the display
diff command
This command compares files line-by-line
Usage
diff file1 file2
Redirections 4
Shell variables
Type 📒 Lecture
Date @January 9, 2022
Lecture # 3
Week # 3
Creating a variable
Exporting a variable
Exporting means making the value of the variable available to a shell spawned by the current shell (wut)
export myvar="value string"
OR
myvar="value string"
export myvar
Shell variables 1
Using variable values
echo $myvar
echo ${myvar}
echo “${myvar}”
Removing a variable
unset myvar
echo $?
Return codes:
Return codes:
So, if myvar is set, change it’s value to “default” and display it else display null
Display 4 chars of the string value of the variable myvar , skipping first 5 chars
Shell variables 2
echo ${myvar%%pattern} → match max possible
Changing case
echo ${myvar,} → change the first char to lower case
You can remove the restrictions by replacing the - sign with a + sign
Indexed arrays
declare -a arr → declare arr as an indexed array
Associative arrays
Kind of like Hash maps?
echo ${hash[”a”]} → value of element with index (or key?) “a” in the array
Shell variables 3
Software Management - pt. 1
Type 📒 Lecture
Date @January 16, 2022
Lecture # 1
Week # 4
Dependency checking
Package Types
Tools
Inquiring package db
apt-cache pkgnames
Try apt-cache pkgnames <starting-char> to search to packages with the few starting chars
Package names
Package priorities
required → essential to the proper functioning of a system
extra → could conflict with packages with higher priority, has specialized requirements, install only if needed
Package sections
https://packages.ubuntu.com/focal
Checksums
A way to verify that the packages that we are installing are legit
sha1sum <filename> to get the SHA1 checksum of a file, use it to compare it to other files
sha256sum <filename> to get the SHA256 checksum of a file, use it to compare it to other files
Lecture # 2
Week # 4
This will ask for the user password and depending on the su status of the user, it will either display the file or show an
error like user is not in the sudoers file. This incident will be reported.
How can a superuser find out the failed sudo attempt by non-su?
Go to /var/log
Look for auth.log file, these events are reported in this file
To get updates
sudo apt-get update
You can pass the -y flag to not get bugged by the Y/N prompt
Reinstall a package
Removing/Cleaning up
Remove packages that were automatically installed to satisfy a dependency and not needed
apt-get autoremove
apt-get clean
Folder: info
Using dpkg
dpkg -l pattern
dpkg -L package
dpkg -s package
dpkg -S pattern
dpkg-query
Example usage
Lecture # 3
Week # 4
Pattern matching
regex & grep
Regex
regex is a pattern template to filter text
Usage
grep ‘pattern’ filename
Character classes
Backreferences
\1 through \9
Uses of grep
Match a pattern at the end of the line, a word boundary one might say
\b looks for a word boundary, so that pattern could also occur at the end of a word in the middle of a line
$ looks for line boundary only, so the pattern occurs at the end of the line
Here, the first character in the pattern is followed by either of the 2 characters given in []
In the grep 'S.*[mn]' command, it matches from the start of the line
In the curly braces, we provide the # of times the preceding character should be matched
We can pass one number or a multiple numbers separated by comma for their matching
Lecture # 4
Week # 4
Match package names that are 3 characters long and start with the letter g
Match package names that are between 1 to 5 characters long and start with the letter g
make sure to use the ^ (hat) character in the front of the regex pattern to match the math category, otherwise it will match
package category and the names
But, there is one problem, if there is any number that is more than 12 digits or more than 6 digits respectively, it will pick
that up too
Pick a roll number (of the type MM22B001) from a text file
cut
1234;hello world,line-1
234567;welcome cmdline,line-2
3456;parse text,line-3
We can skip the beginning or the ending of the substring parameter, it works like python
cut -c -4 fields.txt
cut -c 8- fields.txt
To do the same thing using grep (similar thing, not exactly the same)