The Linux Professional Institute LPIC-1 101 exam requires that you have a knowledge of a number of commands that can be used to manage text files. One such command is the Linux paste command. I know that this is something that many Instructor struggle with especially when trying to come up with ideas of how the command is used. The Linux paste command is quite simple really, it takes two or more files and combines their data. Line 1 of both file1 and file2 will appear together in the new file as line 1.
I am Lost Already
Ok, I can tell I have you confused already, so let’s take a look at two files and use the paste command to join them. The first file is called users with the following three lines:
bob joe bill
The second file contains department names and is called departments :
sales IT kitchen
Using the Linux paste command we can join these two files together:
$ paste users departments
The resulting output will look like this:
bob sales joe IT bill kitchen
Create the files and try it running the command on your system.
But What is the Point
Now this is where it can seem difficult to understand because the data that is being joined has no relationship. We are not saying the the user bill should be in sales and so on. The resulting file in this case does not have any meaning. However, if we had users in one file and passwords in another we can use it to set new passwords for users using the Linux command paste and chpasswd. We just need to have the right data to make sense of the Linux paste command.
Random Password Generator
For a practical demonstration on using the Linux paste command we will set passwords for newly created users. In setting the passwords we will use randomly generated passwords created with the command pwgen. We are running the demonstration on Ubuntu so you can install the package with.
$ sudo apt-get update ; sudo apt-get install pwgen
We can generate passwords then from the command line. If we want 8 character passwords and we want say three passwords created we can run the command:
$ pwgen -B -1 8 3
Where -1 ( dash one) creates a list of passwords. 8 represent the desired password length and 3 tells the command to generate 3 passwords. The -B option avoids using difficult to read characters such as the 0 and O. Lower-case o can be used but pwgen will not generate passwords containing zero or Upper-case O as they can be difficult to differentiate between those characters.
If we want to create the file containing passwords to use for the new users then redirect the output to a file:
$ pwgen -B -1 8 3 > passwords
We now have a file containing the 3 passwords on 3 lines.
$ cat passwords wu3Thija eesegh9g Ahzua9uJ
Linux Paste Command
If we take the original users file that we had containing the 3 users names we can combine the two files together with paste in a format the the command chpasswd can use. The command chpasswd needs the file formatted as:
username:password
To ensure that we have the : between the user name and password we use the -d option with paste. In this way we use the colon to delimit the two fields rather than the default TAB.
$ paste -d: users passwords > user-passwords
We redirect the output to the new file which we have inventively named user-passwords. The content of the new file will look similar to this:
bob:wu3Thija joe:eesegh9g bill:Ahzua9uJ
Exactly the way we want it for chpasswd.
Create the Users
Now we need to create the user accounts. Obviously, we can only set passwords on users that exist.
$ for u in bob joe bill ; do > sudo useradd -m $u > done
With the users created we need to set their passwords:
$ sudo chpasswd < user-passwords
And we are done. More importantly we have seen a practical usage of the Linux paste command.