Sunday, 31 March 2019

shutdown command in Linux with Examples

The shutdown command in Linux is used to shutdown the system in a safe way. You can shutdown the machine immediately, or schedule a shutdown using 24 hour format.It brings the system down in a secure way. When the shutdown is initiated, all logged-in users and processes are notified that the system is going down, and no further logins are allowed.

Shutdown Command, Linux Tutorial and Materials, Linux Guides, Linux Learning

Only root user can execute shutdown command.

Syntax of shutdown Command


shutdown [OPTIONS] [TIME] [MESSAGE]

◈ options – Shutdown options such as halt, power-off (the default option) or reboot the system.
◈ time – The time argument specifies when to perform the shutdown process.
◈ message – The message argument specifies a message which will be broadcast to all users.

Options


-r : Requests that the system be rebooted after it has been brought down.
-h : Requests that the system be either halted or powered off after it has been brought down, with the choice as to which left up to the system.
-H : Requests that the system be halted after it has been brought down.
-P : Requests that the system be powered off after it has been brought down.
-c : Cancels a running shutdown. TIME is not specified with this option, the first argument is MESSAGE.
-k : Only send out the warning messages and disable logins, do not actually bring the system down.

How to use shutdown


In it’s simplest form when used without any argument, shutdown will power off the machine.

sudo shutdown

How to shutdown the system at a specified time


The time argument can have two different formats. It can be an absolute time in the format hh:mm and relative time in the format +m where m is the number of minutes from now.

The following example will schedule a system shutdown at 05 A.M:

sudo shutdown 05:00

The following example will schedule a system shutdown in 20 minutes from now:

sudo shutdown +20

How to shutdown the system immediately


To shutdown your system immediately you can use +0 or its alias now:

sudo shutdown now

How to broadcast a custom message


The following command will shut down the system in 10 minutes from now and notify the users with message “System upgrade”:

sudo shutdown +10 "System upgrade"

It is important to mention that when specifying a custom wall message you must specify a time argument too.

How to halt your system


This can be achieved using the -H option.

shutdown -H

Halting means stopping all CPUs and powering off also makes sure the main power is disconnected.

How to make shutdown power-off machine


Although this is by default, you can still use the -P option to explicitly specify that you want shutdown to power off the system.

shutdown -P

How to reboot using shutdown


For reboot, the option is -r.

shutdown -r

You can also specify a time argument and a custom message:

shutdown -r +5 "Updating Your System"

The command above will reboot the system after 5 minutes and broadcast Updating Your System”

How to cancel a scheduled shutdown


If you have scheduled a shutdown and you want to cancel it you can use the -c argument:

sudo shutdown -c

When canceling a scheduled shutdown, you cannot specify a time argument, but you can still broadcast a message that will be sent to all users.

sudo shutdown -c "Canceling the reboot"

Saturday, 30 March 2019

unexpand command in Linux with Examples

To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command.

Unexpand Command, Linux Tutorial and Material, Linux Guides, Linux Learning, Linux Certifications

The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here’s the syntax of unexpand command :

Syntax :


$unexpand [OPTION]... [FILE]...

where, OPTION refers to the options compatible with unexpand and FILE refers to the fie name.

Using unexpand command


To convert all the space characters into tab characters in the file kt.txt , use unexpand as :

$cat -vet kt.txt
have    a    nice    day$
always    try    harder$
to    achieve    better$

/* In the below output $ refers to
   the new line feed and ^I refers
   to the tab */
$unexpand kt.txt
have^Ia^Inice^Iday$
always^Itry^Iharder$
to^Iachieve^Ibetter$

In order to save the produced output by unexpand command in another file let’s say dv.txt use the below syntax :

/* Saving output in file, dv.txt */
$unexpand kt.txt>dv.txt

$cat -vet dv.txt
have^Ia^Inice^Iday$
always^Itry^Iharder$
to^Iachieve^Ibetter$

Options for unexpand command


◈ -a, – -all option : This option is used to convert all blanks, instead of just initial blanks (which is by default).

/* This converts all the blanks
   also into tabs */
$unexpand -a kt.txt>dv.txt

◈ – -first-only option : This is used to convert only leading sequences of blanks (overrides -a option).

