In linux tar packages the implementation method of the specified path file
- 2020-05-15 03:27:07
- OfStack
Compression:
tar czvf /data/backup/test.tar.gz /data/a/b/directory
Extract:
cd /data/test
tar xzvf /data/backup/test.tar.gz
The problem is that after unzip the files in/data/test/data/a/b/directory inside
To unzip in the current directory path.
So that's the solution
tar czvf /data/backup/test.tar.gz /data/a/b/directory
to
tar czvf /data/backup/test.tar.gz -C /data/a/b directory
-C is the temporary switch working directory, -P is the absolute path, where only the -C parameter is used
For example,
To packaging/home YingMu/folders and exclude/home YingMu/ofstack com folder
tar -czvf YingMu.tar /home/YingMu/ --exclude /home/YingMu/ofstack.com
To exclude multiple folders, add multiple directories to the command --exclude directory name, for example:
tar -czvf YingMu.tar /home/YingMu/ --exclude /home/YingMu/ofstack.com --exclude /home/YingMu/QQ
To exclude files, change the directory name above to something like *.gif's rule.
Example 3: will/tmp etc tar. gz file decompression under/usr local/src
[root@linux ~]# cd /usr/local/src
[root@linux src]# tar -xzvf /tmp/etc.tar.gz
By default, we can (www.ofstack.com) unzip the zip file anywhere! In this case,
# I will working directory first transform to/usr/local/src underneath, and unlock/tmp/etc tar. gz,
# is untied directory in/usr/local src/etc! In addition, if you enter/usr local/src/etc
You will find that the file properties in this directory may be different from /etc/.
Example 4: under the/tmp, I just want to/tmp/etc tar. Within gz etc/passwd untied
[root@linux ~]# cd /tmp
[root@linux tmp]# tar -xzvf /tmp/etc.tar.gz etc/passwd
I can check the file name in tarfile through tar-tzvf. If only one file is needed,
# can be sent in this way! Notice! etc.tar.gz root/has been removed!
Example 5: backup all files in /etc/ and save their permissions!
[root@linux ~]# tar -czvpf /tmp/etc.tar.gz /etc
The -p property is important, especially if you want to keep the properties of the original file
tar main command
- c create packages
- x unpacking
-t lists the contents of the package
-r adds files to the specified package
-u updates the files in the package
These are separate commands, and you need to use one of them to compress and uncompress, and you can use it with other commands, but you can only use one of them. The following parameters are optional when compressing or decompressing the file as needed.
An optional command
-j USES bzip2 for compression or decompression when creating or unpacking packages
-z is used to compress or uncompress packages when creating or unpacking them
-Z is used to compress or uncompress packages when creating or unpacking them
-f followed by the specified package file name
-v shows the packaging/unpacking process
-C specifies the path after unpacking