Tuesday 30 April 2019

chroot command in Linux with examples

Chroot command in Linux/Unix system is used to change the root directory. Every process/command in Linux/Unix like systems has a current working directory called root directory. It changes the root directory for currently running processes as well as its child processes.

A process/command that runs in such a modified environment cannot access files outside the root directory. This modified environment is known as “chroot jail” or “jailed directory”. Some root user and privileged process are allowed to use chroot command.

Chroot Command, Linux Tutorials and Materials, Linux Study Materials

“chroot” command can be very useful:

◈ To create a test environment.
◈ To recover the system or password.
◈ To reinstall the bootloader.

Syntax:


chroot /path/to/new/root command

OR

chroot /path/to/new/root /path/to/server

OR

chroot [options] /path/to/new/root /path/to/server

Options:


◈ –userspec=USER:GROUP : This option describe the user and group which is to be used. Either name or numeric ID can be used to specify the user and group.

◈ –groups=G_LIST : It describe the supplementary groups as g1,g2,..,gN.

◈ –help : Shows the help message, and exit.

◈ –version : Gives version information, and exit.

Example:


◈ Step 1: We will create a mini-jail with bash and basic commands only. Let’s create a “jail” directory inside the “home” directory, which will be our new root.
$ mkdir $HOME/jail

◈ Step 2: Create directories inside “$HOME/jail”:
$ mkdir -p $HOME/jail/{bin, lib64}
$ cd $HOME/jail

◈ Step 3: Copy /bin/bash and /bin/ls into $HOME/jail/bin/ location using cp command:
$ cp -v /bin/{bash, ls} $HOME/jail/bin

◈ Step 4: Use ldd command to print shared libraries:
$ ldd /bin/bash

Chroot Command, Linux Tutorials and Materials, Linux Study Materials

◈ Step 5: Copy required libraries into $HOME/jail/lib64/ location using cp command:
cp -v libraries/displayed/by/above/command $HOME/jail/lib64
Similarly, copy the libraries of ls command into $HOME/jail/lib64 location.

◈ Step 6: Finally, chroot into your mini-jail:
$ sudo chroot $HOME/jail /bin/bash

Now user sees $HOME/jail directory as its root directory. This is a great boost in the security.

Saturday 27 April 2019

depmod command in Linux with examples

depmod(Dependency Modules) command is used to generate a list of dependency description of kernel modules and its associated map files. This analyzes the kernel modules in the directory /lib/modules/kernel-release and creates a “Makefile”-like dependency file named modules.dep based on the symbols present in the set of modules. These modules are generally taken from the directories specified in the configuration file or mentioned on the command line. Then when the stack of modules are added and removed automatically with modprobe, no modules are without the other related modules they require. Simultaneously, it creates an associated map correlating the hardware identifiers and the corresponding modules that handle them for the purpose of use by the hotplug infrastructure. This specifically associated mapping is used to search for and find the correct module when a unit of hardware requests it.

Depmod Command, LPI Tutorials and Materials, LPI Certifications, LPI Study Materials

The Linux kernel relies on depmod and modprobe to send the raw facts of data for its modules, in the proper order to load them. The depmod and modprobe command utilities facilitate a Linux modular kernel manageable for all end users, distribution maintenance engineers, network and system administrators.

Syntax:


depmod [ -a ] [ -b basedir ] [ -e ] [ -F System.map ] [ -n ] [ -v ] [ version ] [ -A ]
       [-n] [-v] [-A] [-P prefix] [-w] [version]

depmod [-e] [-E Module.symvers] [-F System.map] [-m] [-n] [-v] [-P prefix]
       [-w] [version] [filename...][Tex]

Linux kernel modules would provide unique services called “symbols” for other modules to make use of its utilities. This could be done by using one of the EXPORT_SYMBOL variants in the code. Eventually when a second module uses this symbol, then the second module solely depends on the attributes and functions of the first module. These modular dependencies tend to look complex.
The main objective of the depmod command is to creates a list of module dependencies by revoking each module under /lib/modules/kernel-release and finds what export symbols are used to quantify its needs. By default, this list is written to modules.dep, and also made available in binary hashed version named modules.dep.bin, in the same specific directory.

◈ depmod command also creates a list of symbols provided by modules in the file named modules.symbols.

◈ depmod command creates a list of symbols in modules by its binary hashed version, modules.symbols.bin.

◈ depmod will save the output of a file named modules.devname if modules supply unique device names (devname) that should be populated in /dev on boot by a specific utility such as udev.

Options:


◈ -a, –all: Thoroughly probes and examines all of the modules in the kernel. By default, this option is enabled if no file names are given in the command-line.

◈ -A, –quick: This option scans and finds to see if any modules are new one than those in the modules.dep file before any work is done: if found not, it smoothly exits rather than regenerating the files again.

◈ -b basedir, –basedir, basedir: If the modules are not currently in the default directory /lib/modules/kernel-version, but in some other staging area, we can specify a basedir that make a landing to the directory name. This basedir is stripped from the resulting modules.dep file, so it is always ready to be moved into the default location /lib/modules/kernel-version. A distribution vendor who needs to pre-generate the meta-data files rather than running depmod again later would find this option very useful.

◈ -C, –config file-or-directory: This particular option overrides the default configuration directory at /etc/depmod.d/.

◈ -e, –errsyms: When combined with the -F option, this reports any specific symbols which a module particularly needs that are not absolutely supplied by other modules or the kernel. By default, any symbols that not provided by the modules are assumed to be provided by the kernel, but this presumption can be void when additionally updated third party drivers are not properly installed or were built with errors.

◈ -E, –symvers: depmod when combined with the -e option, reports any symbol versions supplied by modules that do not match with the symbol versions provided by the kernel in its Module.symvers. This option is mutually incompatible with -F option.

◈ -F, –filesyms System.map: Supplied with the System.map produced when the kernel was built, this allows the -e option to report unresolved and unidentified symbols. This option is mutually incompatible with -E.

