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  

Delete Files Older Than x Days on Linux

The find utility on linux allows you to pass in a bunch of interesting arguments, including one to execute another command on each file. We’ll use this in order to figure out what files are older than a certain number of days, and then use the rm command to delete them.

Command Syntax

find /path/to/files* -mtime +5 -exec rm {} \;

Note that there are spaces between rm, {}, and \;

Explanation

  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
  • The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.

 

http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/

Published in: on October 11, 2010 at 12:03 pm  Comments Off on Delete Files Older Than x Days on Linux