“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

preface

I haven’t written Linux commands in a long time, so this article is like a note.

In the use of Linux, it is inevitable to decompress or compress files, especially on the server, there is no GUI interface, with Shell operation, we need to know some commands:

  • zip&unzipThe: ZIP file format is a data compression and document storage file format. The original name of the file format is Deflate. It is available on LinuxzipCommand to compress or increase or decrease it; useunzipYou can extract the ZIP file.
  • tar&gz:tarArchive packaging tool on Unix and Unix-like systems, can merge multiple files into a single file; whilegzOr,gzipUnix – like file decompression software. The two are often used together.

ZIP

For ZIP files, there are two main commands:

# decompression
uizp
# compression
zip
Copy the code

Details:

  • www.runoob.com/linux/linux…
  • Zh.wikipedia.org/wiki/ZIP%E6…
  • www.runoob.com/linux/linux…

Extract: unzip

Direct decompression

First, the most commonly used, that is, without parameters directly decompress:

unzip juejinTemp.zip
Copy the code

As you can see, the decompression is successful.

Specify the output directory

If you want to specify the output directory:

Unzip the files into the temp directory
unzip -d temp juejinTemp.zip
Copy the code

Note: If the temp folder does not exist, it will be created automatically (if new enough); But if the parent directory does not exist, it cannot be created.

File for details

Of course, if you want to know what’s in a ZIP file, how do you decide if you need to create a folder to extract it? It’s also simple:

unzip -l juejinTemp.zip
Copy the code

Use the -l command here, or you can use -v instead. The difference is that the latter shows more detail.

As you can see in Figure 1, when unzipped, there is a folder covering the remaining four files. Figure 2 does not. At this time, it is recommended to use -d command to specify the empty folder when decompressing figure 2.

Specify specific file

Finally, if you just want to extract an internal file, it’s easy:

unzip juejinTemp.zip juejinTemp/after.txt
Copy the code

In fact, this command is a variable length argument syntax sugar, and you can specify what to decompress in more files:

Specified file exclusion

In contrast, if you want to exclude a file during decompression, you can use the -x command:

unzip juejinTemp.zip -x juejinTemp/1.txt
Copy the code

Skip and overwrite

As you can see, there is a prompt when I decompress:

replace juejinTemp/after.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:
Copy the code

This is actually unzip the destination address has these files, ask me to skip or replace, abandon.

If you knew you were going to make the substitution, you could add the -o argument; If you choose to skip, you can add the -n argument.

Compression: zip

Direct compression

The output file name must be specified for compression:

zip -q -r juejinTemp.zip *
Copy the code

Among them:

  • -q: No output is displayed, that is, zip run logs
  • -r: Recursive compression, that is, the contents in a folder are recursively compressed
  • *: All files

That is, all files in the current folder are compressed. Do not use ao in the root directory. Instead, you want to specify the compressed directory:

Zip -r juejinTemp juejinTempCopy the code

Delete the file

zipYou can also delete files, for example. We use theunzipTo view:

We want to delete 1.txt, just use -d command:

zip -d juejinTemp.zip juejinTemp/1.txt
Copy the code

TAR

For files that start with files such as tar, run the tar command. For example, zulu8.60.0.21-ca-fX-jdk8.0.322-linux_x64.tar. gz.

This command is much more complex than the ZIP command. This time, decompression (archive) and compression. Enough for everyday use /

Unpack the command

Gz, tar.bz2, tar.xz, tar.z, etc. Online decompression methods are also multifarious.

Since 1.15, tar can automatically recognize the compressed format, so it can be decompressed correctly without the need to distinguish the compressed format:

tar --version
Copy the code

So unzip it with just one command:

tar -xf juejinTemp.tar.gz
Copy the code

Among them:

  • -xUnlock a compressed file parameter command!
  • -f: Uses the file name, infImmediately after the file name.

If you want to display details, you can add the parameter v, verbose:

tar -xvf juejinTemp.tar.gz
Copy the code

If you want to specify unzip address:After the compressed file, add space and add the uncompressed output address directory

Check the details

If you don’t want to decompress, you can use:

tar -tf juejinTemp.tar.gz
Copy the code

Compress/archive

Compressed files are also relatively simple, a command:

tar -czf juejinTemp.tar.gz juejinTemp
Copy the code

In this way, all files in the current directory are compressed into compression. Tar. gz. Among them:

  • -c: compressed parameter instruction!
  • -z: Starts compression. If this parameter is not added, it is normal archiving.
  • -f: Use the file name, immediately after f followed by the file name.

As you will see, I am much more.DS_StoreThe file is actually a macOS log file. The solution is simple, plus--exclude '.DS_Store' --exclude '__MACOSX'You can:

If you just want to archive, you don’t need to add -z for compression:

END

That concludes the tar and zip commands.

What should I say next time?