◈ -h, –help: This option tends to print the help message and exit.

◈ -n, –show: This option displays the dependency file on stdout instead of in the /lib/modules tree.

◈ -q, –quiet: This option instructs depmod to keep quiet and not to complain about missing symbols.

◈ -r, –root: This option help some users who compile modules under a non-root userid, then install the modules as root. This process can leave the modules owned by the non-root userid which is prone to attacks by the intruders, even though the modules directory is owned by root. If the non-root userid is compromised at one stance, an intruder can overwrite the existing modules owned by that non-root userid and use this exposure to bootstrap up to root access. Normally, the modutils will reject attempts to use a module that is not owned by root. Specifying-r will override the error and allow root to load modules that are not owned by root. Use of -r is a major security exposure and is not recommended since the module is prone to attacks.

◈ -s, –syslog: This option displays all error messages via the syslog daemon instead of stderr.

◈ -u, –unresolved-error: depmod command does not set a return code when there are any unresolved symbols present in the module. Some distributions want a non-zero return code in modutils but that change might cause problems for users who expect the old behavior. If you want a non-zero return code in depmod specify -u. In latest Linux kernal versions depmod command will silently ignore the -u flag and will always give a non-zero return code for unresolved symbols.

◈ -n, –dry-run: This option sends the resulting modules.dep and the various associated map files to standard output rather than writing them into the module directory.

◈ -P: This option makes some architectures prefix symbols with an extraneous character. This mention a prefix character (for example ‘_’) to ignore.

◈ -v, –verbose: The option verbose mode in depmod will print to standard output all the symbols each module depends on and the particular module’s file name which provides that symbol.

◈ -V, –version: This option tends to display depmod’s version and exit.

◈ -w: This option pops up a Warning message on duplicate dependencies, aliases, symbol versions, etc.

Example: The following is a series of commands that helps to illustrate a usual way to use depmod command in Linux. Each command is prefixed with sudo since each of them requires proper root permissions:

Retriving file from default location:


 ln -s /path/to/your-kernel-module.ko /lib/modules/`uname -r`
/sbin/depmod -a
 modprobe your-kernel-module

Loading and Unloading a File from location other than default location:


$ ln -s lkm.ko /lib/modules/2.6.32-21-generic/
$  depmod -a
$ modprobe lkm
$  modprobe -r lkm
lkm here refers to located or kept at any part of the memory.

Operations Expained line by line:


$ln -s /path/to/your-kernel-module.ko /lib/modules/`uname -r`

ln is used to create a symbolic link to our module file in the directory /lib/modules/kernel-release. The command uname -r, enclosed in back quotes, is executed by the shell and translates to the appropriate string representing our kernel release version.

Note: $depmod -a is an updated dependency list is generated by depmod -a to make sure that the module we’re installing is aware of all existing modules and dependencies. This dependency list will be used by modprobe when installing the module in the third command.

Thursday 25 April 2019

colrm command in Linux with examples

colrm command in Linux is used for editing text in source code files, script files or regular text files. This command removes selected columns from a file. A column is defined as a single character in a line.

Colrm Command, LPI Tutorials and Materials, LPI Certifications, Linux Guides

◈ It always starts at index 1 and not 0.

◈ If both start and end are specified, then the columns between them, including start and end will be removed.

◈ If only one specific column needs to be deleted, then start and end must be the same.

◈ colrm can also take input from stdin.

Syntax:

colrm [start] [stop]

Example 1: Here, we take input from stdin. Here the start and end are different. So all the characters between start and end, including start and end will be removed. Same can be used on a file (See example 2)

colrm command, Linux Command, Linux Certifications, LPI Guides

In the example given below, we take input form stdin. Here the start and end column are same i.e 6. So only the sixth character will be removed.

colrm command, Linux Command, Linux Certifications, LPI Guides

Example 2: Here, we first make a text file using cat command and then use colrm on the file.

colrm command, Linux Command, Linux Certifications, LPI Guides

colrm command, Linux Command, Linux Certifications, LPI Guides

Tuesday 23 April 2019

hdparm command in Linux with Examples

“hdparm“(i.e, hard disk parameter) is one of the command line programs for Linux which is used to handle disk devices and hard disks. With the help of this command, you can get statistics about the hard disk, alter writing intervals, acoustic management, and DMA settings. It can also set parameters related to drive caches, sleep mode, power management, acoustic management, and DMA settings.

Syntax:


hdparm [options] [device]

Note: When no flags are specified, –acdgkmnru is presumed.

Options:


◈ -a : It is used to Get/set enumeration of a section of file system read-ahead which is implemented to enhance the accomplishment of the uninterrupted reads of the files that are enormous in size.

◈ -A : It Disables/enable the IDE drive’s read-look-ahead property that is generally functioning by default.

◈ -b : It is used to Get/set bus state where, (0 == Off, 1 == On, 2 == tristate).

◈ -B : It is used to Set Advanced Power Management (APM) characteristics, but only if the drive can bear it. If its value is low then the APM is violent and if the value is high then it gives finer accomplishment. To disable the APM you need to set the value to 255.

◈ -c : It Query/enable (E)IDE 32-bit I/O support. Here, 32-bit alludes to the transmission of inputs over PCI or VLB bus.

◈ -C : It is used to inspect the ongoing IDE power mode position. The flags -S, -Y, -y, and -Z are used to control IDE power techniques in a skillful manner.

◈ -d : It disables or enables the flag used by “DMA” drive. It operates through the incorporation of drives and PCI.

◈ -D : It enables or disables the on-drive defect managing property.

◈ -E : It Sets SD/DVD drive speed. In order to make it work you need to assign a speed number succeeding the option. Generally, the number used is two or four.

◈ -f : This is used to synchronize and cleanse the buffer cache for the device on its outlet. This performance can be executed as a segment of the -t and -T timings.

◈ -g : This is used to unveil the configuration of the drive, the expanse of the drive, and the starting offset of the device from the starting point of the drive.

