cut – Splitting a file vertically : NIX Classes


The external cut command displays selected columns or fields from each line of a file.

-c     Cutting columns

to cutting specific columns, you need to follow the -c option with a list of column numbers, delimit by a comma. Range can also be used using the hyphen.

$ cut -c 6-22,24-26 emp.list

this command extract the data, from file, from 6th char to 22th and 24th char to 26th characters .

$ cut -c 2,3-4.6-12,15 emp.list

-f      Cutting fields

to extract useful data from these files u need to cut fields rather than columns. Cut use the tab as the default field delimiter, but can also work with a different delimiter. Option -d specified a single character delimiter which serves as field separator.Two option need to be used here : -d for the field delimiter and -f for the field list.

$ cut -d “|” -f2,3 emp.list

it extract the field 2,3 which is delimit by |.

** One of the option -c and -f must be specified. These option are really not optional, one of them is compulsory.

njoy the simplicity…….


pr – Paginating files : NIX Classes

the pr commands prepares a file for printing by adding suitable headers, footers and formatted text. A simple invocation of the command is to use it with a filename as argument:
$ pr filename

pr adds 5 line of margin at the top and 5 at the bottom. the header shows the date and time of last modification of the along with the filename and page number.

** pr options
-k               prints in k column
-d              double space input , reduce clutter
-n              numbers lines, helping in debugging code
-o n          offset lines by n spaces, increasing left margin of page
-t               a tab

$ pr -t -n -d -o 10 dept.list

pr often used as a “pre- processor” before printing with the lp command :
$ pr -h "Department list" dept.list | lp

$ pr +10 chap01 lp                                            start printing from page 10
$ pr -l 54 chap01 | lp                                 page length set to 54 lines

njoy the simplicity…….

Compressing and Archiving files : NIX classes

In NIX, 3 commands are used for compressing and archiving the files/folders.

* gzip & gunzip ( *.gz )
* bzip2 & bunzip2 ( *.bz2 )
* zip & unzip ( *.zip )

## gzip & gunzip : The Compressing program

$ gzip file.txt

$ gunzip file.txt.gz

gzip Option

-d    Uncompressed a “gzipped” file  (  gunzip  <==> gzip -d  )
-r    Recursive compression

## tar : The archival program

For creating a disk archive that contains a grp of file or an entire dir structure.
tar option

-c       Create an archive
-x       Extract file from archive
-t       Diaplay files in archive
-f arch     Specify the archive arch

Creating an archive ( -c )
$ tar -cvf archive.tar file1 file2      ( c – create, v – diaplay list , f – specify the name )

Extracting an archive ( -x )
$ tar -xvf archive.tar

Viewing the archive ( -t )
$ tar -tvf archive.tar

## zip & unzip : Compressing and archiving together

$ zip file1 file2 file3
$ unzip

-r    ecursive compression
-v    viewing the archive


njoy the simplicity…….

Twelve Methods to Parse a File Line by Line — Part 3

Here we are continues the magic. You can find the part 1 & 2 here ….
Twelve Methods to Parse a File Line by Line — Part 1
Twelve Methods to Parse a File Line by Line — Part 2

Method 7:

cat $FILENAME | while LINE=$(line)
function while_LINE_line_cmdsub2
cat $FILENAME | while LINE=$(line)
echo "$LINE"

Method 8:

while LINE=$(line) from the Bottom
function while_LINE_line_bottom_cmdsub2
while LINE=$(line)
echo "$LINE"
done < $FILENAME

Method 9:

while read LINE Using File Descriptors
function while_read_LINE_FD
exec 3<&0
exec 0< $FILENAME
while read LINE
echo “$LINE”
exec 0<&3

njoy the simplicity…….
© Mastering Unix Shell Scripting

How to Prevent someone else from reseting your root password in NIX

If you are an even slightly security-consious sysadmin, the previous sections must have set off alarms while you were reading them. Is it really that easy to hack Linux? Yes and No. It all it comes down to the following: Physical Access is Root Access. Meaning, if you give someone physical access to a system, then you are giving them a very good chance of getting root access on your box. This is true for Windows, Linux, or any other OS out there.

