Sunday, 29 April 2018

Expr Command Examples in Unix / Linux

LPI Tutorials and Materials, LPI Guides, LPI Certifications, LPI Learning

This continuation to my previous post on bc command - calculator. In this article we will see how to use the expr command in unix or linux system for doing arithmetic operations.

The syntax of expr command is 


expr [expression]

Let see how to use the expr command in unix or linux with examples. Most of the examples are straightforward. I will provide description as and when required.

Note: You have to provide the space between the values and the operands. Otherwise the expr command may throw error or print them as a string.

Arithmetic Operator Examples:


1. Sum of numbers 

$ expr 5 + 3

8

$ expr 1 + 2 + 3

6

$ expr 5+3

5+3

Here in the third expr command, space is not provided between the literals. The expr command treated it as a string and printed on the terminal.

2. Difference between two numbers 

$ expr 10 - 6

4

3. Multiplying numbers 

$ expr 7 \* 9

63

Here the * is shell builtin operator, that is why it needs to escaped with backslash.

4. Dividing numbers 

$ expr 6 / 4

1

The division operator returns only the arithmetic quotient.

5. Remainder or modulus 

$ expr 6 % 4

2

Comparision or Relational Operator Examples: 


You can use the following comparision operators with the expr command:

◈ Val1 < Val2 : Returns 1 if val1 is less than val2. otherwise zero.
◈ Val1 <= Val2 : Returns 1 if val1 is less than or equal to val2. otherwise zero.
◈ Val1 > Val2 : Returns 1 if val1 is greater than val2. otherwise zero.
◈ Val1 >= Val2 : Returns 1 if val1 is greater than or equal to val2. otherwise zero.
◈ Val1 = Val2 : Returns 1 if val1 is equal to val2. otherwise zero.
◈ Val1 != Val2 : Returns 1 if val1 is equal to val2. otherwise zero.
◈ val1 | val2 : Returns val1 if val1 is neither null nor zero. Otherwise val2.
◈ val1 & val2 : Returns val1 if both val1 and val2 is neither null nor zero. Otherwise 0.

Note: You have to escape most of the operators with backslash as they are shell built in.

$ expr 1 \< 2

1

$ expr 1 \<= 1

1

$ expr 2 \> 5

0

$ expr 2 \>= 5

0

$ expr 7 = 7

1

$ expr 9 != 18

1

$ expr 2 \| 5

2

$ expr 0 \| 5

5

$ expr 2 \& 5

2

$ expr 6 \& 3

6

$ expr 6 \& 0

0

$ expr 0 \& 3

0

String Function Examples: 


1. Length of string

The length function is used to find the number of characters in a string.

$ expr length linux

5

$expr length linux\ system

12

$expr length "linux system"

If you have spaces in your string escape them with backslash or quote them with double quotes.

2. Find Substring 

You can extract a portion of the string by using the substr function. The syntax of substr function is

substr string position length

Here position is the character position in the string. length is the number of chracters to extract from the main string. An example is shown below:

$ expr substr unixserver 5 6
server

3. Index of the substring 

You can find the position of a string in the main string using the index function. The syntax of index function is shown below:

index string chars

If the chars string is found in the main string, then the index function returns the position of the chars. Otherwise it returns 0. See the following examples:

$ expr index linux nux
3

$expr index linux win
0

4. Matching a regexp 

The match function is used to find anchored pattern match of regexp in the string. The syntax of match function is shown below:

match string pattern

The match function returns the number of characters in the pattern is a match is found. Otherwise, it returns 0. Alternative synatx is

string : pattern

The following examples shows how to use the match function:

$ expr match linuxserver lin
3

$ expr match linuxserver server
0

Here in the second expr, the pattern (server) exists in the main string. However the pattern does not start from the beggining of the main string. Thats why the match function returns 0.

Friday, 27 April 2018

SSH/Zip Command Examples in Unix / Linux

Linux, Linux Command, SSH, SSH Command, UNIX, Unix Command, Zip, Zip Command

SSH Command 


SSH client utility in unix or linux server is used to logging into a remote host and execute commands on the remote machine. The rlogin and rsh commands can also be used to login into the remote machine. However these are not secure. The ssh command provides a secure connection between two hosts over a insecure network.

The syntax ssh command is


ssh [-l username] hostname | user@remote-hostname [command]

Let see the examples of ssh command.

SSH Command Examples:


1. Logging to a remote server

You can login to a remote server from the local host as shown below:

