Red Hat [Electronic resources] : The Complete Reference Enterprise Linux Fedora Edition؛ The Complete Reference نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Red Hat [Electronic resources] : The Complete Reference Enterprise Linux Fedora Edition؛ The Complete Reference - نسخه متنی

Richard L. Petersen

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








Archiving and Compressing Files with File Roller


Red Hat provides the File Roller tool (accessible from the Accessories menu) that operates as a GUI front end to archive and compress files, letting you perform zip, gzip, tar, and bzip2 operations using a GUI interface (see Figure 10-4). You can examine the contents of archives, extract the files you want, and create new compressed archives. When you create an archive, you determine its compression method by specifying its file name extension such as .gz for gzip or. bz2 for bzip2. You can select the different extensions from the File type menu, or enter the extension yourself. To both archive and compress files you can choose a combined extension like .tar.bz2, which both archives with tar and compresses with bzip2. Click on the Add button to add files to your archive. To extract files from an archive, open the archive to display the list of archive files. You can then click on the Extract button to extract particular files or the entire archive. Other GUI tools available are guiTAR, LnxZip, Ark, KArchive, and Xtar.


Figure 10-4: File Roller archiving and compression tool


Archive Files and Devices: tar


The tar utility creates archives for files and directories. With tar, you can archive specific files, update them in the archive, and add new files as you want to that archive. You can even archive entire directories with all their files and subdirectories, all of which can be restored from the archive. The tar utility was originally designed to create archives on tapes. The term tar stands for tape archive. You can create archives on any device, such as a floppy disk, or you can create an archive file to hold the archive. The tar utility is ideal for making backups of your files or combining several files into a single file for transmission across a network (File Roller is a GUI interface for tar).





Note

As an alternative to tar you can use pax. pax is designed to work with different kinds of Unix archive formats such as cpio, bcpio, and tar. You can extract, list, and create archives. pax is helpful if you are handling archives created on Unix systems that are using different archive formats.


Displaying Archive Contents


Both file managers in GNOME and the K Desktop have the capability to display the contents of a tar archive file automatically. The contents are displayed as though they were files in a directory. You can list the files as icons or with details, sorting them by name, type, or other fields. You can even display the contents of files. Clicking a text file opens it with a text editor and an image is displayed with an image viewer. If the file manager cannot determine what program to use to display the file, it prompts you to select an application. Both file managers can perform the same kind operation on archives residing on remote file systems, such as tar archives on FTP sites. You can obtain a listing of their contents and even read their readme files. The Nautilus file manager (GNOME) can also extract an archive. Right-click the Archive icon and select Extract.

Creating Archives


On Linux, tar is often used to create archives on devices or files. You can direct tar to archive files to a specific device or a file by using the

f option with the name of the device or file. The syntax for the

tar command using the

f option is shown in the next example. The device or filename is often referred to as the archive name. When creating a file for a tar archive, the filename is usually given the extension .tar. This is a convention only and is not required. You can list as many filenames as you want. If a directory name is specified, all its subdirectories are included in the archive.

$ tar optionsf archive-name.tar directory-and-file-names

To create an archive, use the

c option. Combined with the

f option,

c creates an archive on a file or device. You enter this option before and right next to the

f option. Notice no preceding dash is before a tar option. Table 10-7 lists the different options you can use with tar. In the next example, the directory mydir and all its subdirectories are saved in the file myarch.tar. In this example, the mydir directory holds two files, mymeeting and party, as well as a directory called reports that has three files: weather, monday, and friday.

$ tar cvf myarch.tar mydir
mydir/
mydir/reports/
mydir/reports/weather
mydir/reports/monday
mydir/reports/friday
mydir/mymeeting
mydir/party































































Table 10-7: File Archives:

tar


Commands


Execution


tar options files


Backs up files to tape, device, or archive file.


tar optionsf archive_ name filelist


Backs up files to a specific file or device specified as archive_name. filelist; can be filenames or directories.


Options


c


Creates a new archive.


t


Lists the names of files in an archive.


r


Appends files to an archive.


U


Updates an archive with new and changed files; adds only those files modified since they were archived or files not already present in the archive.


--delete


Removes a file from the archive.


w


Waits for a confirmation from the user before archiving each file; enables you to update an archive selectively.


x


Extracts files from an archive.


m


When extracting a file from an archive, no new timestamp is assigned.


M


Creates a multiple-volume archive that may be stored on several floppy disks.


f archive-name


Saves the tape archive to the file archive name, instead of to the default tape device. When given an archive name, the

f option saves the tar archive in a file of that name.


f device-name


Saves a tar archive to a device such as a floppy disk or tape. /dev/fd0 is the device name for your floppy disk; the default device is held in /etc/default/tar-file.


v


Displays each filename as it is archived.


z


Compresses or decompresses archived files using gzip.


j


Compresses or decompresses archived files using bzip2.


Extracting Archives


The user can later extract the directories from the tape using the

x option. The

xf option extracts files from an archive file or device. The tar extraction operation generates all subdirectories. In the next example, the

xf option directs

tar to extract all the files and subdirectories from the tar file myarch.tar:

$ tar xvf myarch.tar
mydir/
mydir/reports/
mydir/reports/weather
mydir/reports/monday
mydir/reports/friday
mydir/mymeeting
mydir/party

You use the

r option to add files to an already created archive. The

r option appends the files to the archive. In the next example, the user appends the files in the letters directory to the myarch.tar archive. Here, the directory mydocs and its files are added to the myarch.tar archive:

$ tar rvf myarch.tar mydocs
mydocs/
mydocs/doc1

Udating Archives


If you change any of the files in your directories you previously archived, you can use the

u option to instruct tar to update the archive with any modified files. The

tar command compares the time of the last update for each archived file with those in the user's directory and copies into the archive any files that have been changed since they were last archived. Any newly created files in these directories are also added to the archive. In the next example, the user updates the myarch.tar file with any recently modified or newly created files in the mydir directory. In this case, the gifts file was added to the mydir directory.

tar uvf myarch.tar mydir
mydir/
mydir/gifts

If you need to see what files are stored in an archive, you can use the

tar command with the

t option. The next example lists all the files stored in the myarch.tar archive:


tar tvf myarch.tar
drwxr-xr-x root/root 0 2000-10-24 21:38:18 mydir/
drwxr-xr-x root/root 0 2000-10-24 21:38:51 mydir/reports/
-rw-r--r-- root/root 22 2000-10-24 21:38:40 mydir/reports/weather
-rw-r--r-- root/root 22 2000-10-24 21:38:45 mydir/reports/monday
-rw-r--r-- root/root 22 2000-10-24 21:38:51 mydir/reports/friday
-rw-r--r-- root/root 22 2000-10-24 21:38:18 mydir/mymeeting
-rw-r--r-- root/root 22 2000-10-24 21:36:42 mydir/party
drwxr-xr-x root/root 0 2000-10-24 21:48:45 mydocs/
-rw-r--r-- root/root 22 2000-10-24 21:48:45 mydocs/doc1
drwxr-xr-x root/root 0 2000-10-24 21:54:03 mydir/
-rw-r--r-- root/root 22 2000-10-24 21:54:03 mydir/gifts

Archiving to Floppies


To back up the files to a specific device, specify the device as the archive. For a floppy disk, you can specify the floppy drive. Be sure to use a blank floppy disk. Any data previously placed on it will be erased by this operation. In the next example, the user creates an archive on the floppy disk in the /dev/fd0 device and copies into it all the files in the mydir directory:

$ tar cf /dev/fd0 mydir

To extract the backed-up files on the disk in the device, use the

xf option:

$ tar xf /dev/fd0

If the files you are archiving take up more space than would be available on a device such as a floppy disk, you can create a tar archive that uses multiple labels. The

M option instructs tar to prompt you for a new storage component when the current one is filled. When archiving to a floppy drive with the

M option, tar prompts you to put in a new floppy disk when one becomes full. You can then save your tar archive on several floppy disks.

$ tar cMf /dev/fd0 mydir

To unpack the multiple-disk archive, place the first one in the floppy drive and then issue the following

tar command using both the

x and

M options. You are then prompted to put in the other floppy disks as they are needed.

$ tar xMf /dev/fd0

Compressing Archives


The

tar operation does not perform compression on archived files. If you want to compress the archived files, you can instruct tar to invoke the gzip utility to compress them. With the lowercase

z option, tar first uses gzip to compress files before archiving them. The same

z option invokes

gzip to decompress them when extracting files.

$ tar czf myarch.tar.gz mydir

To use

bzip instead of

gzip to compress files before archiving them, you use the

j option. The same

j option invokes

bzip to decompress them when extracting files.

$ tar cjf myarch.tar.bz2 mydir

Remember, a difference exists between compressing individual files in an archive and compressing the entire archive as a whole. Often, an archive is created for transferring several files at once as one tar file. To shorten transmission time, the archive should be as small as possible. You can use the compression utility gzip on the archive tar file to compress it, reducing its size, and then send the compressed version. The person receiving it can decompress it, restoring the tar file. Using gzip on a tar file often results in a file with the extension .tar.gz. The extension .gz is added to a compressed gzip file. The next example creates a compressed version of myarch.tar using the same name with the extension .gz:

$ gzip myarch.tar
$ ls
$ myarch.tar.gz

Archiving to Tape


If you have a default device specified, such as a tape, and you want to create an archive on it, you can simply use

tar without the

f option and a device or filename. This can be helpful for making backups of your files. The name of the default device is held in a file called /etc/default/tar. The syntax for the

tar command using the default tape device is shown in the following example. If a directory name is specified, all its subdirectories are included in the archive.

$ tar option directory-and-file-names

In the next example, the directory mydir and all its subdirectories are saved on a tape in the default tape device:

$ tar c mydir

In this example, the mydir directory and all its files and subdirectories are extracted from the default tape device and placed in the user's working directory:

$ tar x mydir





Note

There are other archive programs you can use such as cpio and shar. However, tar is the one most commonly used for archiving application software.



File Compression: gzip, bzip2, and zip


Several reasons exist for reducing the size of a file. The two most common are to save space or, if you are transferring the file across a network, to save transmission time. You can effectively reduce a file size by creating a compressed copy of it. Anytime you need the file again, you decompress it. Compression is used in combination with archiving to enable you to compress whole directories and their files at once. Decompression generates a copy of the archive file, which can then be extracted, generating a copy of those files and directories. File Roller provides a GUI interface for these tasks.

Compression with gzip


Several compression utilities are available for use on Linux and Unix systems. Most software for Linux systems use the GNU gzip and gunzip utilities. The gzip utility compresses files and gunzip decompresses them. To compress a file, enter the command

gzip and the filename. This replaces the file with a compressed version of it, with the extension .gz.

$ gzip mydata
$ ls
mydata.gz

To decompress a gzip file, use either

gzip with the

-d option or the command

gunzip . These commands decompress a compressed file with the .gz extension and replace it with a decompressed version with the same root name, but without the .gz extension. When you use gunzip, you needn't even type in the .gz extension.

gunzip and

gzip

-d assume it. Table 10-8 lists the different gzip options.

$ gunzip mydata.gz
$ ls
mydata





Tip

On your desktop, you can extract the contents of an archive by locating it with the file manager and double clicking on it. This will start the File Roller application, which will open the archive listing its contents. You can then choose to extract the archive. File Roller will use the appropriate tools to decompress the archive (bzip2, zip, or gzip) if compressed, and then extract archive (tar) (see Chapter 4).


































Table 10-8: The

gzip Options


Option


Execution


-c


Sends compressed version of file to standard output; each file listed is separately compressed:

gzip

-c

mydata

preface

>

myfiles.gz


-d


Decompresses a compressed file; or, you can use gunzip:

gzip

-d

myfiles.gz

gunzip

myfiles.gz


-h


Displays help listing.


-l file-list


Displays compressed and uncompressed size of each file listed:

gzip

-l

myfiles.gz.


-r directory-name


Recursively searches for specified directories and compresses all the files in them; the search begins from the current working directory. When used with

gunzip , compressed files of a specified directory are uncompressed.


-v file-list


For each compressed or decompressed file, displays its name and the percentage of its reduction in size.


- num


Determines the speed and size of the compression; the range is from –1 to –9. A lower number gives greater speed but less compression, resulting in a larger file that compresses and decompresses quickly. –1 gives the quickest compression, but with the largest size; –9 results in a very small file that takes longer to compress and decompress. The default is –6.


You can also compress archived tar files. This results in files with the extensions .tar.gz. Compressed archived files are often used for transmitting extremely large files across networks.

$ gzip myarch.tar
$ ls
myarch.tar.gz

You can compress tar file members individually using the

tar

z option that invokes

gzip . With the

z option, tar invokes gzip to compress a file before placing it in an archive. Archives with members compressed with the

z option, however, cannot be updated nor is it possible to add to them. All members must be compressed and all must be added at the same time.

Compress and Uncompress


You can also use the

compress and

uncompress commands to create compressed files. They generate a file that has a .Z extension and use a different compression format than gzip. The

compress and

uncompress commands are not that widely used, but you may run across .Z files occasionally. You can use the

uncompress command to decompress a .Z file. The gzip utility is the standard GNU compression utility and should be used instead of

compress .

Compressing with bzip2


Another popular compression utility is bzip2

. It compresses files using the Burrows-Wheeler block-sorting text compression algorithm and Huffman coding. The command line options are similar to gzip by design, but they are not exactly the same. See the bzip2 Man page for a complete listing. You compress files using the

bzip2 command and decompress with

bunzip2 . The

bzip2 command creates files with the extension .bz2. You can use

bzcat to output compressed data to the standard output. The

bzip2 command compresses files in block and enables you to specify their size (larger blocks give you greater compression). Like gzip, you can use bzip2 to compress tar archive files. The following example compresses the mydata file into a bzip compressed file with the extension .bz2:

$ bzip2 mydata
$ ls
mydata.bz2

To decompress, use the

bunzip2 command on a bzip file.

$ bunzip2 mydata.bz2

Using Zip


Zip is a compression and archive utility modeled on PKZIP, which was used originally on DOS systems. Zip is a cross-platform utility used on Windows, Mac, MS-DOS, OS/2, Unix, and Linux systems. Zip commands can work with archives created by PKZIP and PKZIP programs and can use Zip archives. You compress a file using the

zip command. This creates a Zip file with the .zip extension. If no files are listed,

zip outputs the compressed data to the standard output. You can also use the

- argument to have

zip read from the standard input. To compress a directory, you include the

-r option. The first example archives and compresses a file:

$ zip mydata
$ ls
mydata.zip

The next example archives and compresses the reports directory:

$ zip -r reports

A full set of archive operations is supported. With the

-f option, you can update a particular file in the zip archive with a newer version. The

-u option replaces or adds files, and the

-d option deletes files from the zip archive. Options also exist for encrypting files, and DOS to Unix end-of-line translations, and including hidden files.

To decompress and extract the Zip file, you use the

unzip command.

$ unzip mydata.zip


/ 328