How do I manage file compression with Web Terminal?

Procédure

Compressing files via the terminal offers an effective method of managing storage space and facilitating data transfer. In this guide, we'll explore how to use tools such as gzip, bzip2, zip, and tar to compress and decompress files directly from a web terminal. We'll provide simple commands and examples to help you choose the right compression tool for your specific needs, from maximising compression to speed of operation.

Prior to this documentation, we invite you to access your hosting's Web Terminal.

Creating an archive with the zip command

The zip command is a tool used for file compression. It is used to compress files and directories into .zip archives, a format widely used for distributing and exchanging files because of its compatibility between different platforms.

Basic functions

  • Compression: zip compresses files and directories, reducing their size and combining them into a single .zip archive.
  • Archiving: in addition to compression, zip can simply group files into an archive without compression, using the -0 option (zero compression).

Basic syntax

zip [options] archive_name.zip file1 file2 dir1 ...
  • archive_name.zip: Name of the archive to be created.
  • file1 file2 dir1 ... List of files and directories to include in the archive.

Common options

  • -r: Recursive, includes directories and their contents recursively.
  • -q: Silent mode, reduces command output for less clutter.
  • -e: Encrypts the archive, requiring the user to enter a password to secure the files.
  • -u: Updates the existing archive, adding only modified or new files.
  • -m: Moves files to the archive (removes them from their original location after adding them to the archive).

Examples of use

  1. Create an archive of several files:

    zip archive.zip file1.txt file2.txt

    This creates an archive archive.zip containing file1.txt and file2.txt.

  2. Archive a folder recursively:

    zip -r archive.zip folder/

    Archives the entire contents of the folder/ folder, including subfolders.

  3. Encrypt an archive:

    zip -e secure.zip file1.txt file2.txt

    Creates an encrypted secure.zip archive which will require a password to access its contents.

  4. Update an existing archive:

    zip -u archive.zip newfile.txt
    Adds newfile.txt to an existing archive archive.zip, only if newfile.txt is newer than the version in the archive or is not already there.

Typical use

zip is often used for software distribution, exchanging files between different platforms (such as between Windows and Unix), and for backups where files need to remain accessible without decompression.

In short, zip is a versatile compression and archiving tool, ideal for reducing the size of files for storage or transfer, while ensuring compatibility between different operating systems.

Extracting an archive with unzip

The unzip command is the complementary tool to zip, used to extract and manage archives created with zip. It decompresses files and directories stored in .zip archives, making them accessible and usable again.

Basic functions

  • Extraction: unzip unzips .zip archives, extracting their contents into the current directory or a specified directory.
  • List of contents: Before decompressing, unzip can be used to display the contents of an archive, allowing you to check its contents without extracting.

Basic syntax

unzip [options] archive_name.zip
  • archive_name.zip: Name of the archive to be extracted.

Common options

  • -l: Lists the contents of the archive without extracting, displaying file names, size and date of last modification.
  • -d: Specifies the destination directory for extracting files.
  • -o: Overwrites existing files without asking for confirmation when extracting.
  • -p: Extracts files at standard output, often used to pass the contents to another command.
  • -q: Silent mode which reduces the amount of information displayed during extraction.

Examples of use

  1. Extract an archive into the current directory:

    unzip archive.zip

    This extracts all the files and directories contained in archive.zip into the current directory.

  2. List the contents of an archive:

    unzip -l archive.zip
    Displays the list of files in archive.zip without extracting them.
  3. Extract an archive to a specific folder:

    unzip archive.zip -d /path/to/destination
    Extracts the contents of archive.zip into the /path/to/destination directory.
  4. Extract a specific file from the archive:

    unzip archive.zip file1.txt
    Extracts only file1.txt from archive.zip.

Typical use

unzip is often used to recover the contents of downloaded or received .zip archives, for backup or data recovery tasks, and in scripts where archives need to be automatically decompressed for software installation or file updates.

In short, unzip is an essential tool for manipulating .zip archives, allowing users to easily extract, manage and use archived content.

Creating / extracting an archive with tar

The tar command (short for "tape archive") is a file management tool. It is mainly used to create file archives and to extract files from archives. Although traditionally associated with magnetic tape, tar is widely used for archiving files on hard disks and other storage media.

Basic functions

  • Archive creation: tar can be used to combine numerous files into a single archive file, making them easier to store and transfer.
  • Extracting archives: tar can also be used to extract files from an existing archive.

Basic syntax

tar [options] [archive-file] [file or directory to be archived]
  • options: determines the action to be performed, such as creating an archive, extracting, or listing the contents.
  • archive-file: the name of the archive file to be created or extracted.
  • file or directory to be archived: the files or directories to be archived.