/* This converts only the leading
   sequences of blanks */
$unexpand --first-only kt.txt>dv.txt

◈ -t, – -tabs=N option : This set tabs N characters apart instead of the default of 8 (enables -a option).

/* the -t option with numerical value
   2 forces to change the spaces
   into tabs of only 2 characters */
$unexpand -t2 kt.txt>dv.txt

◈ -t, – -tabs=LIST option : This option uses comma separated LIST of tab positions (enables -a option).
◈ – -help option : This display a help message and exit.
◈ – -version option : This display version information and exit.

Thursday, 28 March 2019

16 Useful ‘cp’ Command Examples for Linux Beginners

cp Command, Linux Tutorial and Material, LPI Study Material, LPI Guides

In this article we will demonstrate 16 useful cp command examples specially for the linux beginners. Following is the basic syntax of cp command,

Copy a file to another file

# cp {options} source_file target_file

Copy File(s) to another directory or folder

# cp {options} source_file   target_directory

Copy directory to directory

# cp {options} source_directory target_directory

Let’s jump into the practical examples of cp command,

Example:1) Copy file to target directory


Let’s assume we want copy the /etc/passwd file to /mnt/backup directory for some backup purpose, so run below cp command,

root@lpicentral:~# cp /etc/passwd /mnt/backup/
root@lpicentral:~#

Use below command to verify whether it has been copied or not.

root@lpicentral:~# ls -l /mnt/backup/
total 4
-rw-r--r-- 1 root root 2410 Feb  3 17:10 passwd
root@lpicentral:~#

Example:2) Copying multiple files at the same time


Let’s assume we want to copy multiples (/etc/passwd, /etc/group & /etc/shadow) at same time to target directory (/mnt/backup)

root@lpicentral:~# cp /etc/passwd /etc/group /etc/shadow /mnt/backup/
root@lpicentral:~#

Example:3) Copying the files interactively (-i)


If you wish to copy the files from one place to another interactively then use the “-i” option in cp command, interactive option only works if the destination directory already has the same file, example is shown below,

root@lpicentral:~# cp -i /etc/passwd /mnt/backup/
cp: overwrite '/mnt/backup/passwd'? y
root@lpicentral:~#

In the above command one has to manually type ‘y’ to allow the copy operation

Example:4) Verbose output during copy command (-v)


If you want the verbose output of cp command then use “-v” option, example is shown below

root@lpicentral:~# cp -v /etc/fstab  /mnt/backup/
'/etc/fstab' -> '/mnt/backup/fstab'
root@lpicentral:~#

In case you want to use both interactive mode and verbose mode then use the options “-iv”

root@lpicentral:~# cp -iv /etc/fstab  /mnt/backup/
cp: overwrite '/mnt/backup/fstab'? y
'/etc/fstab' -> '/mnt/backup/fstab'
root@lpicentral:~#

Example:5) Copying a directory or folder (-r or -R)


To copy a directory from one place to another use -r or -R option in cp command. Let’s assume we want to copy the home directory of lpicentral user to “/mn/backup”,

root@lpicentral:~# cp -r /home/lpicentral/mnt/backup/
root@lpicentral:~#

In above command, -r option will copy the files and directory recursively.

Now verify the contents of lpicentral directory on target place,

root@lpicentral:~# ls -l /mnt/backup/lpicentral/
total 24
drwxr-xr-x 2 root root 4096 Feb  3 17:41 data
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_1.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_2.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_3.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_4.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_5txt
-rw-r--r-- 1 root root    0 Feb  3 17:41 file_5.txt
root@lpicentral:~#

Example:6) Archive files and directory during copy (-a)


While copying a directory using cp command we generally use -r or -R option, but in place of -r option we can use ‘-a’ which will archive the files and directory during copy, example is shown below,

root@lpicentral:~# cp -a /home/lpicentral /mnt/backup/
root@lpicentral:~# ls -l /mnt/backup/lpicentral/
total 24
drwxr-xr-x 2 root root 4096 Feb  3 17:41 data
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_1.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_2.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_3.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_4.txt
-rw-r--r-- 1 root root    7 Feb  3 17:40 file_5txt
-rw-r--r-- 1 root root    0 Feb  3 17:39 file_5.txt
root@lpicentral:~#