◈ -h : It display the help message and exit.

◈ -i : This unveils the recognition data which was acquired from the drive at the boot time.

◈ -I : This seeks the recognition data straight from the drive and it shows more features than the -i flag.

◈ -k : This helps to get/set the keep_settings_over_reset flag for the drive.

◈ -K : This Sets the drive’s keep_features_over_reset flag. This feature is not provided by all the drives.

◈ -m : It is used to get/set sector count for multiple sectors I/O on the drive. To disable this feature you need to set the value to zero.

◈ -M : This helps to Get/set Automatic Acoustic Management (AAM) setting. This feature is experimental and it is not efficiently tested so, one must use it at their own risk.

◈ -n : It is used to get/set “ignore write errors” flag. One should not play with this feature without decoding the source code of the driver first.

◈ -r : It is used to get/set the read-only flag for the device. When this option is set then the write operations are not sanctioned on the device.

◈ -S : It Set the standby timeout for the drive. When the value is set to zero then it is off.

◈ -T : It executes the timing of cache reads for standard and differentiating purposes. It unveils the speed of reading straight from the buffer of the Linux cache in absence of the access to the disk, you need to perform this operation two to three times for better results.

◈ -t : This has the same purpose as -T flag except for that it unveils the speed of reading through the buffer cache to the disk in the absence of any prior caching of the inputs. If the -T flag is also defined here then the rectification done on the aftermath of -T will be encompassed into the consequence appeared at the time of -t operation.

◈ -u : It is used to get/set the interrupt-unmask flag for the drive when the value is set to one then the driver could unmask other interventions which occur in the processing of disk interrupts. This property can cause enormous file system corruption so, use at your own risk.

◈ -v : This unveils all the settings, except -i.

◈ -w : It helps in the device reset.

◈ -W : It Disables/enable the IDE drive’s write-caching characteristics.

◈ -y : It forces an IDE drive to immediately enter the low power consumption standby mode, usually causing it to spin down. The current power mode status can be checked using the -C flag.

◈ -Y : This pressurizes an IDE to interrupt into the low power consumption sleep mode, in order to shut down it completely, here the current power mode status can be detected with the help of -C flag.

◈ -z : This forces kernel re-read of the partition table for the designated device.

◈ -Z : This disables the automatic power-saving function.


Examples:


Command to display information of the hard drive: It is one of the most significant features as it unveils details of the hard disk drive, you need to use -I option and hard disk drive here.
$ hdparm -I /dev/sda


◈ Command to display all the options:
$ hdparm -h


◈ Command to test hard disk drive speed:
$ hdparm -t /dev/vdb


◈ Command to measure hard disk cache read speed:
$ hdparm -T /dev/vdb

Output:


◈ Command to Enable read-ahead:
$ hdparm -A 1 /dev/sda

◈ Command to switch the drive to the lowest degree of power management:
$ hdparm -B 254 /dev/sda

◈ Command to get current settings:
$ hdparm -d /dev/sdX

◈ Command to set DMA on for a device:
$ hdparm -d1 /dev/hda

◈ Command to lower the noise created by some classical hard disk by lowering disk performance:
$ hdparm -M 128 /dev/sda

Saturday 20 April 2019

File Management - Unix / Linux

We will discuss in detail about file management in Unix. All data in Unix is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem.

File Management, Linux Tutorials and Materials, Linux Guides, Linux Study Materials

When you work with Unix, one way or another, you spend most of your time working with files. This Blog will help you understand how to create and remove files, copy and rename them, create links to them, etc.

In Unix, there are three basic types of files −

◈ Ordinary Files − An ordinary file is a file on the system that contains data, text, or program instructions. In this blog, you look at working with ordinary files.

◈ Directories − Directories store both special and ordinary files. For users familiar with Windows or Mac OS, Unix directories are equivalent to folders.

◈ Special Files − Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.

Listing Files


To list the files and directories stored in the current directory, use the following command −

$ls

Here is the sample output of the above command −

$ls

bin        hosts  lib     res.03
ch07       hw1    pub     test_results
ch07.bak   hw2    res.01  users
docs       hw3    res.02  work

The command ls supports the -l option which would help you to get more information about the listed files −

$ls -l
total 1962188

drwxrwxr-x  2 amrood amrood      4096 Dec 25 09:59 uml
-rw-rw-r--  1 amrood amrood      5341 Dec 25 08:38 uml.jpg
drwxr-xr-x  2 amrood amrood      4096 Feb 15  2006 univ
drwxr-xr-x  2 root   root        4096 Dec  9  2007 urlspedia
-rw-r--r--  1 root   root      276480 Dec  9  2007 urlspedia.tar
drwxr-xr-x  8 root   root        4096 Nov 25  2007 usr
drwxr-xr-x  2    200    300      4096 Nov 25  2007 webthumb-1.01
-rwxr-xr-x  1 root   root        3192 Nov 25  2007 webthumb.php
-rw-rw-r--  1 amrood amrood     20480 Nov 25  2007 webthumb.tar
-rw-rw-r--  1 amrood amrood      5654 Aug  9  2007 yourfile.mid
-rw-rw-r--  1 amrood amrood    166255 Aug  9  2007 yourfile.swf
drwxr-xr-x 11 amrood amrood      4096 May 29  2007 zlib-1.2.3
$

Here is the information about all the listed columns −

◈ First Column − Represents the file type and the permission given on the file. Below is the description of all type of files.

◈ Second Column − Represents the number of memory blocks taken by the file or directory.

◈ Third Column − Represents the owner of the file. This is the Unix user who created this file.

◈ Fourth Column − Represents the group of the owner. Every Unix user will have an associated group.

◈ Fifth Column − Represents the file size in bytes.

◈ Sixth Column − Represents the date and the time when this file was created or modified for the last time.

◈ Seventh Column − Represents the file or the directory name.

In the ls -l listing example, every file line begins with a d, -, or l. These characters indicate the type of the file that's listed.