localhost:[~]> ssh -l username remote-server
username@remote-server password:
remote-server:[~]>

Alternatively you can use the below ssh command for connecting to remote host:

localhost:[~]> ssh username@remote-server
username@remote-server password:
remote-server:[~]>

Note: If you are logging for the first time, then it will prints a message that host key not found and you can give yes to continue. The host key of the remote server will be cached and added to the .ssh2/hostkeys directory in your home directory. From second time onwards you just need to enter the password.

2. Logging out from remote server

Simply enter the exit command on the terminal to close the connection. This is shown below:

remote-server:[~]>exit
logout
Connection to remote-server closed.
localhost:[~]>

3. Running remote commands from local host

Sometimes it is necessary to run the unix commands on the remote server from the local host. An example is shown below:

localhost:[~]> ssh user@remote-host "ls test"
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

The ssh command connects to the remote host, runs the ls command, prints the output on the local host terminal and exits the connection from remote host.

Let see whether the ls command actually displayed the correct result or not by connecting to the remote host.

localhost:[~]> ssh user@remote-host
user@remotehost password:
remotehost:[~]> cd test
remotehost:[~/test]> ls
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

4. Version of the SSH command

We can find the version of SSH installed on the unix system using the -V option to the ssh. This is shown below:

> ssh -V
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008

5. Debugging the SSH Client

When we are not able to connect to the remote host, it is good to debug and find the exact error messages that causing the issue. Use the -v option for debugging the ssh client.

ssh -v user@remote-host
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to remote-host [172.22.200.140] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/identity type -1
debug1: identity file /home/user/.ssh/id_rsa type -1
debug1: identity file /home/user/.ssh/id_dsa type 2
debug1: loaded 3 keys
..........
..........

6. Copying files between remote host and local host.

We can use the scp command to copy the files securely between the local host and remote host using the ssh authentication.

To copy the file from local host to remote hosts /var/tmp/ directory, run the below scp command.

scp filename user@remote-host:/var/tmp/

To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, run the below scp command.

scp user@remote-host:/usr/local/bin/add.sh.

Zip Command


Zip is used to compress the files to reduce file size and also used as file package utility. zip is available in many operating systems like unix, linux, windows etc.

If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer.

The syntax of zip command is


zip [options] zipfile files_list

The options of zip command are:

-d : Removes the file from the zip archive
-u : Updates the file in the zip archive
-m : Deletes the original files after zipping.
-r : Recursively zips the files in a directory
-x : Exclude the files in creating the zip
-v : verbose mode
-1 : Compresses the files faster
-9 : Compresses the files better
-f : freshen only changed files.
zipfile : creates the zip file with name as zipfile.zip
files_list : list of files to be zipped.

Zip Command Examples:


The files in my current directory are listed below:

docs/linux.pdf
docs/oracle.pdf
docs/unix.pdf
linux-virtual-server.bat
unix-server.dat

Here docs is a directory which contains the files linux.pdf, unix.pdf and oracle.pdf. We will see how to use zip command with examples.

1. Creating a zip file

The zip command in unix or linux system creates an archive with the specified files. This is shown below:

> zip archive linux-virtual-server.bat unix-server.dat
  adding: linux-virtual-server.bat (deflated 80%)
  adding: unix-server.dat (deflated 80%)
> ls
archive.zip  docs  linux-virtual-server.bat  unix-server.dat

The above command creates the zip file with name archive.zip

2. Extracting files from zip

To extract files from the zip, use the unzip command in unix system. This is shown below:

> unzip archive.zip
Archive:  archive.zip
  inflating: linux-virtual-server.bat
  inflating: unix-server.dat
> ls
archive.zip  linux-virtual-server.bat  unix-server.dat

3. Removing file from a zip file

After creating a zip file, you can remove a file from the archive using the -d option. To remove the file unix-server.dat from the archive, run the below zip command:

> zip -d archive.zip unix-server.dat
deleting: unix-server.dat

> unzip archive.zip
Archive:  archive.zip
  inflating: linux-virtual-server.bat

4. Update existing zip file

You can update the files in already created zip file. If any of the files are modified after zipping, you can fresh the zip file with only those modified files using the -f option.

> zip -f archive.zip
freshening: linux-virtual-server.bat (stored 0%)

Another way is using the -u option. This option can be used to update the specified list of files or add new files to the existing zip file.

> zip -u archive.zip  linux-virtual-server.bat temp
updating: linux-virtual-server.bat (deflated 79%)
  adding: temp (stored 0%)

