File compressing and archiving, processes and job scheduling

Day 5: File compressing and archiving, processes and job scheduling.

When we want to reduce size of file or want to send some useful information over network, for that we have file compressing scheme. We can compress a group files to save disk space or we can transfer files in less time with more accuracy over network. A compressed file is collection of files and directories that are stored in such a way that it uses less disk space than all the individual files and directories combined.
File compressing tools
We can compress Linux files and directories by some ways one is bzip2 and other is gzip having the file extension .bz2 and .gz. We have also some Linux commands like .zip, gzip extensions. We can also use tar command to compressed files.

  1. Bzip2 and Buzip2 : To compress files just type the bzip2 and filename ,the fie will saved as filename.bz2 .The command is as shown below.
# bzip2 filename

For example, we want to compress file file1 .If we used above command it will converted into file1.bz2.

# bzip2 file1

To expand this compress file back to original .just type bunzip2 compressed filename.

# bunzip2 file1.bz2

It will delete the compressed file and converted into unzip file. We can compressed more than one file located in a specific directory. The files file1, file2, file3 is located in a directory like /momo/maker/ can be compressed into single folder.

# bzip2 document.bz2 file1 file2 file3 /momo/maker
  1. Gzip and Gunzip : To use the gzip to compress file ,enter following command into shell prompt.
# gzip filename

The filename is compressed and saved as filename.gz. For example, we want to compress mytool file using gzip and converted into mytool.gz.

# gzip mytool

Again to unzip mytool file ,enter the following command

# gunzip mytool.gz

It will deleted compressed file and expand into original file.

  1. Zip and Unzip : Similar to gzip we can use zip command to compressed file ,enter the following command
# zip -r filename.zip filedir

Here, filename.zip file represents compressed file and filedir is directory where you put your new zip file. The – r option specifies that you want to include all files contained in filedir directory recursively. To extract contents in a zip file ,enter the following command

# unzip filename

We can also compress multiple files and directories at same time using zip command. For example,

# zip -r myfile.zip file1 file2 file3 /usr/work/mydata

The above command will compressed files file1,file2,file3 placed in a directory /usr/work/mydata and coverted into myfile.zip.


Archiving file using tar command


An archive file is collection of files and directories stored in one file. It uses the same amount of disk space as all files and directories combined.A tar command is collection of several files or directories in one file. Some options of tar commands
• -c : create new archive.
• -t : show the list of files in the tar file.
• -v : show the progress of files being archived .
• -x : extract files from an archive
• -z : compress the tar file with gzip
• -j : compress the tar file with bzip2
To create tar file ,enter the following command

# tar -cvf filename.tar directory/file

Here the filename.tar is compressed file and directory/file represents the directory and file you want to put in archived file.We can also use tar for compressing file and directories at the same time.Similarly to extract the contents of files use the command following,

# tar -xvf filename.tar

To create a tarred and bzipped compressed file, use the -j option and file extension would be filename.tbz.

# tar -cjvf filename.tbz file

The above command creates an archive file and then compresses it as the file filename.tbz. If you uncompresses it will removed filename.tbz and converted into filename.tar.You can expand and unarchive a bzip tar file in one command.

# tar –xjvf filename.tbz

To create a tarred and gzipped compressed file, use -z option ,

# tar -czvf filename.tgz file.


Processes, job scheduling and controlling:

In computer operating system, there are many processes running to regulate the work properly. Some processes are background and some are foreground. Background process is task in which we can wait and complete through shell. The background process are task other than foreground it means we can also run other processes while background process are running.
Some foreground processes we can run with the commands which gets finished quickly. But some of the foreground processes may take time to execute. So we can run these processes in background .When process completes, the system will give you message. First of all we will learn commands that are displaying and controlling processes, then we will see how foreground and background processes are run. Finally we will come to job scheduling using crontab.
Job controlling: displaying processes, background and foreground process