Sr.No Prefix & Description
1 2-
Regular file, such as an ASCII text file, binary executable, or hard link.
b
Block special file. Block input/output device file such as a physical hard drive.
c
Character special file. Raw input/output device file such as a physical hard drive.
d
Directory file that contains a listing of other files and directories. 
l
Symbolic link file. Links on any regular file.
p
Named pipe. A mechanism for interprocess communications. 
s
Socket used for interprocess communication. 


Metacharacters


Metacharacters have a special meaning in Unix. For example, * and ? are metacharacters. We use * to match 0 or more characters, a question mark (?) matches with a single character.

For Example −

$ls ch*.doc

Displays all the files, the names of which start with ch and end with .doc −

ch01-1.doc   ch010.doc  ch02.doc    ch03-2.doc 
ch04-1.doc   ch040.doc  ch05.doc    ch06-2.doc
ch01-2.doc ch02-1.doc c

Here, * works as meta character which matches with any character. If you want to display all the files ending with just .doc, then you can use the following command −

$ls *.doc

Hidden Files


An invisible file is one, the first character of which is the dot or the period character (.). Unix programs (including the shell) use most of these files to store configuration information.

Some common examples of the hidden files include the files −

◈ .profile − The Bourne shell ( sh) initialization script

◈ .kshrc − The Korn shell ( ksh) initialization script

◈ .cshrc − The C shell ( csh) initialization script

◈ .rhosts − The remote shell configuration file

To list the invisible files, specify the -a option to ls −

$ ls -a

.         .profile       docs     lib     test_results
..        .rhosts        hosts    pub     users
.emacs    bin            hw1      res.01  work
.exrc     ch07           hw2      res.02
.kshrc    ch07.bak       hw3      res.03
$

◈ Single dot (.) − This represents the current directory.

◈ Double dot (..) − This represents the parent directory.

Creating Files


You can use the vi editor to create ordinary files on any Unix system. You simply need to give the following command −

$ vi filename

The above command will open a file with the given filename. Now, press the key i to come into the edit mode. Once you are in the edit mode, you can start writing your content in the file as in the following program −

This is unix file....I created it for the first time.....
I'm going to save this content in this file.

Once you are done with the program, follow these steps −

◈ Press the key esc to come out of the edit mode.

◈ Press two keys Shift + ZZ together to come out of the file completely.

You will now have a file created with filename in the current directory.

$ vi filename
$

Editing Files


You can edit an existing file using the vi editor. We will discuss in short how to open an existing file −

$ vi filename

Once the file is opened, you can come in the edit mode by pressing the key i and then you can proceed by editing the file. If you want to move here and there inside a file, then first you need to come out of the edit mode by pressing the key Esc. After this, you can use the following keys to move inside a file −

◈ l key to move to the right side.

◈ h key to move to the left side.

◈ k key to move upside in the file.

◈ j key to move downside in the file.

So using the above keys, you can position your cursor wherever you want to edit. Once you are positioned, then you can use the i key to come in the edit mode. Once you are done with the editing in your file, press Esc and finally two keys Shift + ZZ together to come out of the file completely.

Display Content of a File


You can use the cat command to see the content of a file. Following is a simple example to see the content of the above created file −

$ cat filename
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
$

You can display the line numbers by using the -b option along with the cat command as follows −

$ cat -b filename
1   This is unix file....I created it for the first time.....
2   I'm going to save this content in this file.
$

Counting Words in a File


You can use the wc command to get a count of the total number of lines, words, and characters contained in a file. Following is a simple example to see the information about the file created above −

$ wc filename
2  19 103 filename
$

Here is the detail of all the four columns −

◈ First Column − Represents the total number of lines in the file.

◈ Second Column − Represents the total number of words in the file.

◈ Third Column − Represents the total number of bytes in the file. This is the actual size of the file.

◈ Fourth Column − Represents the file name.


You can give multiple files and get information about those files at a time. Following is simple syntax −

$ wc filename1 filename2 filename3

Copying Files


To make a copy of a file use the cp command. The basic syntax of the command is −

$ cp source_file destination_file

Following is the example to create a copy of the existing file filename.

$ cp filename copyfile
$

You will now find one more file copyfile in your current directory. This file will exactly be the same as the original file filename.

Renaming Files


To change the name of a file, use the mv command. Following is the basic syntax −

$ mv old_file new_file

The following program will rename the existing file filename to newfile.

$ mv filename newfile
$

The mv command will move the existing file completely into the new file. In this case, you will find only newfile in your current directory.

Deleting Files

To delete an existing file, use the rm command. Following is the basic syntax −

$ rm filename

Caution − A file may contain useful information. It is always recommended to be careful while using this Delete command. It is better to use the -i option along with rm command.

Following is the example which shows how to completely remove the existing file filename.

$ rm filename
$

You can remove multiple files at a time with the command given below −

$ rm filename1 filename2 filename3
$

Standard Unix Streams


Under normal circumstances, every Unix program has three streams (files) opened for it when it starts up −

◈ stdin − This is referred to as the standard input and the associated file descriptor is 0. This is also represented as STDIN. The Unix program will read the default input from STDIN.

◈ stdout − This is referred to as the standard output and the associated file descriptor is 1. This is also represented as STDOUT. The Unix program will write the default output at STDOUT

◈ stderr − This is referred to as the standard error and the associated file descriptor is 2. This is also represented as STDERR. The Unix program will write all the error messages at STDERR.

Thursday 18 April 2019

SED command in Linux - Set 2

We have discussed some of the SED command options in Sed Command in Linux/Unix with examples

SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search etc. It’s a one of the powerful utility offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you have the basic knowledge about Linux regular expressions.

SED Command, LPI Tutorial and Material, LPI Certifications, LPI Guides, LPI Learning

It provides Non-interactive editing of text files thats why it’s used to automate editing and has two buffers – pattern buffer and hold buffer. Sed use Patter buffer when it read files, line by line and that currently read line is inserted into pattern buffer whereas hold buffer is a long-term storage, it catch the information, store it and reuse it when it is needed. Initially, both are empty. SED command is used for performing different operation without even opening the file.