5. Recursively zip files in directory.

To zip a directory recursively, use the -r option with the zip command. This example is shown below:

> zip -r dir_archive docs
  adding: docs/ (stored 0%)
  adding: docs/unix.pdf (stored 0%)
  adding: docs/oracle.pdf (stored 0%)
  adding: docs/linux.pdf (stored 0%)

6. Excluding files in zipping

Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.

zip exclude_archive * -x linux-virtual-server.bat

The above command zips all the files in the current directory except the file linux-virtual-server.bat

7. Faster compressing

You can compress the files very fast using the -1 option with zip command. An example is shown below with and without using fast compression.

> zip -1 fast_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 79%)

>zip normal_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 80%)

If you use fast compression, the archive file created will occupy more space (size) when compared to normal compression.

8. Better compression.

To reduce more amount of size the files occupied, you can use the -9 option with the zip command. This gives a better compression.

> zip -9 better_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 81%)

Compare the deflated percentages in the example 7 and 8.

Wednesday, 25 April 2018

Join Command in Unix/Linux Examples

Join Command, Linux Command, Unix Command, Command Line

Join command is one of the text processing utility in Unix/Linux. Join command is used to combine two files based on a matching fields in the files. If you know SQL, the join command is similar to joining two tables in a database.

The syntax of join command is

join [options] file1 file2

The join command options are

-1 field number : Join on the specified field number in the first file
-2 field number : Join on the specified field number in the second file
-j field number : Equivalent to -1 fieldnumber and -2 fieldnumber
-o list : displays only the specified fields from both the files
-t char : input and output field delimiter
-a filenumber : Prints non matched lines in a file
-i : ignore case while joining

Unix Join Command Examples


1. Write a join command to join two files on the first field?

The basic usage of join command is to join two files on the first field. By default the join command matches the files on the first fields when we do not specify the field numbers explicitly. Let's say we have two files emp.txt and dept.txt

> cat emp.txt
10 mark
10 steve
20 scott
30 chris
> cat dept.txt
10 hr
20 finance
30 db

Here we will join on the first field and see the output. By default, the join command treats the field delimiter as space or tab.

> join emp.txt dept.txt
10 mark hr
10 steve hr
20 scott finance
30 chris db

Important Note: Before joining the files, make sure to sort the fields on the joining fields. Otherwise you will get incorrect result.

2. Write a join command to join the two files? Here use the second field from the first file and the first field from the second file to join.

In this example, we will see how to join two files on different fields rather than the first field. For this consider the below two files as an example

> cat emp.txt
mark 10 1
steve 10 1
scott 20 2
chris 30 3
> cat dept.txt
10 hr 1
20 finance 2
30 db 3

From the above, you can see the join fields are the second field from the emp.txt and the first field from the dept.txt. The join command to match these two files is

> join -1 2 -2 1 emp.txt dept.txt
10 mark 1 hr 1
10 steve 1 hr 1
20 scott 2 finance 2
30 chris 3 db 3

You can also see that the two files can also be joined on the third filed. As the both the files have the matching join field, you can use the j option in the join command.

Here -1 2 specifies the second field from the first file (emp.txt) and -2 1 specifies the first field from the second file (dept.txt)

> join -j 3 emp.txt dept.txt
1 mark 10 10 hr
1 steve 10 10 hr
2 scott 20 20 finance
3 chris 30 30 db

3. Write a join command to select the required fields from the input files in the output? Select first filed from first file and second field from second file in the output.

By default, the join command prints all the fields from both the files (except the join field is printed once). We can choose what fields to be printed on the terminal with the -o option. We will use the same files from the above example.

> join -o 1.1 2.2 -1 2 -2 1 emp.txt dept.txt
mark hr
steve hr
scott finance
chris db

Here 1.1 means in the first file select the first field. Similarly, 2.2 means in the second file select the second field

4. Write a command to join two delimited files? Here the delimiter is colon (:)

So far we have joined files with space delimiter. Here we will see how to join files with a colon as delimiter. Consider the below two files.

> cat emp.txt
mark:10
steve:10
scott:20
chris:30
> cat dept.txt
10:hr
20:finance
30:db

The -t option is used to specify the delimiter. The join command for joining the files is

> join -t: -1 2 -2 1 emp.txt dept.txt
10:mark:hr
10:steve:hr
20:scott:finance
30:chris:db

5. Write a command to ignore case when joining the files?

If the join fields are in different cases, then the join will not be performed properly. To ignore the case in join use the -i option.

> cat emp.txt
mark,A
steve,a
scott,b
chris,C
> cat dept.txt
a,hr
B,finance
c,db

> join -t, -i -1 2 -2 1 emp.txt dept.txt
A,mark,hr
a,steve,hr
b,scott,finance
C,chris,db

6. Write a join command to print the lines which do not match the values in joining fields?

By default the join command prints only the matched lines from both the files which means prints the matched lines that passed the join condition. We can use the -a option to print the non-matched lines.

> cat P.txt
A 1
B 2
C 3
> cat Q.txt
B 2
C 3
D 4

Print non pairable lines from first file.

> join -a 1 P.txt Q.txt
A 1
B 2 2
C 3 3

Print non pairable lines from second file.

> join -a 2 P.txt Q.txt
B 2 2
C 3 3
D 4

Print non pairable lines from both file.

> join -a 1 -a 2 P.txt Q.txt
A 1
B 2 2
C 3 3
D 4

Saturday, 21 April 2018

Objectives: LPIC-2 Exam 201 (Linux Engineer)

LPIC-2 Exam 201, Linux Engineer, LPIC-2 Certifications, LPIC-2

Topic 200: Capacity Planning


200.1 Measure and Troubleshoot Resource Usage

Weight: 6

Description: Candidates should be able to measure hardware resource and network bandwidth, identify and troubleshoot resource problems.

Key Knowledge Areas:

◈ Measure CPU usage
◈ Measure memory usage
◈ Measure disk I/O
◈ Measure network I/O
◈ Measure firewalling and routing throughput
◈ Map client bandwidth usage
◈ Match / correlate system symptoms with likely problems
◈ Estimate throughput and identify bottlenecks in a system including networking

The following is a partial list of the used files, terms and utilities:

◈ iostat
◈ netstat
◈ w
◈ top
◈ sar
◈ processes blocked on I/O
◈ blocks out
◈ vmstat
◈ pstree, ps
◈ Isof
◈ uptime
◈ swap
◈ blocks in

200.2 Predict Future Resource Needs

Weight: 2

Description: Candidates should be able to monitor resource usage to predict future resource needs.

Key Knowledge Areas:

◈ Use monitoring and measurement tools to monitor IT infrastructure usage.
◈ Predict capacity break point of a configuration
◈ Observe growth rate of capacity usage
◈ Graph the trend of capacity usage
◈ Awareness of monitoring solutions such as Icinga2, Nagios, collectd, MRTG and Cacti

The following is a partial list of the used files, terms and utilities:

◈ diagnose
◈ predict growth
◈ resource exhaustion

Topic 201: Linux Kernel


201.1 Kernel Components

Weight: 2

Description: Candidates should be able to utilize kernel components that are necessary to specific hardware, hardware drivers, system resources and requirements. This objective includes implementing different types of kernel images, identifying stable and development kernels and patches, as well as using kernel modules.

LPIC-2 Exam 201, Linux Engineer, LPIC-2 Certifications, LPIC-2

Key Knowledge Areas:

◈ Kernel 2.6.x, 3.x and 4.x documentation

Terms and Utilities:

◈ /usr/src/linux/
◈ /usr/src/linux/Documentation/
◈ zImage
◈ bzImage
◈ xz compression

201.2 Compiling a kernel

Weight: 3

Description: Candidates should be able to properly configure a kernel to include or disable specific features of the Linux kernel as necessary. This objective includes compiling and recompiling the Linux kernel as needed, updating and noting changes in a new kernel, creating an initrd image and installing new kernels.

Key Knowledge Areas:

◈ /usr/src/linux/
◈ Kernel Makefiles
◈ Kernel 2.6.x/3.x make targets
◈ Customize the current kernel configuration.
◈ Build a new kernel and appropriate kernel modules.
◈ Install a new kernel and any modules.
◈ Ensure that the boot manager can locate the new kernel and associated files.
◈ Module configuration files
◈ Use DKMS to compile kernel modules.
◈ Awareness of dracut

Terms and Utilities:

◈ mkinitrd
◈ mkinitramfs
◈ make
◈ make targets (all, config, xconfig, menuconfig, gconfig, oldconfig, mrproper, zImage, bzImage, modules, modules_install, rpm-pkg, binrpm-pkg, deb-pkg)
◈ gzip
◈ bzip2
◈ module tools
◈ /usr/src/linux/.config
◈ /lib/modules/kernel-version/
◈ depmod
◈ dkms

201.3 Kernel runtime management and troubleshooting