But… you say that you need to give some people physical access to the server? There are some precautions you can take to slow down attackers and stop the noob’s. In this section I will talk about various ways you can make your computer more secure against these types of attacks. So lets get started.
3.1.1 Password protecting GRUB and LILO

First, edit the /etc/inittab file and insert the following line, right after the “initdefault” line: ~~:S:wait:/sbin/sulogin. This will require a password to boot into single-user mode by making init run ‘sulogin’ before dropping the machine to a root shell. ‘sulogin’ requires the user to input the root password before continuing.

Unfortunately, the above step won’t protect us against people who know what they are doing and pass init=/bin/bash to the kernel at the LILO prompt. To prevent unauthorized access I would suggest that you password protect LILO/GRUB by following these steps:

How to Protect LILO:

Open a shell prompt and log in as root
Open /etc/lilo.conf in your favorite text editor
Add the following line before the first image stanza: password=<password> , where <password> is your password.
Run /sbin/lilo -v to let the changes take effect
Type chmod 600 /etc/lilo.conf to give only root access to read and edit the file since all passwords are in plain text
Relax a bit, as your system is a little bit more secure

How to password-protect GRUB

Open a shell prompt and log in as root
Type /sbin/grub-md5-crypt and press enter
Enter the password you chose for GRUB when prompted. This will return an MD5 hash of your password
Open /boot/grub/grub.conf in your favorite text editor
Add password –md5 <password-hash> below the timeout in the main section (Replace <password-hash> with the hash you got in the previous step)
Save and exit
The next time you reboot, the GRUB menu will not let you access the editor or command interface without first pressing [p] followed by the GRUB password.

njoy the simplicity…….

Reset passwords by booting into single-user mode in NIX

A) Booting into single-user mode from LILO

Follow these steps to reset the password when using LILO:

Reboot the system. When you see the LILO: prompt, type in linux single and press ‘Enter’. This will log you in as root in single-user mode. If your system requires you to enter your root password to log in, then try linux init=/bin/bash instead.

Once the system finishes booting, you will be logged in as root in single-user mode. Use passwd and choose a new password for root.
Type reboot to reboot the system and then you can login with the new password you just selected.

If you have a new version of LILO which gives you a menu selection of the various kernels available press Tab to get the LILO: prompt and then proceed as shown above.

B) Booting into single user mode from GRUB

Follow these steps to reset the password when using GRUB:

Reboot the system, and when you are at the selection prompt , highlight the line for Linux and press ‘e’. You may only have 2 seconds to do this, so be quick.

This will take you to another screen where you should select the entry that begins with ‘kernel’ and press ‘e’ again.

Append ‘ single’ to the end of that line (without the quotes). Make sure that there is a space between what’s there and ‘single’. If your system requires you to enter your root password to log into single-user mode, then append init=/bin/bash after ‘single’. Hit ‘Enter’ to save the changes.

Press ‘b’ to boot into Single User Mode.
Once the system finishes booting, you will be logged in as root. Use passwd and choose a new password for root.
Type reboot to reboot the system, and you can login with the new password you just selected.

njoy the simplicity…….

Resetting the Password of a user differently

In some of the Linux distributions, including Ubuntu, when you go to recovery mode to reset the password of root or any other user, the user’s current password would be prompted. To circumvent this, boot with a Live CD of the Linux distribution and edit the /etc/passwd file using vi or any editor of convenience.

As you must be aware, the fields in the file are separated by ‘:’ (colon) character and the second field is the password of the user. In Linux this value is set to ‘x’ to indicate that the password is stored in /etc/shadow file in encrypted form. Just remove this ‘x’ character from the file and let the field be empty against the user whose password needs to be reset. Now, reboot this Linux machine and during the booting process hit ‘Alt + F1’ to open up a console. You would be prompted for the user name to login to. Once the username is entered the shell prompt comes up without requiring any password.

Now you can reset the password without the need to the previous password.

I hope this little Tip will create curiosity in your mind to explore Linux more and you will know more about it. See you in the next newsletter. Until then keep exploring Linux.

njoy the simplicity…….