📜  tar zip 多个文件 (1)

📅  最后修改于: 2023-12-03 14:47:52.029000             🧑  作者: Mango

Tar and Zip multiple files

As a programmer, you might often come across a situation where you need to combine multiple files into a single archive file. This is where the tar and zip commands come in handy.

Tar

The tar command is used to create an archive file, which can contain multiple files and directories. The syntax for creating a tar archive is as follows:

tar -cvf archive.tar file1 file2 dir1 dir2
  • -c option - Creates a new archive file
  • -v option - Verbose mode (prints the name of files as they are being added to the archive)
  • -f option - Specifies the name of the output file

You can use wildcard characters to include multiple files or directories. For example, to include all files in a directory, you can use:

tar -cvf archive.tar /path/to/directory/*

Once you have created a tar archive, you can extract its contents using the following command:

tar -xvf archive.tar
  • -x option - Extracts files from an archive
  • -v option - Verbose mode
  • -f option - Specifies the name of the input file
Zip

The zip command is used to compress files into a single archive file. The syntax for creating a zip archive is as follows:

zip archive.zip file1 file2 dir1 dir2

You can use wildcard characters to include multiple files or directories. For example, to include all files in a directory, you can use:

zip archive.zip /path/to/directory/*

Once you have created a zip archive, you can extract its contents using the following command:

unzip archive.zip

If you want to extract the contents of a zip archive in a specific directory, you can use the -d option:

unzip archive.zip -d /path/to/directory/

Overall, the tar and zip commands are useful tools for organizing and compressing multiple files.