Weight: 4

Description: Candidates should be able to manage and/or query a 2.6.x, 3.x or 4.x kernel and its loadable modules. Candidates should be able to identify and correct common boot and run time issues. Candidates should understand device detection and management using udev. This objective includes troubleshooting udev rules.

Key Knowledge Areas:

◈ Use command-line utilities to get information about the currently running kernel and kernel modules
◈ Manually load and unload kernel modules
◈ Determine when modules can be unloaded
◈ Determine what parameters a module accepts
◈ Configure the system to load modules by names other than their file name.
◈ /proc filesystem
◈ Content of /, /boot/ , and /lib/modules/
◈ Tools and utilities to analyze information about the available hardware
◈ udev rules

Terms and Utilities:

◈ /lib/modules/kernel-version/modules.dep
◈ module configuration files in /etc/
◈ /proc/sys/kernel/
◈ /sbin/depmod
◈ /sbin/rmmod
◈ /sbin/modinfo
◈ /bin/dmesg
◈ /sbin/lspci
◈ /usr/bin/lsdev
◈ /sbin/lsmod
◈ /sbin/modprobe
◈ /sbin/insmod
◈ /bin/uname
◈ /usr/bin/lsusb
◈ /etc/sysctl.conf, /etc/sysctl.d/
◈ /sbin/sysctl
◈ udevmonitor
◈ udevadm monitor
◈ /etc/udev/

Topic 202: System Startup


202.1 Customizing SysV-init system startup

Weight: 3

Description: Candidates should be able to query and modify the behaviour of system services at various targets / run levels. A thorough understanding of the systemd, SysV Init and the Linux boot process is required. This objective includes interacting with systemd targets and SysV init run levels.

Key Knowledge Areas:

◈ Systemd
◈ SysV init
◈ Linux Standard Base Specification (LSB)

Terms and Utilities:

◈ /usr/lib/systemd/
◈ /etc/systemd/
◈ /run/systemd/
◈ systemctl
◈ systemd-delta
◈ /etc/inittab
◈ /etc/init.d/
◈ /etc/rc.d/
◈ chkconfig
◈ update-rc.d
◈ init and telinit

202.2 System Recovery

Weight: 4

Description: Candidates should be able to properly manipulate a Linux system during both the boot process and during recovery mode. This objective includes using both the init utility and init-related kernel options. Candidates should be able to determine the cause of errors in loading and usage of bootloaders. GRUB version 2 and GRUB Legacy are the bootloaders of interest. Both BIOS and UEFI systems are covered.

Key Knowledge Areas:

◈ BIOS and UEFI
◈ NVMe booting
◈ GRUB version 2 and Legacy
◈ grub shell
◈ boot loader start and hand off to kernel
◈ kernel loading
◈ hardware initialisation and setup
◈ daemon/service initialisation and setup
◈ Know the different boot loader install locations on a hard disk or removable device.
◈ Overwrite standard boot loader options and using boot loader shells.
◈ Use systemd rescue and emergency modes.

Terms and Utilities:

◈ mount
◈ fsck
◈ inittab, telinit and init with SysV init
◈ The contents of /boot/, /boot/grub/ and /boot/efi/
◈ EFI System Partition (ESP)
◈ GRUB
◈ grub-install
◈ efibootmgr
◈ UEFI shell
◈ initrd, initramfs
◈ Master boot record
◈ systemctl

202.3 Alternate Bootloaders

Weight: 2

Description: Candidates should be aware of other bootloaders and their major features.

Key Knowledge Areas:

◈ SYSLINUX, ISOLINUX, PXELINUX
◈ Understanding of PXE for both BIOS and UEFI
◈ Awareness of systemd-boot and U-Boot

Terms and Utilities:

◈ syslinux
◈ extlinux
◈ isolinux.bin
◈ isolinux.cfg
◈ isohdpfx.bin
◈ efiboot.img
◈ pxelinux.0
◈ pxelinux.cfg/
◈ uefi/shim.efi
◈ uefi/grubx64.efi

Topic 203: Filesystem and Devices


203.1 Operating the Linux filesystem

Weight: 4

Description: Candidates should be able to properly configure and navigate the standard Linux filesystem. This objective includes configuring and mounting various filesystem types.

Key Knowledge Areas:

◈ The concept of the fstab configuration
◈ Tools and utilities for handling swap partitions and files
◈ Use of UUIDs for identifying and mounting file systems
◈ Understanding of systemd mount units

Terms and Utilities:

◈ /etc/fstab
◈ /etc/mtab
◈ /proc/mounts
◈ mount and umount
◈ blkid
◈ sync
◈ swapon
◈ swapoff

203.2 Maintaining a Linux filesystem​

Weight: 3

Description: Candidates should be able to properly maintain a Linux filesystem using system utilities. This objective includes manipulating standard filesystems and monitoring SMART devices.

Key Knowledge Areas:

◈ Tools and utilities to manipulate and ext2, ext3 and ext4
◈ Tools and utilities to perform basic Btrfs operations, including subvolumes and snapshots
◈ Tools and utilities to manipulate XFS
◈ Awareness of ZFS

Terms and Utilities:

◈ mkfs (mkfs.*)
◈ mkswap
◈ fsck (fsck.*)
◈ tune2fs, dumpe2fs and debugfs
◈ btrfs, btrfs-convert
◈ xfs_info, xfs_check, xfs_repair, xfsdump and xfsrestore
◈ smartd, smartctl

203.3 Creating and configuring filesystem options

Weight: 2

Description: Candidates should be able to configure automount filesystems using AutoFS. This objective includes configuring automount for network and device filesystems. Also included is creating filesystems for devices such as CD-ROMs and a basic feature knowledge of encrypted filesystems.

Key Knowledge Areas:

◈ autofs configuration files
◈ Understanding of automount units
◈ UDF and ISO9660 tools and utilities
◈ Awareness of other CD-ROM filesystems (HFS)
◈ Awareness of CD-ROM filesystem extensions (Joliet, Rock Ridge, El Torito)
◈ Basic feature knowledge of data encryption (dm-crypt / LUKS)

Terms and Utilities:

◈ /etc/auto.master
◈ /etc/auto.[dir]
◈ mkisofs
◈ cryptsetup

Topic 204: Advanced Storage Device Administration


204.1 Configuring RAID

Weight: 3

Description: Candidates should be able to configure and implement software RAID. This objective includes using and configuring RAID 0, 1 and 5.

Key Knowledge Areas:

◈ Software raid configuration files and utilities

Terms and Utilities:

◈ mdadm.conf
◈ mdadm
◈ /proc/mdstat
◈ partition type 0xFD

204.2 Adjusting Storage Device Access

Weight: 2

Description: Candidates should be able to configure kernel options to support various drives. This objective includes software tools to view & modify hard disk settings including iSCSI devices.

Key Knowledge Areas:

◈ Tools and utilities to configure DMA for IDE devices including ATAPI and SATA
◈ Tools and utilities to configure Solid State Drives including AHCI and NVMe
◈ Tools and utilities to manipulate or analyse system resources (e.g. interrupts)
◈ Awareness of sdparm command and its uses
◈ Tools and utilities for iSCSI
◈ Awareness of SAN, including relevant protocols (AoE, FCoE)

Terms and Utilities:

◈ hdparm, sdparm
◈ nvme
◈ tune2fs
◈ fstrim
◈ sysctl
◈ /dev/hd*, /dev/sd*, /dev/nvme*
◈ iscsiadm, scsi_id, iscsid and iscsid.conf
◈ WWID, WWN, LUN numbers

204.3 Logical Volume Manager

Weight: 3

Description: Candidates should be able to create and remove logical volumes, volume groups, and physical volumes. This objective includes snapshots and resizing logical volumes.

Key Knowledge Areas:

◈ Tools in the LVM suite
◈ Resizing, renaming, creating, and removing logical volumes, volume groups, and physical volumes
◈ Creating and maintaining snapshots
◈ Activating volume groups

Terms and Utilities:

◈ /sbin/pv*
◈ /sbin/lv*
◈ /sbin/vg*
◈ mount
◈ /dev/mapper/
◈ lvm.conf

Topic 205: Network Configuration


205.1 Basic networking configuration

Weight: 3

Description: Candidates should be able to configure a network device to be able to connect to a local, wired or wireless, and a wide-area network. This objective includes being able to communicate between various subnets within a single network including both IPv4 and IPv6 networks.

Key Knowledge Areas:

Utilities to configure and manipulate ethernet network interfaces
Configuring basic access to wireless networks

Terms and Utilities:

◈ ip
◈ ifconfig
◈ route
◈ arp
◈ iw
◈ iwconfig
◈ iwlist

205.2 Advanced Network Configuration and Troubleshooting

Weight: 4

Description: Candidates should be able to configure a network device to implement various network authentication schemes. This objective includes configuring a multi-homed network device and resolving communication problems.