Example:7) Copy only when source file is newer than the target file (-u)


There can be some scenarios where you want copy the files only if the source files are newer than the destination ones. This can be easily achieved using “-u” option in the cp command.

In the Example:6  we have copied the lpicentral home directory to /mnt/backup folder, in the lpicentral home folder we have 5 txt files, let’s edit couple of them and then copy all the txt files using “cp -u”.

root@lpicentral:~# cd /home/lpicentral/
root@lpicentral:/home/lpicentral# echo "LinuxRocks" >> file_1.txt
root@lpicentral:/home/lpicentral# echo "LinuxRocks" >> file_4.txt
root@lpicentral:/home/lpicentral# cp -v -u  file_*.txt /mnt/backup/lpicentral/
'file_1.txt' -> '/mnt/backup/lpicentral/file_1.txt'
'file_4.txt' -> '/mnt/backup/lpicentral/file_4.txt'
root@lpicentral:/home/lpicentral#

Example:8) Do not overwrite the existing file while copying (-n)


There are some scenarios where you don’t want to overwrite the existing destination files while copying. This can be accomplished using the option ‘-n’ in ‘cp’ command

root@lpicentral:~# cp -i /etc/passwd /mnt/backup/
cp: overwrite '/mnt/backup/passwd'?

As you can see in above command, it is prompting us to overwrite the existing file, if you use -n then it will not prompt for the overwrite and also will not overwrite the existing file.

root@lpicentral:~# cp -n /etc/passwd /mnt/backup/
root@lpicentral:~#

Example:9) Creating symbolic links using cp command (-s)


Let’s assume we want to create symbolic link of a file instead copying using cp command, for such scenarios use ‘-s’ option in cp command, example is shown below

root@lpicentral:~# cp -s /home/lpicentral/file_1.txt /mnt/backup/
root@lpicentral:~# cd /mnt/backup/
root@lpicentral:/mnt/backup# ls -l file_1.txt
lrwxrwxrwx 1 root root 27 Feb  5 18:37 file_1.txt -> /home/lpicentral/file_1.txt
root@lpicentral:/mnt/backup#

Example:10) Creating Hard link using cp command (-l)


If you want to create hard link of a file instead copy using cp command, then use ‘-l’ option. example is shown below,

root@lpicentral:~# cp -l /home/lpicentral/devops.txt /mnt/backup/
root@lpicentral:~#

As we know in hard link, source and linked file will have the same inode numbers, let’s verify this using following commands,

root@lpicentral:~# ls -li /mnt/backup/devops.txt
918196 -rw-r--r-- 2 root root 37 Feb  5 20:02 /mnt/backup/devops.txt
root@lpicentral:~# ls -li /home/lpicentral/devops.txt
918196 -rw-r--r-- 2 root root 37 Feb  5 20:02 /home/lpicentral/devops.txt
root@lpicentral:

Example:11) Copying attributes from source to destination (–attributes-only)


If you want to copy only the attributes from source to destination using cp command, then use option “–attributes-only”

root@lpicentral:/home/lpicentral# cp --attributes-only /home/lpicentral/distributions.txt /mnt/backup/
root@lpicentral:/home/lpicentral# ls -l /home/lpicentral/distributions.txt
-rw-r--r-- 1 root root 41 Feb  5 19:31 /home/lpicentral/distributions.txt
root@lpicentral:/home/lpicentral# ls -l /mnt/backup/distributions.txt
-rw-r--r-- 1 root root 0 Feb  5 19:34 /mnt/backup/distributions.txt
root@lpicentral:/home/lpicentral#

In the above command, we have copied the distribution.txt file from lpicentral home directory to /mnt/backup folder, if you have noticed, only the attributes are copied, and content is skipped. Size of distribution.txt under /mn/backup folder is zero bytes.

Example:12) Creating backup of existing destination file while copying (–backup)


Default behavior of cp command is to overwrite the file on destination if the same file exists, if you want to make a backup of existing destination file during the copy operation then use ‘–backup‘ option, example is shown below,