sed general syntax –


sed OPTIONS… [SCRIPT] [INPUTFILE…]

First create a.txt file on which I am going to perform operation for SED commands. In this blog, I used “a.txt” file to explain all the examples. Blog will become too long if i write the output of each sed command. So, you may refer the same file to practice all the commands initially.

[root@lpicentral ~]# cat a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life experience will help you differentiating right from wrong.

# Let’s start with File Spacing


1 – Insert one blank line after each line –

[root@lpicentral ~]# sed G a.txt

2 – To insert two blank lines –

[root@lpicentral ~]# sed 'G;G' a.txt

3 – Delete blank lines and insert one blank line after each line –

[root@lpicentral ~]# sed '/^$/d;G' a.txt

4 – Insert a black line above every line which matches “love” –

[root@lpicentral ~]# sed '/love/{x;p;x;}' a.txt

5 – Insert a blank line below every line which matches “love” –

[root@lpicentral ~]# sed '/love/G' a.txt

6 – Insert 5 spaces to the left of every lines –

[root@lpicentral ~]# sed 's/^/     /' a.txt

# Numbering lines


1 – Number each line of a file (left alignment). **=** is used to number the line. \t is used for tab between number and sentence –

[root@lpicentral ~]# sed =  a.txt | sed 'N;s/\n/\t/'

2 – Number each line of a file (number on left, right-aligned). This command is similar to `cat -n filename`.

[root@lpicentral ~]# sed = a.txt | sed 'N; s/^/     /; s/ *\(.\{4,\}\)\n/\1  /'

3 – Number each line of file, only if line is not blank –

[root@lpicentral ~]#  sed '/./=' a.txt | sed '/./N; s/\n/ /'

# Deleting lines


1 – Delete a particular line –
Syntax: sed ‘nd’ filename
Example :

[root@lpicentral ~]# sed '5d' a.txt

2 – Delete the last line
Syntax: sed ‘$d’ filename

3 – Delete line from range x to y
Syntax: sed ‘x,yd’ filename
Example :

[root@lpicentral ~]# sed '3,5d' a.txt

4 – Delete from nth to last line
Syntax: sed ‘nth,$d’ filename
Example :

[root@lpicentral ~]# sed '2,$d' a.txt

5 – Delete the patter matching line –
Syntax: sed ‘/pattern/d’ filename
Example :

[root@lpicentral ~]# sed '/life/d' a.txt

6 – Delete lines starting from nth line and every 2nd line from there –
Syntax: sed ‘n~2d’ filename
Example :

[root@lpicentral ~]# sed '3~2d' a.txt

7 – Delete the lines which matches the pattern and 2 lines after to that –
Syntax: sed ‘/pattern/,+2d’ filename
Example :

[root@lpicentral~]# sed '/easy/,+2d' a.txt

8 – Delete blank Lines

[root@lpicentral ~]# sed '/^$/d' a.txt

9 – Delete empty lines or those begins with “#” –

[root@lpicentral ~]# sed -i '/^#/d;/^$/d' a.txt

# View/Print the files


If we want to view content of file, then we use cat command and if we want to view the bottom and the top content of any file, we use tools such as head and tail. But what if we need to view a particular section in the middle of any file? Here we’ll discuss, how to use SED command to view a section of any file.

1 – Viewing a file from x to y range –
Syntax: sed -n ‘x,yp’ filename
Example :

[root@lpicentral ~]# sed -n '2,5p' a.txt

2 – View the entire file except the given range –
Syntax: sed ‘x,yd’ filename
Example :

[root@lpicentral ~]# sed '2,4d' a.txt

3 – Print nth line of the file –
Syntax: sed -n ‘address’p filename
Example :

[root@lpicentral ~]# sed -n '4'p a.txt

4 – Print lines from xth line to yth line.
Syntax: sed -n ‘x,y’p filename
Example :

[root@lpicentral ~]# sed -n '4,6'p a.txt

5 – Print only the last line –
Syntax: sed -n ‘$’p filename

6 – Print from nth line to end of file –
Syntax: sed -n ‘n,$p’ filename
Example :

[root@lpicentral ~]# sed -n '3,$'p a.txt

Pattern Printing


7 – Print the line only which matches the pattern –
Syntax: sed -n /pattern/p filename
Example :

[root@lpicentral ~]# sed -n /every/p a.txt

8 – Print lines which matches the pattern i.e from input to xth line.
Syntax: sed -n ‘/pattern/,xp’ filename
Example :

[root@lpicentral ~]# sed -n '/everyone/,5p' a.txt
Following prints lines which matches the pattern, 3rd line matches the pattern “everyone”, so it prints from 3rd line to 5th line. Use $ in place of 5, if want to print the file till end.

9 – Prints lines from the xth line of the input, up-to the line which matches the pattern. If the pattern doesn’t found then it prints up-to end of the file.
Syntax: sed -n ‘x,/pattern/p’ filename
Example :

sed -n '1,/everyone/p' a.txt

10 – Print the lines which matches the pattern up-to the next xth lines –
Syntax: sed -n ‘/pattern/,+xp’ filename
Example :

sed -n '/learn/,+2p' a.txt

# Replacement with the sed command


1 – Change the first occurrence of the pattern –

[root@lpicentral ~]# sed 's/life/leaves/' a.txt

2 – Replacing the nth occurrence of a pattern in a line –
Syntax: sed ‘s/old_pattern/new_pattern/n’ filename
Example :

[root@lpicentral ~]# sed 's/to/two/2' a.txt
We wrote “2” because we replaces the second occurrence. Likewise you can use 3, 4 etc according to need.

3 – Replacing all the occurrence of the pattern in a line.

[root@lpicentral ~]# sed 's/life/learn/g' a.txt