Key Knowledge Areas:

◈ Utilities to manipulate routing tables
◈ Utilities to configure and manipulate ethernet network interfaces
◈ Utilities to analyze the status of the network devices
◈ Utilities to monitor and analyze the TCP/IP traffic

Terms and Utilities:

◈ ip
◈ ifconfig
◈ route
◈ arp
◈ ss
◈ netstat
◈ lsof
◈ ping, ping6
◈ nc
◈ tcpdump
◈ nmap

205.3 Troubleshooting Network Issues

Weight: 4

Description: Candidates should be able to identify and correct common network setup issues, to include knowledge of locations for basic configuration files and commands.

Key Knowledge Areas:

◈ Location and content of access restriction files
◈ Utilities to configure and manipulate ethernet network interfaces
◈ Utilities to manage routing tables
◈ Utilities to list network states.
◈ Utilities to gain information about the network configuration
◈ Methods of information about the recognized and used hardware devices
◈ System initialization files and their contents (SysV init process)
◈ Awareness of NetworkManager and its impact on network configuration

Terms and Utilities:

◈ ip
◈ ifconfig
◈ route
◈ ss
◈ netstat
◈ /etc/network/, /etc/sysconfig/network-scripts/
◈ ping, ping6
◈ traceroute, traceroute6
◈ mtr
◈ hostname
◈ System log files such as /var/log/syslog, /var/log/messages and the systemd journal
◈ dmesg
◈ /etc/resolv.conf
◈ /etc/hosts
◈ /etc/hostname, /etc/HOSTNAME
◈ /etc/hosts.allow, /etc/hosts.deny

Topic 206: System Maintenance


206.1 Make and install programs from source

Weight: 2

Description: Candidates should be able to build and install an executable program from source. This objective includes being able to unpack a file of sources.

Key Knowledge Areas:

◈ Unpack source code using common compression and archive utilities
◈ Understand basics of invoking make to compile programs
◈ Apply parameters to a configure script
◈ Know where sources are stored by default

Terms and Utilities:

◈ /usr/src/
◈ gunzip
◈ gzip
◈ bzip2
◈ xz
◈ tar
◈ configure
◈ make
◈ uname
◈ install
◈ patch

206.2 Backup operations

Weight: 3

Description: Candidates should be able to use system tools to back up important system data.

Key Knowledge Areas:

◈ Knowledge about directories that have to be include in backups
◈ Awareness of network backup solutions such as Amanda, Bacula, Bareos and BackupPC
◈ Knowledge of the benefits and drawbacks of tapes, CDR, disk or other backup media
◈ Perform partial and manual backups.
◈ Verify the integrity of backup files.
◈ Partially or fully restore backups.

Terms and Utilities:

◈ /bin/sh
◈ dd
◈ tar
◈ /dev/st* and /dev/nst*
◈ mt
◈ rsync

206.3 Notify users on system-related issues

Weight: 1

Description: Candidates should be able to notify the users about current issues related to the system.

Key Knowledge Areas:

◈ ​Automate communication with users through logon messages
◈ Inform active users of system maintenance

Terms and Utilities:

◈ /etc/issue
◈ /etc/issue.net
◈ /etc/motd
◈ wall
◈ /sbin/shutdown
◈ systemctl

Friday, 20 April 2018

Delete Directory, Files - rm, rmdir command in Unix / Linux

rm, rmdir command, Unix Command, Linux Command

How to delete directories and files in unix/linux

Unix provides rmdir and rm commands to remove the directories and files. Let see each command in detail.

Unix rmdir command syntax


The syntax of rmdir command is

rmdir [options] directories

The rmdir command options are

-p : Removes directory and its parent directories
-v : Provides the diagnostic information of the directory processed

Unix rmdir command examples


1. Write a unix/linux command to remove a directory?

The rmdir command deletes only the empty directories. If a directory contains files or sub directories, then the rmdir command fails.

rmdir docs/
rmdir: docs/: Directory not empty

Here the docs directory is not empty, that is why the rmdir command failed to remove the directory. To remove the docs directory first we have to make the directory empty and then delete the directory.

