Tuesday, 1 September 2020

The Linux wc command (word count)

Linux WC Command, Linux Exam Prep, LPI Certification, LPI Learning, LPI Guides, LPI Study Material

The Linux word count command is named wc. The wc command counts the number of characters, words, and lines that are contained in a text stream. If that sounds simple or boring, it's anything but; the wc command can be used in Linux command pipelines to do all sorts of interesting things.

Also Read: LPI Certifications

Let's take a look at some Linux wc command examples to show the power of this terrific little command.

Linux wc command examples (words, lines, characters)


In its most basic use, the wc command can be used to count the number of lines, words, and characters in a file, like this:

$ wc /etc/passwd
      65     185    3667 /etc/passwd

In that example, the /etc/passwd file has 65 lines, 185 words (as wc determines words), and 3,667 characters.

If you just want to know the number of lines in a file just add the -l argument, like this:

$ wc -l /etc/passwd
      65 /etc/passwd

Or, if you want to know the number of words in a file, add the -w argument, like this:

$ wc -w MyStory.txt
     185 MyStory.txt

Using the Linux wc command in command pipelines


The wc command follows the paradigm of reading input from STDIN and writing output to STDOUT, so it can be used in all sorts of Linux command pipelines. This command shows the number of users currently logged into your Linux system:

who | wc -l

It does that by piping the output of the who command into the input of the wc command, which in this case is used to count the number of lines of output in the who command.

Similarly, this next command shows the number of processes currently running on your Linux system:

ps -e | wc -l

This works the same way as the previous example: Generate output using one command (the ps command), and use the wc -l command to count the number of lines of output from that command.

Saturday, 29 August 2020

Automatically Clean Unused Temporary files in Linux

Linux Tutorial and Material, Linux Certification, Linux Exam Prep, LPI Exam Prep

How can I remove/clean unused files in Linux?. In this article, you will learn how to configure a timer that manages temporary files. In most Modern Linux systems, a large number of temporary files and directories are required for optimal processing. Accumulatively, they could consume gigabytes of storage space if not cleaned frequently. It is therefore necessary to purge the old files so that they do not fill up disk space.

Some users/applications will use the /tmp directory to hold temporary data, while others use a more task-specific location such as daemon and user-specific volatile directories under /run. Volatile means that the files only exist in memory. If the system is rebooted or when there is a lose of power, all the contents of volatile storage will be gone.

Automatically Clean Unused Temporary files in Linux


In Red Hat Enterprise Linux 7 and later, a new tool called systemd-tmpfiles is included. This tool provides a structured and configurable method to manage temporary directories and files.

You can check the services started with the command:

$ systemctl status  systemd-tmpfiles-*
● systemd-tmpfiles-setup.service - Create Volatile Files and Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-setup.service; static; vendor preset: disabled)
   Active: active (exited) since Mon 2020-02-10 08:27:50 EAT; 1 weeks 3 days ago
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)
  Process: 794 ExecStart=/usr/bin/systemd-tmpfiles --create --remove --boot --exclude-prefix=/dev (code=exited, status=0/SUCCESS)
 Main PID: 794 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/systemd-tmpfiles-setup.service

Feb 10 08:27:50 envoy-nginx.novalocal systemd[1]: Starting Create Volatile Files and Directories...
Feb 10 08:27:50 envoy-nginx.novalocal systemd[1]: Started Create Volatile Files and Directories.

● systemd-tmpfiles-setup-dev.service - Create Static Device Nodes in /dev
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-setup-dev.service; static; vendor preset: disabled)
   Active: active (exited) since Mon 2020-02-10 08:27:49 EAT; 1 weeks 3 days ago
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)
  Process: 553 ExecStart=/usr/bin/systemd-tmpfiles --prefix=/dev --create --boot (code=exited, status=0/SUCCESS)
 Main PID: 553 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/systemd-tmpfiles-setup-dev.service

Feb 10 08:27:49 envoy-nginx.novalocal systemd[1]: Starting Create Static Device Nodes in /dev...
Feb 10 08:27:49 envoy-nginx.novalocal systemd[1]: Started Create Static Device Nodes in /dev.

When the systemd-tmpfiles-setup service unit is started, it runs the systemd-tmpfiles –create –remove command. The command checks for configuration files from:

◉ /usr/lib/tmpfiles.d/.conf
◉ /run/tmpfiles.d/.conf
◉ /etc/tmpfiles.d/*.conf

If there are files and directories marked for deletion in above configuration files, the’ll be removed. For the files and directories marked for creation, they are created with the correct permissions if necessary.

How Temporary Files are Cleaned with a Systemd Timer


A systemd timer unit called systemd-tmpfiles-clean.timer triggers the systemd-tmpfiles-clean.service on a regular interval, which then executes the systemd-tmpfiles –clean command.

You’ll specify how often the service should be started in the [Timer] section. See example below.

$ cat /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

In above example, the systemd-tmpfiles-clean.service will be triggered 15 minutes after the system has booted up. Any other trigger happens 24 hours after the last service trigger. You can adjust the values to your liking.

If you make a change, ensure you reload service.

sudo systemctl daemon-reload
sudo systemctl enable --now systemd-tmpfiles-clean.timer

How To Manually Clean Temporary Files


Let’s configure systemd-tmpfiles to clean the /mytmp directory. This will ensure the directory does not contain files that that have not been used in the last 3 days.

You can copy the example configuration file and update it – /usr/lib/tmpfiles.d/tmp.conf

Edit the file like below.

$ sudo vim /etc/tmpfiles.d/mytmp.conf
See tmpfiles.d(5) for details
# Clear tmp directories separately, to make them easier to override
q /mytmp 1777 root root 3d

If you want to ensure a directoty exist with correct owneship, create a configuration like below.

d /run/mytmp 0700 root root 60s

Any file in this directory that remains unused in the last 60 seconds must be purged.

Once the file is created, use the following command to ensure the file contains the appropriate configuration.

sudo systemd-tmpfiles --create /etc/tmpfiles.d/mytmp.conf

If you don’t see any error in the output, then the it confirms that the configuration settings are correct. You can invoke a manual clean anytime with the command:

systemd-tmpfiles --clean /etc/tmpfiles.d/mytmp.conf

Thursday, 27 August 2020

Understanding the Landscape of Linux Certifications for Junior and Senior Systems Administrators

Linux Certifications, LPI Exam Prep, LPI Guides, LPI Learning, LPI Prep

Understanding the availability and viability of current Linux certifications for systems administrators can be a challenge. Because Linux is an open source project, no single company or organization can claim exclusive authority to validate the Linux skills of an IT professional. Intense competition has led to the arrival and departure of many certification programs through the years (here's looking at you SAIR, Novell, and Ubuntu). Some certifications have remained a staple in the marketplace and others are offering good alternatives. Here is a breakdown of certification organizations/companies and their offerings.

LPI - Linux Professional Institute


The Linux Professional Institute has its roots in the open source community as a non-profit certification organization. LPI's certifications have always been vendor neutral in nature. This is a blessing and a curse as LPI certified professionals will be well versed in the breadth of open source technologies that are not tied to any particular Linux distribution. The curse can be than an employer may need specific certification credentials based on vendor-mandated technologies. With an LPI certification, a professional will have the credentials for a generalized and versatile skill set that is not specific to a vendor. Look for the LPIC-1 certification for junior administrators and LPIC-2 for senior administrators. Each certification requires the completion of two exams and the exams can be completed at a Pearson Vue testing center.

LPI is known for a focus on system administrators. Recently the LPI certification portfolio has expanded into a new certification called DevOps Tools Engineer certification. This certification validates skills for use of tools in the workflow of systems administration and software development.

Linux Foundation


The new kid on the block for Linux certifications. The Linux Foundation has been a solid figure in the open source project space for many years. They host well over one hundred open source projects. Some of these projects are well known, including Kubernetes, Prometheus, NodeJS, Jenkins, and many other small and large projects. Arguably the most famous of projects they host is the development of the Linux kernel. This project is at the core of the Linux operating system.

The Linux Foundation has leveraged its long-standing relationship with the Linux kernel developers by offering to the public a unique catalog of certifications. Directly related to Linux systems administration are two certifications: the junior level certification is the LF Certified SysAdmin and the senior level is the LF Certified Linux Engineer. These are unique in the method used for the candidate to sit the exam. Unlike all the other vendors there is no need to go to a testing center, kiosk location, or classroom. The exams are administered online using a variety of tools and remote proctoring. Like the Red Hat exams, these exams are performance-based where candidates are faced with a simulation of real-world tasks and scenarios.

The online exam delivery format that the Linux Foundation has pioneered has been expended to other technologies in the open source space.  Be on the lookout for certifications related to Kubernetes, Cloud Foundry, and Hyperledger.

Red Hat


Although a for-profit corporation, Red Hat has been a big friend to the open source community. Red Hat has recently been acquired by IBM, which cements their quest to expand open source software into the enterprise. Their M.O. seems to be a precise ability to have a very healthy relationship with the open source community while profitably monetizing those technologies.

This respect can also be seen in Red Hat's IT certifications. They have maintained a respected status amongst industry experts, the rank-and-file systems administrators, and the open source community at large. Red Hat's exams are "performance-based." This term in Red Hat parlance requires the examinee to perform actual systems administration tasks on a working system. The completion of these tasks is then checked, validated, and scored.

Theoretical knowledge will only get you so far in a Red Hat exam. Arguably the most respected Linux certification is the RHCE – Red Hat Certified Engineer. The little brother RHCSA – Red Hat Certified Systems Administration is a prerequisite to receiving the RHCE. Taking Red Hat exams can be logistically challenging as candidates must locate, schedule, and travel to a limited number of kiosk and classroom locations. This logistical tension has made other certifications viable competitors.

CompTIA


An association of industry and IT professionals CompTIA is a juggernaut for IT and professional certifications. Up until this year CompTIA and LPI have partnered together to offer the Linux+. Now LPI and CompTIA have parted ways and CompTIA offers the Linux+ that is now *not* powered by LPI. This means that the reciprocal agreement is no longer in place. It used to be that if you received your CompTIA Linux+ powered by LPI then LPI would automatically give you the credentials for the LPIC-1 certification. Essentially the question bank for the two certifications exams were the same. Now they have diverged and CompTIA Linux+ is a different set of questions than the LPIC-1. CompTIA Linux+ is now just a one (1) exam test instead of two like the LPIC-1.

Other Less Popular Linux Certifications


You would think that with these four main players in the Linux certification space, this would be sufficient competition for the need. However, there are even more players and options for vendor-based certification. Here are a few:

◉ Oracle Certified Associate (OCA)
◉ Oracle Certified Professional (OCP)
◉ SUSE Certified Administrator (SCP)
◉ SUSE Certified Engineer (SCE)
◉ GCUX: GIAC Certified Unix Security Administrator

Junior and Senior Level Linux Certification Comparison


(listed in order of perceived popularity)

CERTIFICATION LEVEL  EXAM COST  EXAMINATION LOCATION
RHCE - Red Hat Certified Engineer Sr. Systems Administrator $400 Classroom, Kiosk
RHCSA – Red Hat Certified Systems Administrator  Jr. Systems Administrator  $400  Classroom, Kiosk
CompTIA Linux+  Jr. Systems Administrator  $319  Testing Center 
LPIC-1: Linux Professional Institute Certification Level 1  Jr. Systems Administrator  $200 x 2  Testing Center 
LPIC-2: Linux Professional Institute Certification Level 2  Sr. Systems Administrator  $200 x 2  Testing Center 
LFCS - Linux Foundation Certified SysAdmin  Jr. Systems Administrator  $300  Online 
LFCE - Linux Foundation Certified Engineer  Sr. Systems Administrator  $300  Online 
LPIC-2: Linux Professional Institute Certification Level 2  Sr. Systems Administrator  $200 x 2  Testing Center 

Tuesday, 25 August 2020

Counting Files and Directories in Linux

Directories in Linux, LPI Tutorials and Materials, LPI Exam, LPI Exam Prep, LPI Certification

Although at first counting files and directories in Linux may not seem the most exciting topic you will be surprised just how much you can learn from these tasks. They are also well suited to those starting out in Linux with one or two elements that may be new even to seasoned Linux users.

Firstly, if we want to be counting files and directories in Linux then ls may be a great option Used in conjunction with wc we can count the number of items returned. The command ls is used to list directory content and wc is used for word count, used with -l it can count lines.

$ ls | wc -l

Whilst this is good we will not show hidden files or directories. Hidden files start with a dot. To list these we can use the option -a or -A with ls. To show all files we use -a and almost all files with -A. Yes almost all, we exclude the . and .. directories which are system links.

[tux@packstack ~]$ ls
dir1  dir2  dir3  dir4

[tux@packstack ~]$ ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  dir1  dir2  dir3  dir4

[tux@packstack ~]$ ls -A
.bash_logout  .bash_profile  .bashrc  dir1  dir2  dir3  dir4

[tux@packstack ~]$

If we want to count the output we are better not count the . and .. directory.

[tux@packstack ~]$ ls -A | wc -l
7

[tux@packstack ~]$

From the output we can see that we have a total of 7 items in the current directory.

If we want to count directories and files separetely then we can use the GNU command find. To list files we can use the option -type f. Of course we could count the output as before.

[tux@packstack ~]$ find . -type f
./.bash_logout
./.bash_profile
./.bashrc

Directories in Linux, LPI Tutorials and Materials, LPI Exam, LPI Exam Prep, LPI Certification
Listing directories is similar but we will see that we will include the current directory which we may not want.

[tux@packstack ~]$ find . -type d
.
./dir1
./dir2
./dir3
./dir4

To exclude the current directory from the count we can use the option -mindepth 1 to ensure that we start with the directories content and not the directory.[tux@packstack ~]$

find . -mindepth 1 -type d
./dir1
./dir2
./dir3
./dir4

So we can see that counting files and directories in Linux is not difficult but it can be even easier. Well at least counting directories. The hard link count for a directory can be used to show how many subdirectories there are in the directory. Each subdirectory has a link back to the parent. A directory start with a hard link count of 2 so just remove 2 from the current hard link count to see how many subdirectories.

[tux@packstack ~]$ ls -ld /etc
drwxr-xr-x. 112 root root 8192 Jun 8 15:03 /etc

The etc directory has 110 subdirectories on my system, 112 – 2 = 110

Saturday, 22 August 2020

A BIG collection of Unix/Linux ‘grep’ command examples

Grep Command, LPI Tutorial and Materials, LPI Exam Prep, LPI Certification, LPI Learning

Linux grep FAQ: Can you share some Linux/Unix grep command examples?

Sure. The name grep means "general regular expression parser", but you can think of the grep command as a “search” command for Unix and Linux systems: It’s used to search for text strings and regular expressions within one or more files.

I think it’s easiest to learn how to use the grep command by showing examples, so let’s dive right in.

.

.

.

this space intentionally left blank because of the long Table of Contents over there (and my poor CSS skills) -->

.

.

.

.

Abridged grep command examples


First up, if you don’t like reading a bunch of text and just want to see a collection of grep commands, this section is for you.

(If the Table of Contents over there on the right side is still in the way, click or tap the ‘hide’ link in its title to hide it):

search for a string in one or more files
----------------------------------------
grep 'fred' /etc/passwd              # search for lines containing 'fred' in /etc/passwd
grep fred /etc/passwd                # quotes usually not when you don't use regex patterns
grep null *.scala                    # search multiple files


case-insensitive
----------------
grep -i joe users.txt                # find joe, Joe, JOe, JOE, etc.


regular expressions
-------------------
grep '^fred' /etc/passwd             # find 'fred', but only at the start of a line
grep '[FG]oo' *                      # find Foo or Goo in all files in the current dir
grep '[0-9][0-9][0-9]' *             # find all lines in all files in the current dir with three numbers in a row


display matching filenames, not lines
-------------------------------------
grep -l StartInterval *.plist        # show all filenames containing the string 'StartInterval'
grep -il StartInterval *.plist       # same thing, case-insensitive


show matching line numbers
--------------------------
grep -n we gettysburg-address.txt    # show line numbers as well as the matching lines


lines before and after grep match
---------------------------------
grep -B5 "the living" gettysburg-address.txt        # show all matches, and five lines before each match
grep -A10 "the living" gettysburg-address.txt       # show all matches, and ten lines after each match
grep -B5 -A5 "the living" gettysburg-address.txt    # five lines before and ten lines after


reverse the meaning
-------------------
grep -v fred /etc/passwd             # find any line *not* containing 'fred'
grep -vi fred /etc/passwd            # same thing, case-insensitive


grep in a pipeline
------------------
ps auxwww | grep httpd               # all processes containing 'httpd'
ps auxwww | grep -i java             # all processes containing 'java', ignoring case
ls -al | grep '^d'                   # list all dirs in the current dir


search for multiple patterns
----------------------------
egrep 'apple|banana|orange' *                                                         # search for multiple patterns, all files in current dir
egrep -i 'apple|banana|orange' *                                                      # same thing, case-insensitive
egrep 'score|nation|liberty|equal' gettysburg-address.txt                             # all lines matching multiple patterns
locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG'       # oh yeah

multiple search strings, multiple filename patterns
---------------------------------------------------
grep -li "jtable" $(find . -name "*.java,v" -exec grep -li "prevayl" {} \;)           # find all files named "*.java,v" containing both
                                                                                      # 'prevayl' and 'jtable'

grep + find
-----------
find . -type f -exec grep -il 'foo' {} \;     # print all filenames of files under current dir containing 'foo', case-insensitive


recursive grep search
---------------------
grep -rl 'null' .                             # similar to the previous find command; does a recursive search
grep -ril 'null' /home/al/sarah /var/www      # search multiple dirs
egrep -ril 'aja|lpicentral' .                      # multiple patterns, recursive


grep gzip files
---------------
zgrep foo myfile.gz                           # all lines containing the pattern 'foo'
zgrep 'GET /blog' access_log.gz               # all lines containing 'GET /blog'
zgrep 'GET /blog' access_log.gz | more        # same thing, case-insensitive

That's the short version of the grep examples. The rest of this document describes many of these examples.

Searching for a text string in one file


This first grep command example searches for all occurrences of the text string 'fred' within the /etc/passwd file. It will find and display all of the lines in this file that contain the text string fred, including lines that contain usernames like "fred", and also other strings like "alfred":

grep 'fred' /etc/passwd

In a simple example like this, the quotes around the string fred aren't necessary, but they are needed if you're searching for a string that contains spaces, and will also be needed when you get into using regular expressions (search patterns).

Searching for a string in multiple files


Grep Command, LPI Tutorial and Materials, LPI Exam Prep, LPI Certification, LPI Learning
Our next grep command example searches for all occurrences of the text string joe within all files of the current directory:

grep 'joe' *

The '*' wildcard matches all files in the current directory, and the grep output from this command will show both (a) the matching filename and (b) all lines in all files that contain the string 'joe'.

As a quick note, instead of searching all file with the "*" wildcard, you can also use grep to search all files in the current directory that end in the file extension .txt, like this:

grep 'joe' *.txt


Case-insensitive file searching with the Unix grep command


To perform a case-insensitive search with the grep command, just add the -i option, like this:

grep -i score gettysburg-address.txt

This grep search example matches the string "score", whether it is uppercase (SCORE), lowercase (score), or any mix of the two (Score, SCore, etc.).

Reversing the meaning of a grep search


You can reverse the meaning of a Linux grep search with the -v option. For instance, to show all the lines of my /etc/passwd file that don't contain the string fred, I'd issue this command:

grep -v fred /etc/passwd


Using grep in a Unix/Linux command pipeline


The grep command is often used in a Unix/Linux pipeline. For instance, to show all the Apache httpd processes running on my Linux system, I use the grep command in a pipeline with the ps command:

ps auxwww | grep httpd

This returns the following output:

root     17937  0.0  0.0  14760  6880 ?        Ss   Apr01   0:39 /usr/local/apache/bin/httpd -k start
nobody   21538  0.0  0.0  24372 17108 ?        S    Apr03   0:01 /usr/local/apache/bin/httpd -k start
nobody   24481  0.0  0.0  14760  6396 ?        S    Apr03   0:00 /usr/local/apache/bin/httpd -k start
nobody   26089  0.0  0.0  24144 16876 ?        S    Apr03   0:01 /usr/local/apache/bin/httpd -k start
nobody   27842  0.0  0.0  24896 17636 ?        S    Apr03   0:00 /usr/local/apache/bin/httpd -k start
nobody   27843  0.0  0.0  24192 16936 ?        S    Apr03   0:00 /usr/local/apache/bin/httpd -k start
nobody   27911  0.0  0.0  23888 16648 ?        S    Apr03   0:01 /usr/local/apache/bin/httpd -k start
nobody   28280  0.0  0.0  24664 17256 ?        S    Apr03   0:00 /usr/local/apache/bin/httpd -k start
nobody   30404  0.0  0.0  24360 17112 ?        S    Apr03   0:00 /usr/local/apache/bin/httpd -k start
nobody   31895  0.0  0.0  14760  6296 ?        S    Apr03   0:00 /usr/local/apache/bin/httpd -k start
root     31939  0.0  0.0   1848   548 pts/0    R+   Apr03   0:00 grep http

(I deleted about half of the "httpd -k start" lines from that output manually to save a little space.)

Similarly, here's how you can find all the Java processes running on your system using the ps and grep commands in a Unix pipeline:

ps auxwww | grep -i java

In this example I've piped the output of the ps auxwww command into my grep command. The grep command only prints the lines that have the string "java" in them; all other lines from the ps command are not printed.

One way to find all the sub-directories in the current directory is to mix the Linux ls and grep commands together in a pipe, like this:

ls -al | grep '^d'

Here I'm using grep to list only those lines where the first character in the line is the letter d.

Using the Linux grep command to search for multiple patterns at one time (egrep)


You can use a different version of the grep command to search for multiple patterns at one time. To do this, just use the egrep command instead of grep, like this:

egrep 'score|nation|liberty|equal' gettysburg-address.txt

This Unix egrep command searches the file named gettysburg-address.txt for the four strings shown (score, nation, liberty, and equal). It returns any lines from the file that contain any of those words.

I should also note that "egrep" stands for "extended grep", and as you can see, it lets you do things like searching for multiple patterns at one time.

Searching for regular expressions (regex patterns) with grep


Of course the Linux grep command is much more powerful than this, and can handle very powerful regular expressions (regex patterns). In a simple example, suppose you want to search for the strings "Foo" or "Goo" in all files in the current directory. That grep command would be:

grep '[FG]oo' *

If you want to search for a sequence of three integers with grep you might use a command like this:

grep '[0-9][0-9][0-9]' *

This next grep command searches for all occurrences of the text string fred within the /etc/passwd file, but also requires that the "f" in the name "fred" be in the first column of each record (that's what the caret character tells grep). Using this more-advanced search, a user named "alfred" would not be matched, because the letter "a" will be in the first column:

grep '^fred' /etc/passwd

Regular expressions can get much, much more complicated (and powerful) than this, so I'll just leave it here for now.

Display only filenames with a grep search


If you're looking through a lot of files for a pattern, and you just want to find the names of the files that contain your pattern (or "patterns", as shown with egrep) -- but don't want to see each individual grep pattern match -- just add the -l (lowercase letter L) to your grep command, like this:

grep -l StartInterval *.plist

This command doesn't show every line in every file that contains the string "StartInterval"; it just shows the names of all the files that contain this string, like this:

com.apple.atrun.plist
com.apple.backupd-auto.plist
com.apple.dashboard.advisory.fetch.plist
com.apple.locationd.plist
org.amavis.amavisd_cleanup.plist

Of course you can also combine grep command arguments, so if you didn't happen to know how to capitalize "StartInterval" in that previous example, you could just add the -i argument to ignore case, like this:

grep -il startinterval *.plist

and that would have worked just fine as well, returning the same results as the previous grep command example.

Showing matching line numbers with Linux grep


To show the line numbers of the files that match your grep command, just add the -n option, like this:

grep -n we gettysburg-address.txt

Searching my sample gettysburg-address.txt file, I get the following output from this command:

9:Now we are engaged in a great civil war,
22:that we should do this.
24:But in a larger sense we can not dedicate -
25:we can not consecrate -
26:we can not hallow this ground.
29:have consecrated it far above our poor power
33:what we say here,
43:we take increased devotion to that cause
46:that we here highly resolve that these dead

grep before/after - Showing lines before or after your grep pattern match


After a recent comment, I just learned that you can display lines before or after your grep pattern match, which is also very cool. To display five lines before the phrase "the living" in my sample document, use the -B argument, like this:

grep -B 5 "the living" gettysburg-address.txt

This grep command example returns this output:

The world will little note,
nor long remember,
what we say here,
but can never forget what they did here.

It is for us, the living,

Similarly, to show the five lines after that same search phrase, use the -A argument with your Unix grep command, like this:

grep -A 5 "the living" gettysburg-address.txt

This grep "after" command returns the following output:

It is for us, the living,
rather to be dedicated here
to the unfinished work which they have,
thus far, so nobly carried on.
It is rather for us to be here
dedicated to the great task remaining before us -

Of course you can use any number after the -A and -B options, I'm just using the number five here as an example.

Power file searching with find and grep


A lot of times I know that the string "foo" exists in a file somewhere in my directory tree, but I can't remember where. In those cases I roll out a power command, a Linux find command that uses grep to search what it finds:

find . -type f -exec grep -il 'foo' {} \;

This is a special way of mixing the Linux find and grep commands together to search every file in every subdirectory of my current location. It searches for the string "foo" in every file below the current directory, in a case-insensitive manner. This find/grep command can be broken down like this:

◉ "." means "look in the current directory"
◉ -type f means "look in files only"
◉ -exec grep -il foo means "search for the string 'foo' in a case-insensitive manner, and return the matching line and filename when a match is found
◉ {} \; is a little bizarre syntax that you need to add to the end of your find command whenever you add the -exec option. I try to think of it as a placeholder for the filenames the find command finds.

Note that on Mac OS X systems you may be able to use the mdfind command instead of this find/grep combination command. The mdfind command is a command-line equivalent of the Spotlight search functionality.

Thursday, 20 August 2020

Best LPIC-1 and LPIC-2 certification study books 2020

LPIC-1 Certification, LPIC-2 Certification, LPI Guides, LPI Learning, LPI Exam Prep

To become proficient in administering Linux boxes which opens a plethora of other opportunities such as DevOps, Cloud Computing and System Administration to mention a few, then a lot of work needs to be put in mastering Linux together with other tools. In the quest to master this niche, this article generously provides resources in form of books that will propel one in the right direction. You will not only be able to pass your LPI exams but you will stick with something much more. And that is the power to administer systems. If that sounds great, please stick with me all through.

About LPIC-1 and LPIC-2 Certification


Linux Professional Institute (LPI) is the global certification standard and career support organization for open source professionals. LPIC-1 is the first certification in LPI’s multi-level Linux professional certification program. The LPIC-1 will validate the candidate’s ability to perform maintenance tasks on the command line, install and configure a computer running Linux and configure basic networking.

Read More: LPI Books

On the other hand, LPIC-2 is the second certification in LPI’s multi-level professional certification program. The LPIC-2 will validate the candidate’s ability to administer small to medium–sized mixed networks (lpi.org, 2019).


Best LPIC-1 Certification Study Books



Below is a list of the best LPIC-1 certification study books.

1. LPIC-1: Linux Professional Institute Certification Study Guide: Exams 101 and 102 3rd Edition by Roderick W. Smith


Relying on the extensive experience and in depth experience from the author, this practical book covers key Linux administration topics and all exam objectives and includes real-world examples and review questions to help you practice your skills. In addition, you’ll gain access to a full set of online study tools, including bonus practice exams, electronic flashcards, and more. This book is quite suitable for you because it:

◉ Prepares candidates to take the Linux Professional Institute exams 101 and 102 and achieve their LPIC-1 certification
◉ Covers all exam objectives and features expanded coverage on key topics in the exam
◉ Includes real-world scenarios, and challenging review questions
◉ Gives you online access to bonus practice exams, electronic flashcards, and a searchable glossary
◉ Topics include system architecture, installation, GNU and Unix commands, Linux filesystems, essential system services, networking fundamentals, security, and more

To get your LPIC-1 exam right and have a good understanding of Linux, get your copy by clicking on the link below:

LPIC-1: Linux Professional Institute Certification Study Guide: Exams 101 and 102

2. Linux Command Line and Shell Scripting Bible, 3rd Edition


This book by Richard Blum serves as a basic and very essential Linux resource that will guide you with plenty of examples. Linux Command Line and Shell Scripting Bible goes right away into the fundamentals of the command line, introduces you to bash scripting which will be very important in your day to day Linux administration and goes an extra mile by providing detailed examples. The third edition being the latest release, it has new updated content and examples aligned with the latest Linux features which will help you fulfill the LPIC-1 objectives

What is attractive about this resource is how the author has gone out of the way to provide sound tutorials that you can easily follow through and actually understand. The examples are apt and relevant. Take is away from Amazon by clicking on the link below:

Linux Command Line and Shell Scripting Bible, 3rd Edition

3. Linux Essentials, Second Edition


Authored by experts Christine Bresnahan and Richard Blum Linux Essentials has a professional approach that aims at developing one for Linux Administration profession as well as passing the Linux Essentials exam which can serve as a lasting foundation to Linux and LPIC-1 at the same time. It has hands-on tutorials and learning-by-doing style of learning that equip you with a solid foundation as well giving you the confidence to pass the Linux Exam. For beginners with a keen interest in joining the IT industry as a professional, this book is highly recommended. You can check the reviews at Amazon below:

Linux Essentials, Second Edition

4. Linux Bible 9th Edition


Brought to you by veteran bestselling author Christopher Negus and Christine Bresnahan (contributor), Linux Bible brings to you a complete tutorial packed with major updates, revisions, and hands-on exercises so that you can confidently start using Linux today. There are exercises in abundance aimed to make your learning interesting and hence enable it as a better learning tool. Moreover, Linux Bible places an emphasis on the Linux command line tools and can be used with all distributions and versions of Linux.

Check it out on:

Linux Bible 9th Edition

5. Linux: The Complete Reference, Sixth Edition


Richard Petersen, a Linux Expert has once again released this book that gives the reader an in-depth coverage of all Linux features. As a beginner, you will have the advantage of having a thorough coverage of all aspects of Linux distributions ranging from shells, desktops, deployment of servers, management of applications, understanding security and a good grounding of basic network administration.

Linux: The Complete Reference is the ultimate guide where you will have the chance to learn how to:

◉ Administer any Linux distribution by installing and configuring them
◉ File and directory administration/manipulation from the BASH, TCSH, and Z shells
◉ Understand and use various desktop environments such as GNOME and KDE desktops, X Windows, and display managers
◉ Understand how to install essential applications such as office, database, connection to the Internet, and manage multimedia applications
◉ Have a good coverage of security by learning SELinux, netfilter, SSH, and Kerberos
◉ Get a good grounding of encryption such as encrypting network transmissions with GPG, LUKS, and IPsec
◉ Acquire skills of deploying FTP, Web, mail, proxy, print, news, and database servers and many more.

Check out the book at: Linux: Linux: The Complete Reference, Sixth Edition

Best LPIC-2 Certification Study Books


After you have had your LPIC-1 Certification and you have had the opportunity to practice your skills in administering systems, time will come when you will need to upgrade your certification level to get the trust from employers to handle more responsibilities in your wonderful career. This section gives you a place to select the resources that will help you climb the ladder stress-free.

1. LPIC-2: Linux Professional Institute Certification Study Guide: Exam 201 and Exam 202, 2nd Edition by Christine Bresnahan


The LPI-level 2 certification confirms your advanced Linux skill set, and the demand for qualified professionals continues to grow.

Christine once again goes over the objectives that LPIC-2 demands and produced this study guide that covers it all 100 percent. This book provides clear and concise coverage of the Linux administration topics you’ll need to know for exams 201 and 202. Crafted to benefit you to not only pass the exams, the examples provided highlight the real-world applications of important concepts, and together, the author team provides insights based on almost fifty years in the IT industry.

This brand new second edition has been completely revamped to align with the latest versions of the exams, with authoritative coverage of the Linux kernel, system startup, advanced storage, network configuration, system maintenance, web services, security, troubleshooting, and more.

You will be ahead by learning a lot for example:

◉ Understand all of the material for both LPIC-2 exams
◉ Gain insight into real-world applications
◉ Test your knowledge with chapter tests and practice exams
◉ Access online study aids for more thorough preparation

All this and more awaits you when you purchase your copy on the link below:

LPIC-2: Linux Professional Institute Certification Study Guide: Exam 201 and Exam 202, 2nd Edition

2. LPIC-2 Linux Professional Institute Certification Study Guide: Exams 201 and 202 1st Edition by Roderick W. Smith


Compiled from a long period of Linux exposure and confident experiences of Roderick, this book picks up from your LPIC-1 journey and takes you a step at a time to master the advanced stuff hidden in Linux. You will be sure to get satisfied by the manner in which the clear explanations and language has been done. This study guide provides unparalleled coverage of the LPIC-2 objectives for exams 201 and 202. Clear and concise coverage examines all Linux administration topics while practical, real-world examples enhance your learning process. What is more, the book:

◉ Prepares you for exams 201 and 202 of the Linux Professional Institute Certification
◉ Offers clear, concise coverage on exam topics such as the Linux kernel, system startup, networking configuration, system maintenance, domain name server, file sharing, and more
◉ Addresses additional key topics for the exams including network client management, e-mail services, system security, and troubleshooting

There is no other place to look than Amazon for a copy of this book. Click on the link below and you will be taken there

LPIC-2 Linux Professional Institute Certification Study Guide: Exams 201 and 202 1st Edition

3. LPIC-2 Cert Guide: (201-400 and 202-400 exams) (Certification Guide) 1st Edition by William Rothwell


There is so much that one can learn from a veteran or one with so much experience in a given area of expertise especially when they go out of their way to put it all on paper. Expert Linux/Unix instructor William “Bo” Rothwell does this and shares preparation hints and test-taking tips, helping students identify areas of weakness and improve both conceptual knowledge and hands-on skills. Material is presented in a concise manner, focusing on increasing understanding and retention of exam topics such as

◉ Capacity planning
◉ Managing the kernel
◉ Managing system startup
◉ Managing filesystems and devices
◉ Administering advanced storage devices
◉ Configuring the network
◉ Performing system maintenance
◉ Administering Domain Name Server (DNS)
◉ Configuring web services
◉ Administering file sharing
◉ Managing network clients
◉ Administering e-mail services
◉ Administering system security

This can be of real value to your preparation process. If you are inspired, head over to Amazon and take a look at it below:

LPIC-2 Cert Guide: (201-400 and 202-400 exams) (Certification Guide)