root@lpicentral:~# cp --backup=simple -v /home/lpicentral/distributions.txt /mnt/backup/distributions.txt
'/home/lpicentral/distributions.txt' -> '/mnt/backup/distributions.txt' (backup: '/mnt/backup/distributions.txt~')
root@lpicentral:~#

If you have noticed, backup has been created and appended tilde symbol at end of file. backup option accept following parameters

◈ none, off  – never make backups
◈ numbered, t – make numbered backups
◈ existing, nil – numbered if numbered backups exist, simple otherwise
◈ simple, never – always make simple backups

Example:13) Preserve mode, ownership and timestamps while copying (-p)


If you want to preserve the file attributes like mode, ownership and timestamps while copying then use -p option in cp command, example is demonstrated below,

root@lpicentral:~# cd /home/lpicentral/
root@lpicentral:/home/lpicentral# cp -p devops.txt /mnt/backup/
root@lpicentral:/home/lpicentral# ls -l devops.txt
-rw-r--r-- 1 root root 37 Feb  5 20:02 devops.txt
root@lpicentral:/home/lpicentral# ls -l /mnt/backup/devops.txt
-rw-r--r-- 1 root root 37 Feb  5 20:02 /mnt/backup/devops.txt
root@lpicentral:/home/lpicentral#

Example:14) Do not follow symbolic links in Source while copying (-P)


If you do not want to follow the symbolic links of source while copying then use -P option in cp command, example is shown below

root@lpicentral:~# cd /home/lpicentral/
root@lpicentral:/home/lpicentral# ls -l /opt/nix-release.txt
lrwxrwxrwx 1 root root 14 Feb  9 12:28 /opt/nix-release.txt -> os-release.txt
root@lpicentral:/home/lpicentral#
root@lpicentral:/home/lpicentral# cp -P os-release.txt /mnt/backup/
root@lpicentral:/home/lpicentral# ls -l /mnt/backup/os-release.txt
-rw-r--r-- 1 root root 35 Feb  9 12:29 /mnt/backup/os-release.txt
root@lpicentral:/home/lpicentral#

Note: Default behavior of cp command is to follow the symbolic links in source while copying.

Example:15) Copy the files and directory forcefully using -f option


There can be some scenarios where existing destination file cannot be opened and removed. And if you have healthy file which can be copied in place of existing destination file, then use cp command along with -f option

root@lpicentral:/home/lpicentral# cp -f distributions.txt  /mnt/backup/
root@lpicentral:/home/lpicentral#

Example:16) Copy sparse files using sparse option in cp command


Sparse is a regular file which contains long sequence of zero bytes that doesn’t consume any physical disk block. One of benefit of sparse file is that it does not consume much disk space and read operation on that file would be quite fast.

Let’s assume we have sparse cloud image named as “ubuntu-cloud.img”

root@lpicentral:/home/lpicentral# du -sh ubuntu-cloud.img
12M     ubuntu-cloud.img
root@lpicentral:/home/lpicentral# cp --sparse=always ubuntu-cloud.img /mnt/backup/
root@lpicentral:/home/lpicentral# du -sh /mnt/backup/ubuntu-cloud.img
0       /mnt/backup/ubuntu-cloud.img
root@lpicentral:/home/lpicentral#

Different options can be used while using sparse parameter in cp command,

◈ sparse=auto
◈ sparse-always
◈ sparse=never

Wednesday, 27 March 2019

Doing It For The Right Reasons: Millennials, Open Source, and Philosophy

Congratulations on choosing a carreer in Linux and Open Source! If you're just starting on the journey, I wish you all the best. I'm thrilled to hear that you've decided to pursue a career in Linux and open source software, and I hope that it brings you lots of success, both financially and professionally. However, and you knew that was going to be a "however", I hope you've decided to do this for the right reasons.


Not that there's anything wrong with picking a career that's going to ensure a steady stream of income for the next several years, on which you are going to build not only your future but your family's future, but there's more to selecting open source as a career then just the money.

So, are you going into open source for the right reasons?

