Version 12 (modified by 2 years ago) ( diff ) | ,
---|
Hands-On
Linux package management
The most common and popular package managers they are likely to first start using is the apt (Advanced Package Tool) because it is most widely shipped as the default Package Manager for users of Debian, Ubuntu and Mint.
We’ll cover some of the basic commands of how to use apt in order to illustrate the ease of use of using a package manager in the terminal window.
Let's see - update/upgrade, package installation
How to install a package
Install a package as follows by specify a single package name or install many packages at once by listing all their names.
To install apache2
sudo apt install apache2
Find dependencies of a package
Once package has been installed you might want to understand which dependencies the package is making use of, you can list out the dependencies of a package using apt as follows.
sudo apt depends apache2
Search for package
It is highly likely that you will need to search for a package to perform a specific task. The apt package manager enables you to easily search for packages via the terminal.
In this case, I want to search for an image manipulation program, I can simply include the phrase in quotes and use the apt search facility
sudo apt search "image manipulation program"
View package details
You will often want to find and view details of package. In this example I want to view some details regarding the apache2 package we installed earlier.
sudo apt show apache2
Upgrade system packages
Often the above command will instruct you that new releases of packages are available, so to install new versions of all the packages on your system.
sudo apt upgrade
You will often want to execute both these commands at the same time, so you can run them both at the same time using.
sudo apt update && sudo apt upgrade -y
Remove packages
To remove packages from your system.
sudo apt remove apache2
if you want to ensure all packages configuration and dependencies are removed at the same time you can use the purge switch.
sudo apt remove apache2 --purge
Remove unused packages
Installing or upgrading packages will result in some dependencies not being required, you can clean up these unused dependencies after removing that particular package, it's dependencies will remain on the system, therefore to remove them use auto-remove as follows:
sudo apt autoremove
Repositories and key management
A Linux repository is a storage location that contains essential and popular software for different Linux distributions and, each distribution has its own official repositories (also called standard-repositories).
Debian-based distributions, including, Ubuntu make use of the Advanced Package Tool (APT) to install and update packages. You can find software repository information in the /etc/apt/sources
. list file on your Debian-based Linux installation.
to add manually
Although you can manually enter repository details in the file, it can quickly become a tiresome job. A better way of adding them to your system is by using the add-apt-repository
tool.
======= Install add-apt-repository =======
You won't find the add-apt-repository utility installed on your system by default. It is a part of the software-properties-common package. To install add-apt-repository using the APT package manager, type:
sudo apt install software-properties-common
Add Repositories Using add-apt-repository
Now that you've installed the package, it's time to add a third-party software repository to your system. The basic syntax for adding repositories is:
sudo add-apt-repository [options] repository
ex:
sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe"
Key Management
The Linux key-management facility is primarily a way for various kernel components to retain or cache security data, authentication keys, encryption keys, and other data in the kernel
Linux Editors
nano
Nano, text editor is a user-friendly, free and open-source text editor that usually comes pre-installed in modern Linux systems. It is easy to use command line text editor for Unix and Linux operating systems. It includes all the basic functionality you'd expect from a regular text editor, like syntax highlighting, multiple buffers, search and replace with regular expression support, spellchecking, UTF-8 encoding, and more.
To check if it is installed on your system type:
nano --version
- To create and open a new file.
$nano new_filename
- To save a file
press Ctrl+o
- To cut paste in a file.
Ctrl+k
is used to cut and Ctrl+u
is used to paste the text.
- To search a word in a file.
Ctrl+w
- To enable spell check in nano. First, install the spell check package.
$sudo apt install spell
- To do spell check first press Ctrl+t
- Now it will ask you to replace the incorrect words
- Enter the word to replace with there
- As soon as you will press the enter key
Vim
Vim is a text editor that is an upgraded version of the Vi editor and is more compatible with Vi. The most usage of vi editors is to create a new file, edit an existing file, or just read a file. Vim editor is more useful in editing different kinds of plain text.
To install vim on Debian based Linux like ubuntu run the command:
sudo apt-get install vim
1.To open a file in vim editor just write the file name after the vim command in the terminal as follows:
vim filename.txt
- Write into file
press i
3.Save and Exit:
We have written the data into a file now the task is to save and close the file to do that first exit from insert mode by pressing the Esc key. To write a command first type semicolon ( : ) and then type the command wq! or x! (both do the same thing) And then hit ENTER.
:wq!
4.Exit without saving the file:
To exit from the file without saving the file just use the command q! As follows
:q!
5.Search:
To search the word After the cursor uses the backslash key and then write the word and press enter.
:/word
- Use
:n
to move on next matching word
- Use
:N
to move on previous matching word
6.Search and Replace:
To replace the word in file use s/ command in vim like
:s/searchword/replaceword/
To do replace all occurrence of word use g
:s/searchword/replaceword/g
This command will replace the word globally.
To confirm before replacements of words use gc
:s/searchword/replaceword/gc
To use this command in the whole file use % before the command
:%s/searchword/replaceword/gc
Filters - find, grep, sed and awk
find
The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other UNIX commands can be executed on files or folders found.
$ find [where to start searching from]
- Search a file with specific name.
$ find ./folder -name sample.txt
- Search a file with pattern.
$ find ./folder -name *.txt
It will give all files which have ‘.txt’ at the end.
- How to find and delete a file with confirmation.
$ find ./folder -name sample.txt -exec rm -i {} \;
- Search for empty files and directories.
$ find ./folder -empty
- Search for file with entered permissions.
$ find ./folder -perm 664
- Search text within multiple files.
$ find ./ -type f -name "*.txt" -exec grep 'Geek' {} \;
grep
The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out). Syntax: grep [options] pattern [files]
- Case insensitive search : The -i option enables to search for a string case insensitively in the given file. It matches the words like “UNIX”, “Unix”, “unix”.
$grep -i "UNix" file.txt
- Displaying the count of number of matches : We can find the number of lines that matches the given string/pattern
$grep -c "unix" file.txt
- Display the file names that matches the pattern : We can just display the files that contains the given string/pattern.
$grep -l "unix" *
or
$grep -l "unix" f1.txt f2.txt f3.xt f4.txt
- Checking for the whole words in a file : By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.
$ grep -w "unix" file.txt
- Displaying only the matched pattern : By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
$ grep -o "unix" file.txt
- Show line number while displaying the output using grep -n : To show the line number of file with the line matched.
$ grep -n "unix" file.txt
- Inverting the pattern match : You can display the lines that are not matched with the specified search string pattern using the -v option.
$ grep -v "unix" file.txt
- Matching the lines that start with a string : The regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
$ grep "^unix" file.txt
- Matching the lines that end with a string : The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.
$ grep "os$" file.txt
10.Specifies expression with -e option. Can use multiple times :
$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" file.txt
- Search recursively for a pattern in the directory: -R prints the searched pattern in the given directory recursively in all the files.
Syntax
$grep -R [Search] [directory]
sed
SED is a text stream editor used on Unix systems to edit files quickly and efficiently. The tool searches through, replaces, adds, and deletes lines in a text file without opening the file in a text editor.
- SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).
- SED command in unix supports regular expression which allows it perform complex pattern matching.
1.Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file.
$sed 's/unix/linux/' geekfile.txt
- Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word “unix” with “linux” in a line.
$sed 's/unix/linux/2' geekfile.txt
3.Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
$sed 's/unix/linux/g' geekfile.txt
4.Replacing from nth occurrence to all occurrences in a line : Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth… “unix” word with “linux” word in a line.
$sed 's/unix/linux/3g' geekfile.txt
5.Parenthesize first character of each word : This sed example prints the first character of every word in parenthesis.
$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
6.Replacing string on a specific line number : You can restrict the sed command to replace the string on a specific line number. An example is
$sed '3 s/unix/linux/' geekfile.txt
7.Duplicating the replaced line with /p flag : The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.
$sed 's/unix/linux/p' geekfile.txt
8.Printing only the replaced lines : Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.
$sed -n 's/unix/linux/p' geekfile.txt
9.Replacing string on a range of lines : You can specify a range of line numbers to the sed command for replacing a string.
$sed '1,3 s/unix/linux/' geekfile.txt
10.Deleting lines from a particular file : SED command can also be used for deleting lines from a particular file. SED command is used for performing deletion operation without even opening the file Examples:
1.To Delete a particular line say n in this example
Syntax:
$ sed 'nd' filename.txt
Example:
$ sed '5d' filename.txt
- To Delete a last line
Syntax:
$ sed '$d' filename.txt
- To Delete line from range x to y
Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt
- To Delete from nth to last line
Syntax:
$ sed 'nth,$d' filename.txt
Example:
$ sed '12,$d' filename.txt
- To Delete pattern matching line
Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt
awk
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators.
Syntax:
awk options 'selection _criteria {action }' input-file > output-file
- Default behavior of Awk: By default Awk prints every line of data from the specified file.
$ awk '{print}' employee.txt
- Print the lines which match the given pattern.
$ awk '/manager/ {print}' employee.txt
- Splitting a Line Into Fields : For each record i.e line, the awk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.
$ awk '{print $1,$4}' employee.txt