4 – Replace pattern from nth occurrence to all occurrences in a line.
Syntax: sed ‘s/old_pattern/new_pattern/ng’ filename
Example :

[root@lpicentral ~]# sed 's/to/TWO/2g' a.txt
Note – This sed command replaces the second, third, etc occurrences of pattern “to” with “TWO” in a line.

If you wish to print only the replaced lines, then use “-n” option along with “/p” print flag to display only the replaced lines –

[root@lpicentral ~]# sed -n 's/to/TWO/p' a.txt
And if you wish to print the replaced lines twice, then only use “/p” print flag without “-n” option-

[root@lpicentral ~]# sed 's/to/TWO/p' a.txt

5 – Replacing pattern on a specific line number. Here, “m” is the line number.
Syntax: sed ‘m s/old_pattern/new_pattern/’ filename
Example :

[root@lpicentral ~]# sed '3 s/every/each/' a.txt
If you wish to print only the replaced lines –

[root@lpicentral ~]# sed -n '3 s/every/each/p' a.txt

6 – Replace string on a defined range of lines –
Syntax: sed ‘x,y s/old_pattern/new_pattern/’ filename
where,
x = starting line number
and y = ending line number

Example :

[root@lpicentral ~]# sed '2,5 s/to/TWO/' a.txt
Note – $ can be used in place of “y” if we wish to change the pattern up-to last line in the file.
Example :

[root@lpicentral ~]# sed '2,$ s/to/TWO/' a.txt

7 – If you wish to replace pattern in order to ignore character case (beginning with uppercase or lowercase), then there are two ways to replace such patterns –
Frist, By using “/i” print flag –
Syntax: sed ‘s/old_pattern/new_pattern/i’ filename
Example :

[root@lpicentral ~]# sed 's/life/Love/i' a.txt
Second, By using regular expressions –

[root@lpicentral ~]# sed 's/[Ll]ife/Love/g' a.txt

8 – To replace multiple spaces with a single space –
[root@lpicentral clang]# sed 's/  */ /g' filename

9 – Replace one pattern followed by the another pattern –
Syntax: sed ‘/followed_pattern/ s/old_pattern/new_pattern/’ filename
Example :

[root@lpicentral ~]# sed '/is/ s/live/love/' a.txt

10 – Replace a pattern with other except in the nth line.
Syntax: sed ‘n!s/old_pattern/new_pattern/’ filename
Example :

[root@lpicentral ~]# sed -i '5!s/life/love/' a.txt

Tuesday 16 April 2019

halt, poweroff and reboot Commands in Linux

Below are typical uses of halt, poweroff and reboot.

LPI Tutorial and Material, LPI Certifications, LPI Study Material, LPI Guides

1. halt : Instructs hardware to stop CPU functions.
2. poweroff : Instructs the system to power down.
3. reboot : Restarts or reboots the system.

The above mentioned commands can only be run by super user as these involve the actions to stop the system hardware. If the user is not logged in as super user then sudo command can be used to run these commands.

halt Command


This instructs the hardware to stop all the CPU functions.

// syntax of halt command
halt [OPTION]...

The halt simply can be used to halt, poweroff and reboot the system :

◈ //  Instructs hardware to stop CPU functions.
halt

◈ // power off the system
halt -p

◈ // reboots the system
halt --reboot


Applications

◈ This lets the user to power down the system from the command line.

◈ Using halt all the CPU functions will be stopped and system will enter a state where user can perform low level maintenance.

poweroff Command


poweroff Sends an ACPI signal which instructs the system to power down. Here’s the syntax of poweroff command :

// syntax of poweroff
poweroff [OPTION]...

The poweroff simply can be used to halt, poweroff and reboot the system as :

◈ // Powers off the system
poweroff

◈ // Halts the system
poweroff --halt

◈ // Reboots the system
poweroff --reboot

reboot Command


the reboot command instructs the system to restart or reboot. Here’s the syntax of reboot command :

// syntax of reboot
reboot [OPTION]...

The reboot simply can be used to halt, poweroff and reboot the system as :

◈ // Halts the system
reboot --halt

◈ // Powers off the system
reboot -p

◈ // Reboots the system
reboot

Saturday 13 April 2019

Sed Command in Linux/Unix with examples

SED command in UNIX is stands for stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening it, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.

Sed Command, Linux Tutorial and Material, Linux Certifications, Linux Guides

◈ 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.

Syntax:


sed OPTIONS... [SCRIPT] [INPUTFILE...] 

Example:


Consider the below text file as an input.

$cat > lpicentral.txt

unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

Sample Commands


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/' lpicentral.txt

Output :

linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

Here the “s” specifies the substitution operation. The “/” are delimiters. The “unix” is the search pattern and the “linux” is the replacement string.

By default, the sed command replaces the first occurrence of the pattern in each line and it won’t replace the second, third…occurrence in the line.

2. 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' lpicentral.txt

Output:

unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.

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' lpicentral.txt

Output :

linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

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' lpicentral.txt

Output:

unix is great os. unix is opensource. linux is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.

5. Parenthesize first character of each word : This sed example prints the first character of every word in paranthesis.

$ echo "Welcome To The LPICentral Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'

Output:

(W)elcome (T)o (T)he (G)eek (S)tuff

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/' lpicentral.txt

Output:

unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

The above sed command replaces the string only on the third line.

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' lpicentral.txt

Output:

linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

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' lpicentral.txt

Output:

linux is great os. unix is opensource. unix is free os.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

If you use -n alone without /p, then the sed does not print anything.

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/' lpicentral.txt

Output:

linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

Here the sed command replaces the lines with range from 1 to 3. Another example is

$sed '2,$ s/unix/linux/' lpicentral.txt

Output:

unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful

Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in the file.

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

2. To Delete a last line

Syntax:
$ sed '$d' filename.txt

3. To Delete line from range x to y

Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt

4. To Delete from nth to last line

Syntax:
$ sed 'nth,$d' filename.txt
Example:
$ sed '12,$d' filename.txt

