How to Archive Files in Linux Using tar, jar

Archiving Files

To safeguard your files and directories, you can create a copy, or archive of the files and directories on a removable medium, such as a cartridge tape. You can use the archived copies to retrieve lost, deleted, or damaged files.

Archiving Techniques

You can use several commands to store, locate, and retrieve files on a tape device or from an archive file. Two of the commands you can use are:

  • The tar command, to create and extract files from a file archive or any removable media such as a tape or diskette.
  • The jar command, to combine multiple files into a single archive file.

The tar Command

The tar command archives files to and extracts files from a single file called a tar file. The default device for a tar file is a magnetic tape device. The syntax for the tar command is:

tar functions archivefile filenames

The table below shows the functions associated with the tar command.

Function Definition
c Creates a new tar file.
t Lists the table of contents of the tar file.
x Extracts files from the tar file.
f Specifies the archive file or tape device. The default tape device is /dev/rmt/0. If the name of the archive file is “-”, the tar command reads from the standard input when reading from a tar archive or writes to the standard output if creating a tar archive.
v Executes in verbose mode, writes to the standard output.
h Follows symbolic links as standard files or directories.

Creating an Archive

You can use the tar command to create an archive file that contains multiple files or directories. You can then place the file on a tape or a diskette so that other users can share the file or attach it to email messages.

Creating an Archive on a Tape

To create an archive on a tape, first verify that the system has a tape drive available. You can use the mt utility with the status option to print status information about the tape unit. You use the mt utility to send commands to a magnetic tape drive.

The following example shows you how to use the default tape device to archive your home directory. In this example, user1 creates a tape archive of the user1 home directory.

$ cd
$ mt -f /dev/rmt/0 status

$ tar cvf /dev/rmt/0 .
a ./ 0 tape blocks
a ./.rhosts 1 tape blocks
a ./dante 3 tape blocks
a ./fruit 1 tape blocks
a ./dante_1 1 tape blocks
a ./dir1/ 0 tape blocks
a ./dir1/coffees/ 0 tape blocks
a ./dir1/coffees/beans/ 0 tape blocks
a ./dir1/coffees/beans/beans 24 tape blocks
... (output truncated)

You can also use the tar command to create an archive file containing multiple files or directories. The following example shows you how to archive the file1, file2, and file3 files in an archive file called files.tar.

$ cd
$ tar cvf files.tar file1 file2 file3
a file1 2K
a file2 1K
a file3 1K

Viewing an Archive

You can view the names of all the files that have been written directly to a tape archive or a file archive.

Viewing an Archive From a Tape

To view the contents of the user1 home directory on a tape, perform the command:

$ tar tf /dev/rmt/0
/.rhosts
./dante
./fruit
./dante_1
./dir1/
./dir1/coffees/
./dir1/coffees/beans/
./dir1/coffees/beans/beans
./dir1/coffees/nuts
./dir1/coffees/brands
./dir1/fruit/
./dir1/trees/

Viewing Files in an Archive File

To view the contents of the files.tar archive file, perform the command:

$ tar tf files.tar
file1
file2
file3

Retrieving tar Archive Data

You can retrieve or extract the contents of an archive that was written directly to a tape device or to a file.

Retrieving a Directory From a Tape

If the contents of your home directory are deleted, you can extract the directory contents from an archive tape.

To retrieve all the files from the tape archive, perform the commands:

$ cd
$ tar xvf /dev/rmt/0
x ., 0 bytes, 0 tape blocks
x ./.rhosts, 2 bytes, 1 tape blocks
x ./dante, 1319 bytes, 3 tape blocks
x ./fruit, 57 bytes, 1 tape blocks
x ./dante_1, 368 bytes, 1 tape blocks
x ./dir1, 0 bytes, 0 tape blocks
x ./dir1/coffees, 0 bytes, 0 tape blocks
x ./dir1/coffees/beans, 0 bytes, 0 tape blocks
x ./dir1/coffees/beans/beans, 12288 bytes, 24 tape blocks
x ./dir1/coffees/nuts, 0 bytes, 0 tape blocks
x ./dir1/coffees/brands, 0 bytes, 0 tape blocks
x ./dir1/fruit, 0 bytes, 0 tape blocks
x ./dir1/trees, 0 bytes, 0 tape blocks
... (output truncated)

You can extract files from an archive file using the tar command. The following example shows you how to extract files from the files.tar archive file for placement into the current directory.

$ tar xvf files.tar
tar: blocksize = 11
x file1, 1610 bytes, 4 tape blocks
x file2, 105 bytes, 1 tape blocks
x file3, 218 bytes, 1 tape blocks

Compressing and Archiving Files by Using the jar Command

This section describes how to use the jar command to compress and archive files. The jar command combines multiple files into a single archive file.

The jar Command

The jar command has syntax similar to the tar command, but the jar command compresses the named files in addition to archiving the files. The jar command copies and compresses multiple files into a single archive file. The jar command is available on any system that uses Java virtual machine (JVM) software.

The syntax for the jar command is:

$ jar options destination filenames

Options for the jar Command

The table below shows the options you can use with the jar command.

Option Definition
c Creates a new jar file.
t Lists the table of contents of a jar file.
x Extracts the specified files from the jar file.
f Specifies the jar file to process (for example, /tmp/file.jar). The jar command sends the data to the screen (stdout) if the f option is not used.
v Executes in verbose mode.

Adding All the Files in a Directory to an Archive

The following example shows how to use the jar command to copy and compress multiple files into a single archive file called bundle.jar.

$ ls
dante dir2 dir5 file.2 file3 fruit tutor.vi
dante_1 dir3 file.1 file2 file4 fruit2
dir1 dir4 file1 file.3 files.tar practice
$ jar cvf /tmp/bundle.jar *
added manifest
adding: dante(in = 1319) (out= 744)(deflated 43%)
adding: dante_1(in = 368) (out= 242)(deflated 34%)
adding: dir1/(in = 0) (out= 0)(stored 0%)
adding: dir1/coffees/(in = 0) (out= 0)(stored 0%)
adding: dir1/coffees/beans/(in = 0) (out= 0)(stored 0%)
adding: dir1/coffees/beans/beans(in = 12288) (out= 3161)(deflated 74%)
adding: dir1/coffees/nuts(in = 0) (out= 0)(stored 0%)
adding: dir1/coffees/brands(in = 0) (out= 0)(stored 0%)
adding: dir1/fruit/(in = 0) (out= 0)(stored 0%)
adding: dir1/trees/(in = 0) (out= 0)(stored 0%)
 ... (output truncated)