In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to archive files and directories in Linux". In daily operation, I believe many people have doubts about how to archive files and directories in Linux. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to archive files and directories in Linux". Next, please follow the editor to study!
Archive files and directories
The most common programs for archiving files and directories are:
Tar
Zip
This is a big topic, so I will publish this article in two parts. In the * * section, we will see how to use the tar command to archive files and directories.
Use the tar command to archive files and directories
Tar is a Unix command that stands for Tape Archive (tape archiving). It is used to combine or store multiple files (the same or different sizes) in a single file. There are four main modes of operation in the tar utility.
C-create an archive from a file or directory
X-extraction and archiving
R-append files to the archive
T-list the contents of the archive
For a complete list of patterns, see the man man page.
Create a new archive
For the purposes of this guide, I will use a folder called ostechnix, which contains three different types of files.
$ls ostechnix/file.odt image.png song.mp3
Now, let's create a new tar archive for the ostechnix directory.
$tar cf ostechnix.tar ostechnix/
Here, the c flag refers to the creation of a new archive, and f is the specified archive file.
Similarly, create an archive file for a set of files in the current working directory, using the following command:
$tar cf archive.tar file1 file2 file 3 extraction and archiving
To extract the archive file from the current directory, simply do the following:
$tar xf ostechnix.tar
We can also use the C flag (capital C) to extract the archive to a different directory. For example, the following command extracts the archive file into the Downloads directory.
$tar xf ostechnix.tar-C Downloads/
Alternatively, go to the Downloads folder and extract the archive as shown below.
$cd Downloads/$ tar xf.. / ostechnix.tar
Sometimes, you may want to extract specific types of files. For example, the following command extracts files of type ".png".
$tar xf ostechnix.tar-- wildcards "* .png" creates a compressed archive in gzip and bzip format
By default, tar creates an archive file that ends with .tar. In addition, the tar command can be used in conjunction with the compression utilities gzip and bzip. The file ends with a .tar extension using normal tar to archive the file, the file ends with tar.gz or .tgz and is archived and compressed with gzip, and the file ends with tar.bz2 or .tbz with bzip archived and compressed.
First, let's create a gzip archive:
$tar czf ostechnix.tar.gz ostechnix/
Or:
$tar czf ostechnix.tgz ostechnix/
Here, we use the z flag to compress the archive using the gzip compression method.
You can use the v flag to check progress when creating an archive.
$tar czvf ostechnix.tar.gz ostechnix/ostechnix/ostechnix/file.odtostechnix/image.pngostechnix/song.mp3
Here, v means to show progress.
Create a gzip archive from a list of files:
$tar czf archive.tgz file1 file2 file3
To extract the gzip archive file in the current directory, use:
$tar xzf ostechnix.tgz
To extract to a different folder, use the-C flag:
$tar xzf ostechnix.tgz-C Downloads/
Now, let's create a bzip archive. To do this, use the j flag below.
Create an archive of a directory:
$tar cjf ostechnix.tar.bz2 ostechnix/
Or
$tar cjf ostechnix.tbz ostechnix/
Create an archive from a list file:
$tar cjf archive.tar.bz2 file1 file2 file3
Or
$tar cjf archive.tbz file1 file2 file3
To show progress, use the v flag.
Now, in the current directory, let's extract a bzip archive. Do this:
$tar xjf ostechnix.tar.bz2
Alternatively, extract the archive file to another directory:
$tar xjf ostechnix.tar.bz2-C Downloads creates archives of multiple directories and / or files at a time
This is another coolest feature of the tar command. To create a gzip archive of more than one directory or file at a time, use the following files:
$tar czvf ostechnix.tgz Downloads/ Documents/ ostechnix/file.odt
The above command creates an archive of file.odt files in the Downloads, Documents directory, and ostechnix directory, and saves the archive in the current working directory.
Skip directories and / or files when creating an archive
This is very useful when backing up data. You can exclude unimportant files or directories from the backup, which is what the-exclude option can help. For example, you want to create an archive of the / home directory, but you don't want to include Downloads, Documents, Pictures, Music.
This is what we do:
$tar czvf ostechnix.tgz / home/sk-exclude=/home/sk/Downloads-exclude=/home/sk/Documents-exclude=/home/sk/Pictures-exclude=/home/sk/Music
The above command will create a gzip archive of my $HOME directory, excluding the Downloads, Documents, Pictures, and Music directories. To create a bzip archive, replace z with j and use the extension .bz2 in the example above.
List archive files but do not extract them
To list the contents of the archive, we use the t flag.
$tar tf ostechnix.tarostechnix/ostechnix/file.odtostechnix/image.pngostechnix/song.mp3
To see the detailed output, use the v flag.
$tar tvf ostechnix.tardrwxr-xr-x sk/users 0 2018-03-26 19:52 ostechnix/-rw-r--r-- sk/users 9942 2018-03-24 13:49 ostechnix/file.odt-rw-r--r-- sk/users 36013 2015-09-30 11:52 ostechnix/image.png-rw-r--r-- sk/users 112383 2018-02-22 14:35 ostechnix/song.mp3 additional files to the archive
Files or directories can be added / updated to an existing archive using the r flag. Look at the following command:
$tar rf ostechnix.tar ostechnix/ sk/ example.txt
The above command adds a directory named sk and a directory named exmple.txt to the ostechnix.tar archive.
You can verify that the file has been added using the following command:
$tar tvf ostechnix.tardrwxr-xr-x sk/users 0 2018-03-26 19:52 ostechnix/-rw-r--r-- sk/users 9942 2018-03-24 13:49 ostechnix/file.odt-rw-r--r-- sk/users 36013 2015-09-30 11:52 ostechnix/image.png-rw-r--r-- sk/users 112383 2018-02-22 14:35 ostechnix/song.mp3drwxr-xr-x sk/users 0 2018-03-26 19:52 sk/ -rw-r--r-- sk/users 0 2018-03-26 19:39 sk/linux.txt-rw-r--r-- sk/users 0 2018-03-26 19:56 example.txtTL DR
Create a tar archive:
Normal tar Archive: tar-cf archive.tar file1 file2 file3
Gzip tar Archive: tar-czf archive.tgz file1 file2 file3
Bzip tar Archive: tar-cjf archive.tbz file1 file2 file3
Extract tar Archive:
Normal tar Archive: tar-xf archive.tar
Gzip tar Archive: tar-xzf archive.tgz
Bzip tar Archive: tar-xjf archive.tbz
We only introduced the basic usage of the tar command, which is enough to start using the tar command. However, if you want more details, see the man man page.
Man tar at this point, the study on "how to archive files and directories in Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.