Back in 1985, Starship -- the band, not an actual starship-- released a song called, "We Built This City". Starship, if you are too young to remember, was the evolution of Jefferson Starship, itself the evolution of Jefferson Airplane. A rather messy legal battle accompanied said evolution but I won't bore you with that here. The song, "We Built This City," was written by Bernie Taupin, Martin Page, Dennis Lambert, and Peter Wolf, while the vocals for the song came from Grace Slick and Mickey Thomas. I tell you all this because, well, it takes as village, as they say, even when you're building a city on rock and roll.

Since I mentioned a legal battle in the previous paragraph, I'd be remiss not to mention that Linux, the poster child for Open Source software, had it's own long running legal battle against a poser for open source, namely the company formerly known as "SCO". For the record the good guys won.

When people try to convince you that some moral point of view should grab and hold your attention, it's not uncommon to hear something to the effect that you should "do it for the children." Proponents of Open Source have long believed that free software can be a boon to children at every stage of development, both in terms of age, but also financially. Not far from where I live, there's the "Computer Recycling Centre", where volunteers take old computers, fix them up, load up Linux (usually), and make them available to people who otherwise could not afford them.

In 2005, Nicholas Negroponte spoke at the World Economic Forum in Davos, a purportedly non-profit organization represented by some of the richest people in the history of this planet. Negroponte was there to champion a project to elevate children of distant African villages into participants of the great global village, the Internet. This ambition child-oriented open source project was OLPC, or "One Laptop Per Child". Much has been written about this bold and grand plan to create a small, insanely durable laptop, and to put untold numbers of these laptops into the hands of disadvantaged children around the world for $100 per laptop (see the image accompanying this article). The project never achieved its lofty goals for reasons that would fill a book, but it did leave some interesting DNA behind in the form of free software.

Despite grand plans and a handful of bright green and white notebooks, the project never really achieved its goal of creating the $100 laptop, leaving behind tantalizing bits of open source DNA and some hard lessons about idealism in a world where questions about who counts the money while playing all those corporation games tends to rise above any attempt to put idealism first.

The Internet itself, the greatest boon to the world's impressive economic engine, is built on open source idealism. Powered by countless Linux servers in enormous corporate clouds, backed up by open protocols, open source domain and routing services, open source email, open source file sharing, open source Web services, and open source pretty much everything, the idealists of open source built the Internet. Even more tantalizing is that with open source powering this economic behemoth, business has wrapped itself in the mantle of open source, generating billions upon billions of dollars and defining, through the awesome power of their wealth, the shape of the world in which we live.

Except that we built this city. When viewed through the lens of the early creators of open source technology, we see a world where the technical and technological power of a handful of closed source companies (e.g. Microsoft, IBM, Sun) was reduced to a level playing field where anyone with the knowledge to code could, with freely available open source tools on a Linux system, create any kind of business (or world) they damn well pleased. In fact, we could imagine a world where open source was the norm and closed source was a kind of relic of an earlier age. When that day came, we would all be free to share in the wealth that was currently held by so few.

Spoiler alert! We did it. Today, open source is ubiquitous, so much so that most people don't realize that they use open source every moment of every day they interact with the digital world. Those crazy open source idealists of 25 or 30 years past managed to create something out of science fiction, a world where you can talk to a little gray speaker on your kitchen table and have it tell you the weather, read you the news, tell you a joke, book an appointment, make a call, or ask who wrote "We Built This City" and then play you the song.

But something happened on the way to the great open world of the future. A few powerful companies where born who knew how to take advantage of the work done by those open source idealists. Heck, some of them were, and may still be to some degree, open source idealists. Trouble is, the impact that people learning about and working with open source has steadily diminished. Those who count the money decide what happens next and most of us, myself included, are basically along for the ride.

Back at the tail end of November of 2018, I read an article titled, "Millennials and Open Source: Don't Know, Don't Care?" and, as they say, it got me to thinking. Thinking about it makes me think of that old saying about those  who forget history being doomed to repeat it. Understanding what makes open source special created the present we live in and gave us amazing super powers. Forgetting that means forgetting that you have those powers and letting only the rich and powerful decide what kind of world you will inherit. If you are going to work in this field, you most definitely should care, must care, about open source.

