Friday, 28 February 2014

File Compression and Archiving with Gzip, Zip, and Tar

Compressing with Gzip and Zip

Compressed files use less disk space and download faster than large, uncompressed files. You can compress Linux files with the open-source compression tool Gzip or with Zip, which is recognized by most operating systems.
By convention, compressed files are given the extension .gz. The command Gzip creates a compressed file ending with .gz; Gunzip extracts the compressed files and removes the .gz file.
To compress a file, at a shell prompt, type the following command:
gzip filename.ext
The file will be compressed and saved as filename.ext.gz.
To expand a compressed file, type:
gunzip filename.ext.gz
The filename.ext.gz is deleted and replaced with filename.ext.
If you exchange files with non-Linux users, you may want to use zip to avoid compatibility problems. Red Hat Linux can easily open zip or gzip files, but non-Linux operating systems may have problems with gzip files.
To compress a file with zip, type the following:
zip -r files
In this example, filename represents the file you are creating and files represents the files you want to put in the new file:
To extract the contents of a zip file, type:
You can zip or gzip multiple files at the same time. List the files with a space between each one.
gzip filename.gz file1 file2 file3 /user/work/school 
The above command will compress file1, file2, file3, and the contents of the /user/work/school directory and put them in filename.gz.

Archiving with Tar

Tar files place several files or the contents of a directory or directories in one file. This is a good way to create backups and archives. Usually, tar files end with the .tar extension.
To create a tar file, type:
tar -cvf filename.tar files/directories
In this example, filename.tar represents the file you are creating and files/directories represents the files or directories you want to put in the new file.
You can use absolute or relative pathnames for these files and directories (for more on pathnames, see the section called Changing Directories with cd in Chapter 10). Separate the names of files and directories with a space.
The following input would create a tar file using absolute pathnames:
  tar -cvf foo.tar /home/mine/work /home/mine/school
The above command would place all the files in the /work subdirectory and the /school subdirectory in a new file called foo.tar in the current working directory.
The command tar -cvf foo.tar file1.txt file2.txt file3.txt would place file1.txt, file2.txt and file3.txt in a new file called foo.tar.
To list the contents of a tar file, type:
 tar -tvf foo.tar
To extract the contents of a tar file, type:
tar -xvf foo.tar
This command does not remove the .tar file, but it places copies of the .tar contents in the current working directory.
The tar command does not compress files automatically. You can compress tar files with:
tar -czvf foo.tar
Compressed tar files are conventionally given the extension .tgz and are compressed with gzip.
To expand a compressed tar file type:
tar -xzvf foo.tgz

Thursday, 13 February 2014

Welcome to my blog!!!

Warm welcome to all linux newbies.. You will find a lot of interesting stuffs here which you can understand easily. Experienced guys please excuse..This is just a beginners guide....:)

Cron jobs tutorial for beginners

Cron is a utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be executed at specified times.


crontab filename Install filename as your crontab file.
crontab -e --> Edit your crontab file.
crontab -l --> Show your crontab file.
crontab -r --> Remove your crontab file.
crontab -v --> Display the last time you edited your crontab file

Inorder to setup a cron you just need to specify the task and the time at which it should be executed. Definitely it should be in the format as below. It can be a little tough at the beginning, but will be much easier if you learn how to specify the time at which a particular task should be executed. Workout with examples to get more idea about cron jobs.

Crontab syntax :-

A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

minute hour day month day-of-week command-line-to-execute
0-59 0-23 1-31 1-12 0-7

In a linux server you can find cron entry for a user at /var/spool/cron/username


if you want a certain command to run at 5.30 am, you will have to code it as:
30 5 * * * command to execute

If you want something run at 8 pm everyday, it has to be coded as:
0 20 * * * command to execute (20 is 8 pm in the 24-hour time format)

If you want to set the cron job every sunday at midnight 11.30 PM
30 23 * * 0 command to execute (0--represents the Sunday)

If you want to run the task at 1am and 2am only from Monday to Friday:
* 1,2 * * 1-5 command to execute

If you want to execute a cronjob at 4 am every Sunday:
00 4 * * 0 command to execute

To execute a cronjob at 4:42 am every 1st of the month
42 4 1 * * command to execute

Cron also supports 'step' values.
A value of */2 in the date of month field means that the command runs every two days and */5 in the hours field would mean the command runs every 5 hours.

Difference between */5 * * * * and 5 * * * *

*/5 * * * * command to execute -->> This will execute the cronjob in every 5

5 * * * * command to execute -->> This will execute cron job in 5th minute of every hour

If both the day of month and day of week are specified, the command will be executed when either of the events happen.
* 12 16 * 1 command to execute -->> command will be executed on every Monday and every 16th

Cron also accepts lists in the fields. Lists can be in the form, 1,2,3 (meaning 1 and 2 and 3) or 1-3 (also meaning 1 and 2 and 3)
59 11 * * 1,2,3,4,5 command to execute -->> will execute the command at 11:59 Monday, Tuesday, Wednesday, Thursday and Friday

to be continued...


Wednesday, 12 February 2014

IP Tables

                             Basic iptables Syntax

Firewalls built with Netfilter are built through the iptables firewall administration command.
The iptables command implements the firewall policies that you create and manages the
behavior of the firewall. Netfilter firewalls have three individual tables: filter, NAT, and man-
gle. Within these tables, firewalls are built through chains, with each individual link in the
chain being an individual iptables command.
Within the default filter table there is a chain for input or data coming into the firewall, a
chain for output or data leaving the firewall, a chain for forwarding or data being sent through
the firewall, and other chains including chains named and configured by the user, commonly
(and appropriately) called user-defined chains. The NAT and mangle tables have specialty
chains that will be discussed later. For now, it’s sufficient to know that the filter table is the
default table for implementing a basic firewall, the NAT table is used to provide NAT and
related functions, and the mangle table is used when the packet will be altered by the firewall.
iptables commands are issued with very specific syntax. Many times, the ordering of the
options given to iptables makes the difference between a successful command and a syntax
error. The commands issued to iptables fall through, so a command that allows certain pack-
ets that follows a command that denies those same packets will cause the data to be dropped
by the firewall.
The basic syntax for an iptables command begins with the iptables command itself, followed
by one or more options, a chain, a set of match criteria, and a target or disposition. The layout
of the command largely depends on the action to be performed. Consider this syntax:
iptables <option> <chain> <matching criteria> <target>
In building a firewall, the option is usually -A to append a rule onto the end of the ruleset.
Naturally, there are several options depending on the target and the operation being per-
formed. This chapter covers most of those options.
As previously stated, the chain can be an input chain, an output chain, a forwarding chain, or
a user-defined chain. In addition, the chain might also be a specialty chain contained in the
NAT or mangle tables.