5. To Delete pattern matching line

Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt

Thursday 11 April 2019

Operating System - The Linux Kernel

The main purpose of a computer is to run a predefined sequence of instructions, known as a program. A program under execution is often referred to as a process. Now, most special purpose computers are meant to run a single process, but in a sophisticated system such a general purpose computer, are intended to run many processes simulteneously. Any kind of process requires hardware resources such are Memory, Processor time, Storage space, etc.

Operating System, Linux Kernel, Linux Command, LPI Study Materials

In a General Purpose Computer running many processes simulteneously, we need a middle layer to manage the distribution of the hardware resources of the computer efficiently and fairly among all the various processes running on the computer. This middle layer is referred to as the kernel. Basically the kernel virtualizes the common hardware resources of the computer to provide each process with its own virtual resources. This makes the process seem as it is the sole process running on the machine. The kernel is also responsible for preventing and mitigating conflicts between different processes.

This schematically represented below:

Operating System, Linux Kernel, Linux Command, LPI Study Materials

Figure: Virtual Resources for each Process

The Core Subsystems of the Linux Kernel are as follows:

1. The Process Scheduler
2. The Memory Management Unit (MMU)
3. The Virtual File System (VFS)
4. The Networking Unit
5. Inter-Process Communication Unit

Operating System, Linux Kernel, Linux Command, LPI Study Materials

Figure: The Linux Kernel

For the purpose of this article we will only be focussing on the 1st three important subsystems of the Linux Kernel.

The basic functioning of each of the 1st three subsystems is elaborated below:

◈ The Process Scheduler:

This kernel subsystem is responsible for fairly distributing the CPU time among all the processes running on the system simulteneously.

◈ The Memory Management Unit:

This kernel sub-unit is responsible for proper distribution of the memory resources among the various processes running on the system. The MMU does more than just simply provide separate virtual address spaces for each of the processes.

◈ The Virtual File System:

This subsystem is responsible for providing a unified interface to access stored data across different filesystems and physical storage media.

Tuesday 9 April 2019

Soft and Hard links in Unix/Linux

A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file, elsewhere.

Linux Tutorial and Material, LPI Guides, LPI Certifications, LPI Study Materials, LPI Learning

There are two types of links :

1. Soft Link or Symbolic links
2. Hard Links

These links behave differently when the source of the link (what is being linked to) is moved or removed. Symbolic links are not updated (they merely contain a string which is the pathname of its target); hard links always refer to the source, even if moved or removed.

For example, if we have a file a.txt. If we create a hard link to the file and then delete the file, we can still access the file using hard link. But if we create a soft link of the file and then delete the file, we can’t access the file through soft link and soft link becomes dangling. Basically hard link increases reference count of a location while soft links work as a shortcut (like in Windows)

1. Hard Links


◈ Each hard linked file is assigned the same Inode value as the original, therefore they reference the same physical file location. Hard links more flexible and remain linked even if the original or linked files are moved throughout the file system, although hard links are unable to cross different file systems.

◈ ls -l command shows all the links with the link column shows number of links.

◈ Links have actual file contents

◈ Removing any link, just reduces the link count, but doesn’t affect other links.

◈ We cannot create a hard link for a directory to avoid recursive loops.

◈ If original file is removed then the link will still show the content of the file.

◈ Command to create a hard link is:

$ ln  [original filename] [link name]

2. Soft Links


◈ A soft link is similar to the file shortcut feature which is used in Windows Operating systems. Each soft linked file contains a separate Inode value that points to the original file. As similar to hard links, any changes to the data in either file is reflected in the other. Soft links can be linked across different file systems, although if the original file is deleted or moved, the soft linked file will not work correctly (called hanging link).

◈ ls -l command shows all links with first column value l? and the link points to original file.

◈ Soft Link contains the path for original file and not the contents.

◈ Removing soft link doesn’t affect anything but removing original file, the link becomes “dangling” link which points to nonexistent file.

◈ A soft link can link to a directory.

◈ Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft links.

◈ Command to create a Soft link is:

$ ln  -s [original filename] [link name]

Saturday 6 April 2019

tr command in Unix/Linux with examples

LPI Tutorial and Material, LPI Guides, LPI Certifications, LPI Learning

The tr command in UNIX is a command line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace. It can be used with UNIX pipes to support more complex translation. tr stands for translate.

Syntax :


$ tr [OPTION] SET1 [SET2]

Options


-c : complements the set of characters in string.i.e., operations apply to characters not in the given set
-d : delete characters in the first set from the output.
-s : replaces repeated characters listed in the set1 with single occurrence
-t : truncates set1

Sample Commands


1. How to convert lower case to upper case


To convert from lower case to upper case the predefined sets in tr can be used.

$cat greekfile

Output:

WELCOME TO
LPICentral

$cat greekfile | tr “[a-z]” “[A-Z]”

Output:

WELCOME TO
LPICentral

or

$cat lpifile | tr “[:lower:]” “[:upper:]”

Output:

WELCOME TO
LPICENTRAL

2. How to translate white-space to tabs


The following command will translate all the white-space to tabs

$ echo "Welcome To LPICentral" | tr [:space:] '\t'

Output:

Welcome    To    LPICentral 

3. How to translate braces into parenthesis


You can also translate from and to a file. In this example we will translate braces in a file with parenthesis.

$cat greekfile

Output:

 {WELCOME TO}
LPICentral
$ tr '{}' '()'   newfile.txt

Output:

(WELCOME TO)
LPICentral

The above command will read each character from “lpifile.txt”, translate if it is a brace, and write the output in “newfile.txt”.

4. How to use squeeze repetition of characters using -s


To squeeze repeat occurrences of characters specified in a set use the -s option. This removes repeated instances of a character.
OR we can say that,you can convert multiple continuous spaces with a single space

$ echo "Welcome    To    LPICentral" | tr -s [:space:] ' '

Output:

Welcome To LPICentral

5. How to delete specified characters using -d option


