Home >php教程 >PHP开发 >How to implement tar packaging of specified path files in Linux

How to implement tar packaging of specified path files in Linux

高洛峰
高洛峰Original
2017-01-07 13:38:552281browse

Compression:

tar czvf /data/backup/test.tar.gz /data/a/b/directory

Decompression:

cd /data/test
tar xzvf /data/backup/test.tar.gz

The problem is that the decompressed file is in /data/test/data/a/b/directory

I want to extract it in the current directory path.


Writing like this can solve the problem

tar czvf /data/backup/test.tar.gz /data/a/b/directory

Change to

tar czvf /data/backup/test.tar.gz -C /data/a/b directory

-C is to temporarily switch the working directory, -P is the absolute path, only use it here Just go to the -C parameter


For example

To package the /home/YingMu/ folder and exclude the /home/YingMu/jb51.net folder

tar -czvf YingMu.tar /home/YingMu/ --exclude /home/YingMu/jb51.net

To exclude multiple folders, add multiple directories in the command --exclude directory name, for example:

tar -czvf YingMu.tar /home/YingMu/ --exclude /home/YingMu/jb51.net --exclude /home/YingMu/QQ

If you want to exclude files, change the directory name above. It can be an exclusion rule similar to *.gif.


Example 3: Extract the /tmp/etc.tar.gz file under /usr/local/src

[root@linux ~]# cd /usr/local /src

[root@linux src]# tar -xzvf /tmp/etc.tar.gz

# In the default case, we can (www.jb51.net) Unzip archives anywhere! For this example,

# I first change the working directory to /usr/local/src and extract /tmp/etc.tar.gz,

# then extract The directory will be in /usr/local/src/etc! In addition, if you enter /usr/local/src/etc

#, you will find that the file attributes in this directory may be different from /etc/!

Example 4: Under /tmp, I just want to unlock etc/passwd in /tmp/etc.tar.gz

[root@ linux ~]# cd /tmp

[root@linux tmp]# tar -xzvf /tmp/etc.tar.gz etc/passwd
# I can check the tarfile through tar -tzvf File name, if you only need one file,

# can be downloaded through this method! Notice! The root directory / in etc.tar.gz has been removed!


Example 5: Back up all the files in /etc/ and save their permissions!

[root@linux ~]# tar -czvpf /tmp/etc.tar.gz /etc
# The -p attribute is very important, especially when you want to retain the attributes of the original file

tar Main commands

-c Create package
-x Unpack
-t List the contents of the package
-r Add files to the specified package
-u Update files in the package

These five are independent commands. One of them is used for compression and decompression. It can be used in conjunction with other commands but only one of them can be used. The following parameters are optional when compressing or decompressing archives as needed.

Optional commands

-j Use bzip2 to compress or decompress when creating or unpacking the package
-z Use gzip to compress or decompress when creating or unpacking the package
- Z Use compress to compress or decompress when creating or unpacking a package
-f followed by the specified package file name
-v Display the packaging/unpacking process
-C Specify the path after unpacking

The above method of implementing tar packaging of specified path files in Linux is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more related articles on how to implement tar packaging of specified path files in Linux, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn