Urgent.News

What's breaking now, across thousands of outlets.

Tech

Unzip Files on Linux Without Losing Track of Where They Go

A ZIP file can contain more than you expect, and unzip archive.zip writes its contents into your shell’s current directory—not necessarily beside the archive. Before extracting, check where you are and what the archive contains. That small pause can prevent files from landing in the wrong place or overwriting something important. Inspect first, extract second Start by checking your current…

Files contained within a ZIP archive can end up in unexpected locations if you're not careful. Before extracting, it's wise to pause and inspect both your current directory and the contents of the archive itself. To check where files will be written, use the pwd command. To view the archive's contents without extracting them, utilize unzip -l archive.zip.

This shows the archive's listing, helping you avoid accidental overwrites or misplaced files. The pwd command displays your current working directory, which reveals where files will be placed if you extract without specifying a destination. The -l option lists the archive's entries, allowing you to preview the files without actually extracting them.

If you're uncertain about the current directory's contents, list them with ls as well. A quick test for archive integrity can be performed using unzip -t archive.zip. While this test often uncovers common archive corruption issues, it doesn't guarantee file safety. When the archive is untrusted or potentially corrupted, extract it into a separate directory rather than overwriting important files in your current location.

To extract into a specific location, use the -d option followed by the desired directory path. For example, unzip backup.zip -d ~/extracted/backup will place the contents into the ~/extracted/backup directory. The -d option creates the destination directory automatically if it doesn't already exist. If you want to create the destination directory explicitly first, you can use mkdir -p before running unzip.

The -p option ensures that parent directories are created as needed. The location of the ZIP file and the extraction destination are independent of each other. For instance, you can read an archive from ~/Downloads but extract it to /tmp using unzip ~/Downloads/site-files.zip -d /tmp/site-files. In cases where a file with the same name already exists in the destination, unzip prompts you whether to replace it.

You can either skip replacing existing files with the -n option or overwrite them without prompting using the -o option. Use -o only when replacing existing files is intentionally desired, as it skips the chance to review each conflict interactively. When unsure, it's safer to extract into a new, empty directory. To extract a single file from the archive without decompressing the entire archive, provide the file name after the archive name.

For example, unzip backup.zip reports/summary.txt -d ~/extracted/backup will only extract reports/summary.txt to the specified destination directory. Note that the extraction path must match the name stored inside the ZIP archive. To avoid shell interpretation of special characters like spaces or wildcard characters, enclose the file name in quotes.

If you want to flatten the directory structure and place all files directly into the destination without preserving the original directory hierarchy, use the -j option: unzip -j backup.zip -d ~/extracted/flat. Be cautious with this option, as it can create filename conflicts if multiple directories in the archive contain files with the same basename.

Before running unzip, verify that the unzip command is available by checking for the error message "unzip: command not found." If the error appears, install the unzip package using your distribution's package manager. For Debian or Ubuntu, use sudo apt update && sudo apt install unzip. For Fedora, RHEL, or Rocky Linux, use sudo dnf install unzip.

For Arch Linux, use sudo pacman -S unzip. ZIP archives are distinct from gzip-compressed files. A .tar.gz or .tgz file is a tar archive compressed with gzip, while a standalone .gz file typically contains a single compressed file. To extract a .tar.gz archive into a chosen directory, first create the directory with mkdir -p /tmp/archive-files and then use tar -xzf archive.tar.gz -C /tmp/archive-files.

For a standalone .gz file, use gunzip file.gz. Remember that gunzip replaces the compressed file with its decompressed output, so be cautious when using it. Before extracting an unfamiliar ZIP file, perform a quick safety checklist. Run pwd to confirm your current directory and ensure you're not about to overwrite important files.

Use unzip -l archive.zip to inspect the archive's paths and filenames, helping you avoid unwanted file placement. Choose a dedicated destination using the -d option to explicitly specify where the files will be extracted. Avoid using the -o option unless you deliberately want to replace existing files, as it skips the confirmation prompt.

For most routine extractions, use unzip archive.zip -d /path/to/destination to explicitly set the extraction location. This approach keeps the archive's contents separate from your existing files, minimizing the risk of unintended modifications or overwrites.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Saturday 10 October →