= 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` 1. To create and open a new file. `$nano new_filename` 2. To save a file `press Ctrl+o` 3. To cut paste in a file. `Ctrl+k` is used to cut and `Ctrl+u` is used to paste the text. 4. To search a word in a file. `Ctrl+w` 5. 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` 2. 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]` 1. Search a file with specific name. `$ find ./folder -name sample.txt ` 2. Search a file with pattern. `$ find ./folder -name *.txt ` It will give all files which have ‘.txt’ at the end. 3. How to find and delete a file with confirmation. `$ find ./folder -name sample.txt -exec rm -i {} \; ` 4. Search for empty files and directories. `$ find ./folder -empty` 5. Search for file with entered permissions. `$ find ./folder -perm 664` 6. 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] 1. 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` 2. 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` 3. 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` 4. 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` 5. 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` 6. 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` 7. 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` 8. 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` 9. 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` 12. 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 ==== ==== awk ====