Common options

  • -c: Creates a new archive.
  • -x: Extracts files from an archive.
  • -v: Verbose mode which displays details of operations performed.
  • -f: Used to specify the filename of the archive. This option is always necessary as tar has no default filename.
  • -z: Uses gzip to compress or decompress the archive (creates or extracts .tar.gz or .tgz files).
  • -j: Uses bzip2 to compress or decompress (creates or extracts .tar.bz2 files).
  • -J: Uses xz for compression or decompression (creates or extracts .tar.xz files).

Examples of use

  1. Create a tar archive of several:

    tar -cvf archive.tar file1 file2

    This creates an archive named archive.tar containing file1 and file2.

  2. Create a compressed tar.gz archive:

    tar -czvf archive.tar.gz directory/
    Archives and compresses the contents of the directory/ directory using gzip.
  3. Extract the contents of a tar archive:

    tar -xvf archive.tar
    Extract all files from archive.tar into the current directory.
  4. List the contents of a tar archive without extracting:

    tar -tvf archive.tar
    Displays a list of files contained in archive.tar.

Usage tips

  • Use the -v option to view the archiving or extraction process, which can be useful for debugging.
  • Be careful with file paths when extracting to avoid overwriting important files.

tar is therefore an indispensable command for managing files and archives, offering considerable flexibility in the way you manipulate, compress and decompress archives.

Creating / extracting an archive with gzip

The gzip command (GNU zip) is a very popular compression utility. It is mainly used to compress files, reducing their size to save disk space and speed up data transfer. gzip is often used in combination with other tools, such as tar, to compress entire directories.

Basic functions

  • Compression: gzip replaces each file with a compressed version of itself, bearing the .gz extension.

Basic syntax

gzip [options] [file...]
  • options: Allows you to modify the behaviour of gzip.
  • file... One or more files to be compressed.

Common options

  • -d: Decompresses files (equivalent to the gunzip command).
  • -k: Keeps the source files, i.e. gzip will not delete the original files after compressing them.
  • -c: Writes to standard output, allowing redirection to another file or program.
  • -r: If the arguments include directories, gzip will go through them recursively, compressing all the files they contain.
  • -v: Verbose mode, displays the size of files before and after compression as well as the compression ratio.
  • -9: Specifies the compression level, with 9 representing the highest and slowest compression level. The default level is -6.

Examples of use

  1. Compressing a:

    gzip example.txt

    This will replace example.txt with example.txt.gz in the current directory.

  2. Unzip a file:

    gzip -d example.txt.gz

    Or, equivalently :

    gunzip example.txt.gz

    This will reconstitute the original example.txt file from example.txt.gz.

  3. Compress several files:

    gzip file1.txt file2.txt file3.txt

    Each file will be compressed and replaced by a corresponding .gz version.

  4. Compress and redirect the output:

    gzip -c file.txt > file.txt.gz

    Compresses file.txt and redirects the compressed output to file.txt.gz without deleting the original file.

Typical use

gzip is ideal for compressing individual files or data streams. For archives of multiple files or directories, gzip is often used with tar, as in

tar czf archive.tar.gz directory/

which creates a compressed tar archive of directory.

In summary, gzip is a simple and effective file compression tool, widely used to reduce the size of files for storage or transmission.

Conclusion

You now know how to :

  • Use the gzip, bzip2, zip and tar commands to optimise your storage space and secure the transfer of your data 💾.
  • Choosing the right compression tool for your specific needs, whether to maximise compression or to prioritise speed 🔄.
  • Create .zip archives with the zip command and manage them efficiently using the many options available 🗄️.
  • Extract files and folders from an archive using the unzip command, with the option of viewing the contents without decompression 📂.
  • Manipulate .tar archives with the tar command, creating compact archives or extracting their contents 📦.
  • Compress files with gzip to save space and make it easier to share compressed .gz files 🚀.

Mastering these compression tools is essential in the day-to-day management of files and directories, whether for backup, archiving or transfer purposes. Each command offers unique benefits and can be used individually or in combination to best meet your requirements.

We hope you have found this article useful and that it has given you the knowledge you need to optimise your use of storage space and the management of your files. We thank you for reading this and invite you to share your experiences or ask any questions you may have by leaving a comment below. Your feedback is invaluable and helps us to continue improving our services. Thank you and see you soon on our platform! 😊👍

Rate this article :

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

1mn reading

How do I connect to the Web Terminal with LWS Panel? (ssh web console)

4mn reading

How can I use GIT with the Web terminal on my LWS shared hosting?

2mn reading

How to change the PHP version of the LWS Panel Web Terminal

0mn reading

What can I do on my WordPress site using the Web Terminal?


Ask the LWS team and its community a question