How to Change the Attributes of a File in Linux using chattr Command

chattr : chattr is a command in the Linux operating system that allows a user to set certain attributes on a file residing on an ext2/ext3/ext4 based filesystem.

 

Syntax :

 

 #chattr [operator] [switch] [file name]
Operator :

+      Add attribute.
–      Remove attribute.
=      Assign attributes (removing unspecified attributes)

 

Switch :

 

-R      Recursively change attributes of directories and their contents. Symbolic links encountered during   recursive directory traversals are ignored.
-a      A file with the ‘a’ attribute set can only be open in append mode for writing. Only the superuser can set or clear this attribute.
-i      A file with the ‘i’ attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser can set or clear this attribute.

 

Example 1:

 

 

Example 2 :

 

 

Main difference between a and i switch is in i you cannot append the file while in a switch you can append the file.

 

Example 3:

 

Advertisements
Published in: on January 31, 2013 at 8:54 am  Comments Off on How to Change the Attributes of a File in Linux using chattr Command  

How To Split Big File into Smaller Files in Linux

split command is used to to split large files into smaller files in Unix.. Syntax of Split command :

 

#  split [options]   filename   prefix

 

Replace filename with the name of the large file you wish to split. Replace prefix with the name you wish to give the small output files. You can exclude [options], or replace it with either of the following:

 

-l  linenumber
-b  bytes
If we use -l (a lowercase L) option, replace linenumber with the number of lines you’d like in each of the smaller files (the default is 1,000). If you use the -b option, replace bytes with the number of bytes you’d like in each of the smaller files.

The split command will give each output file it creates the name prefix with an extension tacked to the end that indicates its order. By default, the split command adds aa to the first output file, proceeding through the alphabet to zz for subsequent files. If you do not specify a prefix, most systems use x.

 

 

Examples  :

  • In this simple example, assume testfile is 3,000 lines long:

     

    # split testfile
    This will output three 1000-line files: xaa, xab, and xac.

     

  • Working on the same file, this next example is little complex: 

    # split -l 500 testfile segment
    This will output six 500-line files: segmentaa, segmentab, segmentac, segmentad, segmentae, and segmentaf.

     

  • Finally, assume testfile is a 160KB file: 

    #  split -b 40k   testfile segment

     

    This will output four 40KB files: segmentaa, segmentab, segmentac, and segmentad.

Published in: on January 31, 2013 at 8:54 am  Comments Off on How To Split Big File into Smaller Files in Linux  

How to reduce or shrink the size of lvm partitions

In my case I will shrink or reduce the /home size using lvreduce command. To reduce lvm size first umount the partition and run e2fsck & resize2fs.

 

Steps to reduce the /home partition to 5GB

 

Step:1 Check the disk space using below command

 

 

Umount /home using below command :

 

[root@localhost ~]# umount  /home

 

Step:2 Use e2fsck command to check the file system

 

[root@localhost ~]# e2fsck -f /dev/mapper/VolGroup-LogVol01

 

Step:3 Resize the partition using below command

 

 

Step:4 Use lvreduce command to reduce the size as shown below

 

 

Step:5 Mount and check the partition size

 

Published in: on January 31, 2013 at 8:51 am  Comments Off on How to reduce or shrink the size of lvm partitions  

How to extend or increase the size of LVM partitions

LVM  stands for Logical Volume Manager. One of the biggest advantage of lvm partitions is  that we can extend  file-system size using lvextend command.

 

I am taking the example of /home partition which is lvm based , currently /home is of 5GB , We will extend the size to 7GB , means we have to  add 2GB space.

 

Steps to Extend the size of /home by 2GB

 

Step:1 Check the space using below command

 

 

Step:2  After extending the size of /home by 2GB, it should be 7GB.

 

Umount /home partition using below command

 

[root@localhost ~]# umount /home/

 

Step:3 Use e2fsck command to check the file system as shown below

 

 

Step:4 Use resize2fs to resize the ext2,ext3 & ext4 file system

 

 

Now Mount /home using mount command and check the space using df command

 

 

Note : lvextend uses free space of the volume group for extending the size.To check the free size of volume group use vgs or vgdisplay command.

Published in: on January 31, 2013 at 8:47 am  Comments Off on How to extend or increase the size of LVM partitions