Linux tar command use the _linux 1th/2 page

Source: Internet
Author: User
Tags mkdir touch
Create an archive with tar

Tar is the abbreviation for "Tape Archive" (Tape archive), which is the early stage of a computer that does not yet have a floppy disk drive, hard drive, and optical drive. When software releases and backups require large volumes of tape, one of the first few programs running on a computer has to be a reading program for the tape. Over time, the tar command became a tool for archiving many files, and many of the current programs for Linux operating systems were packaged in the form of tar files.

The format of the tar command is:

tar [option] <file>;

The tar command has many parameters, using the F option, the device, or the name of the file to instruct the tar command to place the file in a specific device or file. When a file is created for the tar file, the extension of the filename is usually if a directory name is specified, all of its subdirectories are included in the file.

The format for using the TAR command and the F option is as follows:

#tar opionsf Archive-name.tar Directory-and-filenames

To create a file you should use the C option, along with the F option, the C option creates the file in the device or file, and the option should be entered on the left side of the F option. In the following example, the directory Mydir and all of its subdirectories are stored in the file Myarch.tar.

#tar CF Myarch.tar Mydir

If you want to change the files in the archived directory, you can use the U option to make tar update the files in the file. Tar compares the last modification time of each profile with the file in the corresponding user directory, and then copies the files that were modified after the last archive to the file. Newly created files in the user directory are also added to the profile. In the following example, the user updates the Myarch.tar file with a file that was recently modified or added to the Mydir directory.

#tar uf Myarch.tar Mydir

If you want to see which files are stored in one file, you can use the tar command and the T option. The following command lists all files stored in the Myarch.tar file:

#tar TF Myarch.tar

To back up a file to a specific device, simply use the device name as a file. In the following example, the user creates a file on the floppy disk of the/DEV/FDO device and copies all the files in the Mydir directory to the file.

#tar Cf/dev/fdo Mydir

To recover files on a device disk, use the XF option:

#tar Xf/dev/fdo

If you are backing up a file that is larger than the device's available storage, such as a floppy disk, you can create a tar file that uses multiple labels at this point. When the M option is used to archive a floppy disk drive, the tar command reminds you to put a new floppy disk when a floppy disk is full.

#tar Cmf/dev/fdo Mydir

To recover files from several disks, simply put the first floppy disk in the floppy drive and enter the TAR command with the X and M options. If necessary, be reminded to put a different floppy disk.

#tar Xmf/dev/fdo

The tar operation does not compress the profile. If you want to compress your profile, you can instruct tar to activate the Gzip utility and compress the files before archiving. With the z option, tar first uses gzip to compress files before archiving them. When the file is recovered, the same z option activates gzip to extract the file.

#tar Czf Myarch.tar Mydir

It is worth noting that compressed files in a single file and the overall compressed file is different. Typically, files are used to combine several files into a tar file for easy transmission. In order to shorten the transmission time, the file should be smaller and better, you can compress it and then transfer the compressed version. The receiver can decompress it and then restore the tar file. Using gzip on the tar file will result in a file with the. tar.gz extension. The extension. GZ is added after the compressed gzip file name.

The following example creates a compressed version of Myarch.tar that has a. gz extension.

#gzip Myarch.tar
#ls
#myarch. tar.gz

The tar command has a lot of parameters, but it's not difficult to use, and it's quick and easy to generate files for any subdirectories you need.

First, we'll build a subdirectory with three files, and then create a subdirectory with another three files, as follows:

# mkdir Mydir
#cd Mydir
# Touch File1 file2 file3
# mkdir Mydir2
#cd Mydir2
# Touch File21 File22 File23
#cd. /..
#tree Mydir
Mydir
File1
File2
File3
Mydir2
File21
File22
File23

Now that you have a subdirectory and a file in it, use the C (build) and F (file) parameters of this command to generate a tar profile:

# tar CF Mydir.tar Mydir
# ls-l *.tar
-R W-r--R--1 bball u s e R S 10240 5 15:01 Mydir. T a R

Note that the original subdirectory has not changed. By default, the tar command does not delete the original subdirectories and files. If you want to see the process of command execution, you can use the V parameter as follows:

# tar CVF mydir.tar Mydir
mydir/
Mydir/file1
Mydir/file2
Mydir/file3
mydir/mydir2/
Mydir/mydir2/file21
Mydir/mydir2/file22
Mydir/mydir2/file23

When the tar command is in operation, the file names of subdirectories and files that are being added to the profile are displayed. Use the W parameter, the interaction parameter, so that the tar command asks if you want to join each file during execution. This is handy when you want to selectively back up a subdirectory with little content, as follows:

# tar CWF mydir.tar Mydir
Add Mydir?y
Add Mydir/file1?n
Add Mydir/file2?y
Add Mydir/file3?n
Add Mydir/mydir2?y
Add Mydir/mydir2/file21?y
Add Mydir/mydir2/file22?n
Add Mydir/mydir2/file23?y

In the above example, file File1, File3, and file File22 are not archived. You can use the T parameter of the TAR command to list the contents of the file, the F parameter defines the TAR profile used by the operation, as follows:

# tar TF Mydir.tar
mydir/
Mydir/file2
mydir/mydir2/
Mydir/mydir2/file21
Mydir/mydir2/file23

It should be noted that if the order of the parameters is misplaced, the tar command displays an error message and exits.

Let's take a look at how to release the entire file or one of the files. If you want to free all of these files, you can use-X to release both parameters and-F. To understand the procedure for command execution, you can also add the-V parameter:

# tar XVF Mydir.tar
mydir/
Mydir/file2
mydir/mydir2/
Mydir/mydir2/file21
Mydir/mydir2/file23
Current 1/2 page 12 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.