We all need the money. Until we enter into that Star Trek universe where money isn't really used by most people, dinner makes it to the table by virtue of selling your skills to an employer. You learn Linux and open source, maybe specialize in DevOps, and insert yourself into a big company making a mint from open source. But remember that those tools, and the knowledge that you've learned along the way, allow you to build and create a world of wonders as amazing as anything that Google or Facebook or Amazon have built (with our help). If we don't like this city, we can build another, and another, because open source is power and, thanks for some great licensing foresight, we all own it.

Build the next laptop for needy children the world over. Create a security system that protects privacy while delivering the good of the Internet. Think of a way to develop the next level sharing economy that will make your neighbours' poverty disappear as they share in the wealth that open source creates. Find a way, as you work at getting your paycheque, to recapture some of that wide-eyed idealism that comes with understanding that open source gives you the same power as everyone else regardless of how rich and powerful they are.

Tuesday, 26 March 2019

md5sum Command in Linux with Examples

The md5sum is designed to verify data integrity using MD5 (Message Digest Algorithm 5).

md5sum Command, Linux Certifications, Linux Learning, Linux Tutorial and Material

MD5 is 128-bit cryptographic hash and if used properly it can be used to verify file authenticity and integrity.

Example :


Input : md5sum /home/xyz/test/test.cpp
Output : c6779ec2960296ed9a04f08d67f64422  /home/xyz/test/test.cpp

Importance :

Suppose, anyone wants to install an operating system , so to verify if it’s correct CD, it’s always a good idea to verify .iso file using MD5 checksum, so that you don’t end up installing wrong software (some sort of virus which can corrupt your filesystem).

Syntax :


md5sum [OPTION]... [FILE]...

It will print or check MD5(128-bit) checksum.

It computes MD5 checksum for file “test.cpp”

Output :

c6779ec2960296ed9a04f08d67f64422  /home/xyz/test/test.cpp

Options :

-b : read in binary mode
-c : read MD5 from files and check them
–tag : create a BSD-style checksum
-t : read in text mode(it’s by default)

The options which are useful when verifying checksum :


–ignore-missing : don’t report status for missing files
–quiet : don’t print OK for each successfully verified file
–status : don’t output anything, status code shows success
–strict : exit non-zero for improperly formatted checksum files
-w : warn about improperly formatted checksum files

Command usage examples with options :

Example 1: Store the MD5 checksum in file and then verify it.

# md5sum /home/xyz/test/test.cpp > checkmd5.md5

It will store the MD5 checksum for test.cpp in file checkmd5.md5

# md5sum -c checkmd5.md5

It will verify the contents of file

Output :

/home/xyz/test/test.cpp: OK

After changing the contents of file checkmd5.md5, the output will be :

/home/xyz/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

Example 2: create a BSD-style checksum with –tag option

# md5sum --tag /home/xyz/test/test.cpp

Output :

MD5 (/home/xyz/test/test.cpp) = c6779ec2960296ed9a04f08d67f64422

Example 3: – quiet option, can be used when verifying checksum, don’t print OK when verification is successful.

#  md5sum -c --quiet  checkmd5.md5

Don’t produce any output, means it’s successful.

But if checksum don’t match, it produces warning.

# md5sum -c --quiet  checkmd5.md5
/home/xyz/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

Example 4: – warn option, it can be used for generating a warning for improperly formatted checksum files.

content of file checkmd5.md5:

c6779ec2960296ed9a04f08d67f64422 /home/xyz/test/test.cpp

Now, execute command with –warn option

# md5sum -c --warn  checkmd5.md5
/home/xyz/test/test.cpp: OK

It don’t produce any warning.

Now, do some formatting in file checkmd5.md5

c6779ec2960296ed9a04f08d67f64422
/home/xyz/test/test.cpp

Now, execute the command

# md5sum -c --warn  checkmd5.md5

Output :

md5sum: checkmd5.md5: 1: improperly formatted MD5 checksum line
md5sum: checkmd5.md5: 2: improperly formatted MD5 checksum line
md5sum: checkmd5.md5: no properly formatted MD5 checksum lines found

and if –warn is replaced with –strict option, it will exit non-zero for improperly formatted checksum lines

# md5sum -c --strict  checkmd5.md5
md5sum: checkmd5.md5: no properly formatted MD5 checksum lines found

