Linux Foundation Certified System Administrator (LFCS) : File types, Package management, compression, Archive , Grep (Part 2)
File types in Linux
Every object in Linux can be called as files. For example,
Also special files,
To check that in terminal, use file <object name>
Also, we can use ls -ld object name
here starting d means directory. check the table to verify
Filesystem hierarchy
The /home
directory carries all users (Bob/Dave/Rahim etc) except the root user.
/opt
directory carries all 3rd party software etc.
To mounts software to the device, we use /mnt
and use /tmp
to keep it temporarily
All external drives (usb disks) etc. are kept on /media file
Once you use this command, you will see your USB disk is on the /dev/sdb1 file system mounted on /media/usb
The basic binary files are within /bin folder
/etc
stores most of the configuration files in linux
/lib
and /lib64
is where imported libraries are kept
/var
is where system writes all logs
Linux package management
Package managers like DPKG , APT works for Ubuntu, also RPM for RHEL (Red Hat Enterprise Linux) , CentOs
What's the difference between Ubuntu, RHEL and Centos?
here, Ubuntu is a Debian based and RHEL and Centos is RPM Based
Talking about RHEL and Centos,
RHEL is paid and Centos is a community version.
But what is this package?
Its a compressed archieve which has all files needed to run a software/application.
Assume that we want to install gimp in our ubuntu system
To download gimp, we will use it's debian package which carries all binaries to run it
But there are some issues! As linux has 100s of distributions, these package managers don't fit with every distribution.
While installing, you will get errors and issues
You will find dependencies and get stuck. Here we can see gimp depends on libgimp2.0
Again, those dependencies can have their own dependencies too!
To solve all of these issues, we have package manager
Types of package managers
DPKG -> Package manager for debian based distributions (example, Ubuntu, LInux mint, Elementry os)
APT -> Newer front end for DPKG system
APT-GET -> Traditional front end for DPKG System
RPM -> Base package manager found in Redhat based distribution like RHEL, CIntos, fedora
YUM -> Frontend for RPM System
DNF -> More feature reached front end for RPM System
RPM (Red Hat Package Manager)
The extension is .rpm
the basic functionalities are
- Installation
rpm -ivh <package name>
-i means installation, -v means verbose (using or expressed in more words than are needed) and
Uninstall
rpm -e <package name>
Upgrade
rpm -Uvh <package name>
Query
rpm -q <package name>
Verification
A lab:
rpm -q will also work
Another task
I have checked the location and the file name then installed using rpm -ivh
Let's use yum to solve this issue and the dependencies.
this time we will use firefox
, instead of the firefox-68.6.0-1.el7.centos.x86_64.rpm
(Specific version file) we have on the folder. Basically yum will install all dependencies to install firefox (Basically a generalized name)
**YUM (**Yellowdog Updater Modified) Package manager
how yum installs a package?
yum depends on repositories which can be locally available or online.The information about the repository is saved on the etc folder
Yum still uses RPM package manager to install
For redhat based system, a file called redhat.repo
can be created. Also, for unofficial file or other 3rd party, we can create <otherfile>.repo
.here nginx.repo
Whys this 3rd party repo?
Many times the official repo (redhat.repo
) is not updated and thus we create our new repo and install there.
Let's see how it installs
it firstly runs a transaction and then checks the dependencies.
example:
DPKG (Debian Package Manager) and APT
DPKG is a lower level package manager
Sometimes, it creates issue and therefore, we use higher level package manager which is APT or APT-GET
We can solve it using apt or, apt-get command. apt (Advanced package manager) is much better option than apt-get
APT basically uses DPKG just like how Yum used RPM . Also, all of its repositories are listed in /etc/apt/........
Example:
Solution:
sudo apt install firefox
View sizes of file
Archiving files
To create an compressed file, use tar -cf
tar -tf to see files within the compressed file, tar -xf to see the content of the compressed file, tar -zhf to reduce the size of the compressed file
Example:
Solution:
Task:
Compressing and Archive
To see file sizes use du -sk filename
or, ls -lh filename
Archiving
here -c is for creating an archive and -f for name of the file.
again tar -tf
used to see contents in the compressed file, -xf
to extract the contents of the compressed file, -zcf
to compress files to a reduced size
Compression
To reduce the size we do the compression.We can use bzip2, gzip, xz
. To uncompress use bunzip2, gunzip, unxz
we can also read files without un-compressing using zcat, bzcat, xzcat
Searching for files and directories
To find a file we can use locate
But it can happen that your desired file is created just recently and you might not find it as the database is not updated. So, update that using updatedb
and then use locate or find
command
Task:
Solution:
Using GREP to find
here, we are looking for 'second' & 'capital' from the sample.txt file
To search case sensitive, use grep -i,
if you don’t know which file to search from use grep -r
. It will show file name and the result
For lines that does not
match a word , use grep -v
Assume you need exact words like exam here.
To search whole word, use grep -w
But if we don’t need exact words but words associated with it like examples
use grep -vw
to print matching line and one more line below, use -A1
to print one row before the desired sentence, use -B1
Task:
Solution:
Task
Solution:
IO Redirection
There are three data streams created when you launch a Linux Command.
Standard input, output and error can be redirected to text files. To redirect standard output to a file instead of printing, we use > .
Here shell.txt file will have $SHELL in it now. But as we know $SHELL means the location of shell which is in /bin/bash
Basically >
overwrites existing content
but if we want to add one more line, use >>
Again, in order to redirect the error message , we need to use 2 followed by forward arrow symbol
Task
Solution
echo "a file in my home directory" > /home/bob/file_with_data.txt
Task:
Solution:
Task
Solution
Redirecting data to a file to be later consumed by another command can be tedious process. So, we overcome this by command line pipes. The pipes are defines using vertical bar symbol (|)
Here sample.txt file has 2 lines.
BY making use of the pipe, the output of the command grep Hello sample.txt becomes the standard input for the less command that follows after the operator (|)
Another way can be tee command. With tee
, the standard output is still printed on the screen before overwriting the contents of the file with the same string.
to append a line instead of overwrite use tee -a
Task
we will use sudo grep -ir 172.16.238.197 /etc/ > /home/bob/ip
sudo for administrative purpose, grep to search and use -ir as we don’t case sensitive (-i) and we don’t know which folder we have the string . So, use -r too.
then directory name (/etc/) and then save that answer to /home/bob/ip and pass it by >.
We can also verify that our output has been passed to /home/bob/ip channel
Task
Answer should be echo "a file in my home directory" > /home/bob/file_with_data.txt
because echo is to write and we write “a file……….” and then pass it (>) to /home/bob/file_with_data.txt
Task
Here we check the error first using python3 /home/bob/my_python_
test.py
We get this error “python3: can't open file '/home/bob/my_python_test.py': [Errno 2] No such file or directory”
Now, we will pass this error using cat and then the command (ython3 /home/bob/my_python_
test.py
) which generates error and then 2> and then to the address (/home/bob/py_error.txt) where we want to pass the error
cat python3 /home/bob/my_python_
test.py
2> /home/bob/py_error.txt
We can verify if the /home/bob/py_error.txt file can has the error or not by writing this:
cat /home/bob/py_error.txt
We get these error messages :
cat: python3: No such file or directory
cat: /home/bob/my_python_test.py: No such file or directory
Task
Use zcat /usr/share/man/man1/tail.1.gz | tee /home/bob/pipes
zcat because we don’t want to extract
then the file path and then command line pipe (|) and then . With tee
, the standard output is still printed on the screen before overwriting the contents of the file with the same string and finally the goal path (/home/bob/pipes)
we can verify that our goal path got the result using cat goal path (/home/bob/pipes)
VI Editor
Note: You can check the default editor using this command update-alternatives --display editor
Command to open the editor is vi followed by the name of the file/path
by default, when we open the VI editor, we are in the command Mode. We can change that as our need by pressing i or, esc or, : etc.
In command mode, we have
In insert mode,
For the last line,
We can also use, VIM editor which is the upgraded version of VI
Done for this blog!!