To delete specific characters use the -d option.This option deletes characters in the first set specified.

$ echo "Welcome To LPICentral" | tr -d 'w'

Output:

elcome To LPICentral

6. To remove all the digits from the string, use


$ echo "my ID is 73535" | tr -d [:digit:]

Output:

my ID is


7. How to complement the sets using -c option


You can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.

$ echo "my ID is 73535" | tr -cd [:digit:]

Output:

73535

Or

$ echo “unix” | tr –c “u” “a”

Output:

Uaaa

Tuesday 2 April 2019

SORT command in Linux/Unix with examples

SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in sort command, it can also be used to sort numerically.

SORT Command, LPI Study Materials, LPI Guides, LPI Learning, LPI Certifications

◈ SORT command sorts the contents of a text file, line by line.
◈ sort is a standard command line program that prints the lines of its input or concatenation of all files listed in its argument list in sorted order.
◈ The sort command is a command line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by number, by month and can also remove duplicates.
◈ The sort command can also sort by items not at the beginning of the line, ignore case sensitivity and return whether a file is sorted or not. Sorting is done based on one or more sort keys extracted from each line of input.
◈ By default, the entire input is taken as sort key. Blank space is the default field separator.

The sort command follows these features as stated below:

1. Lines starting with a number will appear before lines starting with a letter.
2. Lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.
3. Lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase.

Examples

Suppose you create a data file with name file.txt

Command :
$ cat > file.txt
abhishek
chitransh
satish
rajan
naveen
divyam
harsh

Sorting a file : Now use the sort command

Syntax :

$ sort filename.txt

Command:
$ sort file.txt

Output :
abhishek
chitransh
divyam
harsh
naveen
rajan
satish

Note: This command does not actually change the input file, i.e. file.txt.

Sort function with mix file i.e. uppercase and lower case : When we have a mix file with both uppercase and lowercase letters then first the lower case letters would be sorted following with the upper case letters .

Example:

Create a file mix.txt

Command :
$ cat > mix.txt
abc
apple
BALL
Abc
bat

Now use the sort command

Command :
$ sort mix.txt
Output :
abc
Abc
apple
bat
BALL

Options with sort function


1. -o Option : Unix also provides us with special facilities like if you want to write the output to a new file, output.txt, redirects the output like this or you can also use the built-in sort option -o, which allows you to specify an output file.
Using the -o option is functionally the same as redirecting the output to a file.
Note: Neither one has an advantage over the other.

Example: The input file is the same as mentioned above.

Syntax :

$ sort inputfile.txt > filename.txt
$ sort -o filename.txt inputfile.txt

Command:
$ sort file.txt > output.txt
$ sort -o output.txt file.txt
$ cat output.txt

Output :
abhishek
chitransh
divyam
harsh
naveen
rajan
satish

2. -r Option: Sorting In Reverse Order : You can perform a reverse-order sort using the -r flag. the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default.

Example: The input file is the same as mentioned above.

Syntax :

$ sort -r inputfile.txt

Command :
$ sort -r file.txt
Output :
satish
rajan
naveen
harsh
divyam
chitransh
abhishek

3. -n Option : To sort a file numerically used –n option. -n option is also predefined in unix as the above options are. This option is used to sort the file with numeric data present inside.

Example :

Let us consider a file with numbers:

Command :
$ cat > file1.txt
50
39
15
89
200

Syntax :

$ sort -n filename.txt

Command :
$ sort -n file1.txt
Output :
15
39
50
89
200

4. -nr option : To sort a file with numeric data in reverse order we can use the combination of two options as stated below.
Example :The numeric file is the same as above.

Syntax :

$ sort -nr filename.txt

Command :
$ sort -nr file1.txt
Output :
200
89
50
39
15

5. -k Option : Unix provides the feature of sorting a table on the basis of any column number by using -k option.

Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.

Example :

Let us create a table with 2 columns

$ cat > employee.txt
manager  5000
clerk    4000
employee  6000
peon     4500
director 9000
guard     3000

Syntax :

$ sort -k filename.txt

Command :
$ sort -k 2n employee.txt
guard    3000
clerk    4000
peon     4500
manager  5000
employee 6000
director 9000

6. -c option : This option is used to check if the file given is already sorted or not & checks if a file is already sorted pass the -c option to sort. This will write to standard output if there are lines that are out of order.The sort tool can be used to understand if this file is sorted and which lines are out of order

Example :

Suppose a file exists with a list of cars called cars.txt.

Audi
Cadillac
BMW
Dodge

Syntax :

$ sort -c filename.txt

Command :
$ sort -c cars.txt
Output :
sort: cars.txt:3: disorder: BMW

Note: If there is no output then the file is considered to be already sorted

7. -u option : To sort and remove duplicates pass the -u option to sort. This will write a sorted list to standard output and remove duplicates.
This option is helpful as the duplicates being removed gives us an redundant file.

Example : Suppose a file exists with a list of cars called cars.txt.

Audi
BMW
Cadillac
BMW
Dodge

Syntax :

$ sort -u filename.txt

Command :
$ sort -u cars.txt
$ cat cars.txt
Output :
Audi
BMW
Cadillac
Dodge

8. -M Option : To sort by month pass the -M option to sort. This will write a sorted list to standard output ordered by month name.

Example:

Suppose the following file exists and is saved as months.txt

$ cat > months.txt

February
January
March
August
September

Syntax :

$ sort -M filename.txt

Using The -M option with sort allows us to order this file.

Command :
$ sort -M months.txt
$ cat months.txt
Output :
January
February
March
August
September

Application and uses of sort command


1. It can sort any type of file be it table file text file numeric file and so on.
2. Sorting can be directly implemented from one file to another without the present work being hampered.
3. Sorting of table files on the basis of column has been made way simpler and easier.
4. So many option are available for sorting in all possible ways.
5. The most beneficial use is that a particular data file can be used many times as no change is made in the input file provided.
6. Original data is always safe and not hampered.