Saturday, 23 March 2019

Paste command in Linux with examples

Paste Command, Linux Command, Linux Tutorial and Materials, Linux Learning

Paste command is one of the useful commands in Unix or Linux operating system. It is used to join files horizontally (parallel merging) by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output. When no file is specified, or put dash (“-“) instead of file name, paste reads from standard input and gives output as it is until a interrupt command [Ctrl-c] is given.

Syntax:


paste [OPTION]... [FILES]...

Let us consider three files having name state, capital and number. state and capital file contains 5 names of the Indian states and capitals respectively. number file contains 5 numbers.

$ cat state
Arunachal Pradesh
Assam
Andhra Pradesh
Bihar
Chhattisgrah

$ cat capital
Itanagar
Dispur
Hyderabad
Patna
Raipur

Without any option paste merges the files in parallel. The paste command writes corresponding lines from the files with tab as a deliminator on the terminal.

$ paste number state capital
1       Arunachal Pradesh       Itanagar
2       Assam   Dispur
3       Andhra Pradesh  Hyderabad
4       Bihar   Patna
5       Chhattisgrah    Raipur

In the above command three files are merges by paste command.

Options:


1. -d (delimiter): Paste command uses the tab delimiter by default for merging the files. The delimiter can be changed to any other character by using the -d option. If more than one character is specified as delimiter then paste uses it in a circular fashion for each file line separation.

Only one character is specified
$ paste -d "|" number state capital
1|Arunachal Pradesh|Itanagar
2|Assam|Dispur
3|Andhra Pradesh|Hyderabad
4|Bihar|Patna
5|Chhattisgrah|Raipur

More than one character is specified
$ paste -d "|," number state capital
1|Arunachal Pradesh,Itanagar
2|Assam,Dispur
3|Andhra Pradesh,Hyderabad
4|Bihar,Patna
5|Chhattisgrah,Raipur

First and second file is separated by '|' and second and third is separated by ','.
After that list is exhausted and reused.

2. -s (serial): We can merge the files in sequentially manner using the -s option. It reads all the lines from a single file and merges all these lines into a single line with each line separated by tab. And these single lines are separated by newline.

$ paste -s number state capital
1       2       3       4       5
Arunachal Pradesh       Assam   Andhra Pradesh  Bihar   Chhattisgrah
Itanagar        Dispur  Hyderabad       Patna   Raipur

In the above command, first it reads data from number file and merge them into single line with each line separated by tab. After that newline character is introduced and reading from next file i.e. state starts and process repeats again till all files are read.

Combination of -d and -s: The following example shows how to specify a delimiter for sequential merging of files:

$ paste -s -d ":" number state capital
1:2:3:4:5
Arunachal Pradesh:Assam:Andhra Pradesh:Bihar:Chhattisgrah
Itanagar:Dispur:Hyderabad:Patna:Raipur

3. –version: This option is used to display the version of paste which is currently running on your system.

$ paste --version
paste (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Applications of Paste Command


1. Combining N consecutive lines: The paste command can also be used to merge N consecutive lines from a file into a single line. Here N can be specified by specifying number hyphens(-) after paste.

With 2 hyphens
$ cat capital | paste - -
Itanagar        Dispur
Hyderabad       Patna
Raipur

With 3 hyphens
$ paste - - - < capital
Itanagar        Dispur  Hyderabad
Patna   Raipur

2. Combination with other commands: Even though paste require at least two files for concatenating lines, but data from one file can be given from shell. Like in our example below, cut command is used with -f option for cutting out first field of state file and output is pipelined with paste command having one file name and instead of second file name hyphen is specified.

Note: If hyphen is not specified then input from shell is not pasted.

Without hypen
$ cut -d " " -f 1 state | paste number
1
2
3
4
5

With hypen
$ cut -d " " -f 1 state | paste number -
1       Arunachal
2       Assam
3       Andhra
4       Bihar
5       Chhattisgrah

Ordering of pasting can be changed by altering the location of hyphen:

$ cut -d " " -f 1 state | paste - number
Arunachal       1
Assam   2
Andhra  3
Bihar   4
Chhattisgrah    5