rm doc/*
rmdir docs/

We will see later how to remove non-empty directories with a single command.

2. Write a unix/linux command to remove the directory and its parent directories?

As mentioned earlier the -p option allows the rmdir command to delete the directory and also its parent directories.

rmdir -p docs/entertainment/movies/

This rmdir command removes the docs directory completely. If you don’t use the -p option, then it only deletes the movies directory.

3. Write a unix/linux command to remove directories using pattern matching?

You can specify the directory names using the regular expressions and can delete them.

rm doc*

This rm command deletes the directories like doc, documents, doc_1 etc.

Now we will see the rm command in unix.

Unix rm command syntax


The syntax of rm command is

rm [options] [directory|file]

The rm command options are

f : Removes all files in a directory without prompting the user.
i : Interactive: prompts the user for confirmation before deleting a file.
R or r : Recursively remove directories and sub directories.

The rm command can be used to delete both the files and directories. The rm command also deletes the non-empty directories.

Unix rm command examples


1. Write a unix/linux command to remove a file?

This is the basic feature of rm command. To remove a file, logfile.dat, in the current directory use the below rm command

rm logfile.dat

2. Write a unix/linux command to remove all the files in a directory?

use the * regular pattern as the file list in rm command for deleting all the files in the current directory.

rm *

3. Write a unix/linux command to delete empty directory?

The rm command can also be used to delete the empty directory. The command for this is

rm docs/

If the directory is non-empty, then the above command fails to remove the directories.

4. Write a unix/linux command to delete directories recursively (delete non empty directories)?

As mentioned earlier, the -r option can be used to remove the directories and sub directories.

rm -r docs

This removes the docs directory even if it is non-empty.

Saturday, 14 April 2018

Grep Command in Unix and Linux Examples

Grep Command, Unix Command and Linux Command

Grep is the frequently used command in Unix (or Linux). Most of us use grep just for finding the words in a file. The power of grep comes with using its options and regular expressions. You can analyze large sets of log files with the help of grep command.

Grep stands for Global search for Regular Expressions and Print.

The basic syntax of grep command is


grep [options] pattern [list of files]

Let see some practical examples on grep command.

1. Running the last executed grep command

This saves a lot of time if you are executing the same command again and again.

!grep

This displays the last executed grep command and also prints the result set of the command on the terminal.

2. Search for a string in a file

This is the basic usage of grep command. It searches for the given string in the specified file.

grep "Error" logfile.txt

This searches for the string "Error" in the log file and prints all the lines that has the word "Error".

3. Searching for a string in multiple files.

grep "string" file1 file2
grep "string" file_pattern

This is also the basic usage of the grep command. You can manually specify the list of files you want to search or you can specify a file pattern (use regular expressions) to search for.

4. Case insensitive search

The -i option enables to search for a string case insensitively in the give file. It matches the words like "UNIX", "Unix", "unix".

grep -i "UNix" file.txt

5. Specifying the search string as a regular expression pattern.

grep "^[0-9].*" file.txt

This will search for the lines which starts with a number. Regular expressions is huge topic and I am not covering it here. This example is just for providing the usage of regular expressions.

6. Checking for the whole words in a file.

By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option to grep makes it match only the whole words.

grep -w "world" file.txt

7. Displaying the lines before the match.

Some times, if you are searching for an error in a log file; it is always good to know the lines around the error lines to know the cause of the error.

grep -B 2 "Error" file.txt

This will prints the matched lines along with the two lines before the matched lines.

8. Displaying the lines after the match.

grep -A 3 "Error" file.txt

This will display the matched lines along with the three lines after the matched lines.

9. Displaying the lines around the match

grep -C 5 "Error" file.txt

This will display the matched lines and also five lines before and after the matched lines.

10. Searching for a sting in all files recursively

You can search for a string in all the files under the current directory and sub-directories with the help -r option.

grep -r "string" *

11. Inverting the pattern match

You can display the lines that are not matched with the specified search sting pattern using the -v option.

grep -v "string" file.txt

12. Displaying the non-empty lines

You can remove the blank lines using the grep command.

grep -v "^$" file.txt

13. Displaying the count of number of matches.

We can find the number of lines that matches the given string/pattern

grep -c "sting" file.txt

14. Display the file names that matches the pattern.

We can just display the files that contains the given string/pattern.

grep -l "string" *

15. Display the file names that do not contain the pattern.

We can display the files which do not contain the matched string/pattern.

grep -L "string" *

16. 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 "string" file.txt

17. Displaying the line numbers.

We can make the grep command to display the position of the line which contains the matched string in a file using the -n option

grep -n "string" file.txt

18. Displaying the position of the matched string in the line

The -b option allows the grep command to display the character position of the matched string in a file.

grep -o -b "string" file.txt

19. 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 "^start" file.txt

20. 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 "end$" file.txt