To display the currently running processes use the ps command. If no options are specified, ps displays the processes associated with your current session. We have some options of ps command as follow.

  • -f – Full format listing.
  • -u username – Display processes running as username.
  • -p pid – Display process information for pid. A PID is a process ID.
  • Common ps commands:
  • ps -e – Display all processes.
  • ps -ef – Display all processes.
  • ps -eH – Display a process tree.
  • ps -e –forest – Display a process tree.
  • ps -u username – Display processes running as username.
  • -e – Everything , all processes .

Foreground and Background Processes:

When a command, process, or program is running in the foreground the shell prompt will not be displayed until that process exits.
Processes that are backgrounded still execute and perform their task, however they do not block you from entering further commands at the shell prompt.
To background a process, place an ampersand (&) at the end of the command. Some following commands for processes.command & – Start command in the background.

  • Ctrl-c – Kill the foreground process.
  • Ctrl-z – Suspend the foreground process.
  • bg [%num] – Background a suspended process.
  • fg [%num] – Foreground a background process.
  • kill [%num] – Kill a process by job number or PID.
  • jobs [%num] – List jobs

The plus sign (+) in the jobs output represents the current job while the minus sign (-) represents the previous job.The current job and previous job can be referred to by %% or %+ and %-. To bring a job back to the foreground, type the name of the job or use the fg command. To foreground the current job execute %%, %+, fg, fg %%, fg %+, or fg %num. To foreground job number 3, execute %3 or fg %3.

You can stop or kill a background job using the kill command. For example, to kill job number 1 execute kill %1. To kill a job that is running in the foreground, type Ctrl-c.


Scheduling job using crontab

If you need to repeat a task on a schedule, you can use the cron service. Every minute the cron service checks to see if there are any scheduled jobs to run and if so runs them. Cron jobs are often used to automate a process or perform routine maintenance. You can schedule cron jobs by using the crontab command.

  • cron – A time based job scheduling service. This service is typically started when the system boots.
  • crontab – A program to create, read, update, and delete your job schedules.

A crontab (cron table) is a configuration file that specifies when commands are to be executed by cron. Each line in a crontab represents a job and contains two pieces of information: 1) when to run and 2) what to run.


Crontab Format

* * * * command
| | | | |
| | | | +– Day of the Week (0-6)
| | | +—- Month of the Year (1-12)
| | +—— Day of the Month (1-31)
| +——– Hour (0-23)
+———- Minute (0-59)
The command will only be executed when all of the time specification fields match the current date and time. You can specify that a command be run only once, but this is not the typical use case for cron. Typically, one or more of the time specification fields will contain an asterisk (*) which matches any time or date for that field. Here is an example crontab. Run every Monday at 07:00

0 7 * * 1 /opt/sales/bin/weekly-report
Here is a graphical representation of the above crontab entry.
0 7 * * 1 /opt/sales/bin/weekly-report
| | | | |
| | | | +– Day of the Week (0-6)
| | | +—- Month of the Year (1-12)
| | +—— Day of the Month (1-31)
| +——– Hour (0-23)
+———- Minute (0-59)
This job will run only when the minute is 0, the hour is 7, and the day of the week is 1. In the day of the week field 0 represents Sunday, 1 Monday, etc. This job will run on any day and during any month since the asterisk was used for those two fields.

Example.

Run at 02:00 every day and send output to a log.

0 2 * * * /opt/acme/bin/backup > /tmp/backup.log 2>&1
You can provide multiple values for each of the fields. If you would like to run a command every half-hour, you could do this.

Run every 30 minutes.

0,30 * * * * /opt/acme/bin/half-hour-check

Another way to do the same thing.

*/2 * * * * /opt/acme/bin/half-hour-check
Instead of using 0,30 for the minute field you could have used */2. You can even use ranges with a dash. If you want to run a job every minute for the first four minutes of the hour you can use this time specification: 0-4 * * * * command.

In next post, you will learn about Vi editor and file editing using vi…

Leave a comment