How to zip and unzip files and folders in Linux
Install Zip Unzip
Install on RHEL, Centos, Fedora, Rocky Linux, etc.
sudo yum install zip unzip
Install on Debian, Ubuntu, etc.
sudo apt-get install zip unzip
Using zip and unzip to compress and extract files and folders.
Current directory contents, files and one sub folder.
ls -lh /home/dragos
total 8.0K
drwxrwxr-x 2 dragos dragos 23 testdir
-rw-rw-r-- 1 dragos dragos 37 testfile
-rw-rw-r-- 1 dragos dragos 71 testfile2
[dragos@localhost ~]$ ls -lh /home/dragos/testdir/
total 4.0K
-rw-rw-r-- 1 dragos dragos 35 innerfile
Zip files and directories recursively
This creates the zip file, the .zip extension is optional you can name it anything you like but .zip makes it more clear what it is later.
The * wildcard character tells zip everything in that directory.
[dragos@localhost ~]$ zip -r diyrednumberone.zip /home/dragos/*
adding: home/dragos/testdir/ (stored 0%)
adding: home/dragos/testdir/innerfile (deflated 80%)
adding: home/dragos/testfile (deflated 57%)
adding: home/dragos/testfile2 (deflated 68%)
[dragos@localhost ~]$
Zip files only
Zip files only in first level directory – the one you specify or the current directory.
zip archive-name.zip /path-to-files/
Zipping test files
[dragos@localhost ~]$ ls -lh /home/dragos
total 12K
-rw-rw-r-- 1 dragos dragos 760 diyrednumberone.zip
drwxrwxr-x 2 dragos dragos 23 testdir
-rw-rw-r-- 1 dragos dragos 37 testfile
-rw-rw-r-- 1 dragos dragos 71 testfile2
Moving the previous zip file to avoid confusion
[dragos@localhost ~]$ mv diyrednumberone.zip testdir/
Files are ready for zip
[dragos@localhost ~]$ ls -lh /home/dragos
total 8.0K
drwxrwxr-x 2 dragos dragos 50 testdir
-rw-rw-r-- 1 dragos dragos 37 testfile
-rw-rw-r-- 1 dragos dragos 71 testfile2
Compress the files and add them to a new zip file.
[dragos@localhost ~]$ zip diyrednumberone_files.zip /home/dragos/*
adding: home/dragos/testdir/ (stored 0%)
adding: home/dragos/testfile (deflated 57%)
adding: home/dragos/testfile2 (deflated 68%)
[dragos@localhost ~]$
Zip file is created
[dragos@localhost ~]$ ls -lh /home/dragos
total 12K
-rw-rw-r-- 1 dragos dragos 567 diyrednumberone_files.zip
drwxrwxr-x 2 dragos dragos 50 testdir
-rw-rw-r-- 1 dragos dragos 37 testfile
-rw-rw-r-- 1 dragos dragos 71 testfile2
Encrypted zip
You can encrypt the zip file so that nobody can read the file contents without a password.
zip -e diyrednumberone_files_enc.zip /home/dragos/*
The -e parameter specifies the zip is to be encrypted and you will have to enter the password twice after pressing enter.
[dragos@localhost ~]$ zip -e diyrednumberone_files_enc.zip /home/dragos/*
Enter password:
Verify password:
adding: home/dragos/diyrednumberone_files.zip (stored 0%)
adding: home/dragos/testdir/ (stored 0%)
adding: home/dragos/testfile (deflated 57%)
adding: home/dragos/testfile2 (deflated 68%)
Zip command options
[dragos@localhost ~]$ zip -h
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
The default action is to add or replace zipfile entries from list, which
can include the special name - to compress standard input.
If zipfile and list are omitted, zip compresses stdin to stdout.
-f freshen: only changed files -u update: only changed or new files
-d delete entries in zipfile -m move into zipfile (delete OS files)
-r recurse into directories -j junk (don't record) directory names
-0 store only -l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster -9 compress better
-q quiet operation -v verbose operation/print version info
-c add one-line comments -z add zipfile comment
-@ read names from stdin -o make zipfile as old as latest entry
-x exclude the following names -i include only the following names
-F fix zipfile (-FF try harder) -D do not add directory entries
-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)
-T test zipfile integrity -X eXclude eXtra file attributes
-y store symbolic links as the link instead of the referenced file
-e encrypt -n don't compress these suffixes
-h2 show more help
Unzip
Extract zip to current directory
unzip zipfile.zip
Extract to a specific directory -d sets the destination directory where the files will be extracted.
unzip zipfile.zip -d /directory whereto extract/
#
[dragos@localhost ~]$ mkdir extracted/diyrednumberone
[dragos@localhost ~]$ mkdir extracted/diyrednumberone_files
[dragos@localhost ~]$ mkdir extracted/diyrednumberone_files_enc
[dragos@localhost ~]$ ls extracted/
diyrednumberone diyrednumberone_files diyrednumberone_files_enc
[dragos@localhost ~]$ unzip testdir/diyrednumberone.zip -d extracted/diyrednumberone
Archive: testdir/diyrednumberone.zip
creating: extracted/diyrednumberone/home/dragos/testdir/
inflating: extracted/diyrednumberone/home/dragos/testdir/innerfile
inflating: extracted/diyrednumberone/home/dragos/testfile
inflating: extracted/diyrednumberone/home/dragos/testfile2
[dragos@localhost ~]$ unzip diyrednumberone_files.zip -d extracted/diyrednumberone_files
Archive: diyrednumberone_files.zip
creating: extracted/diyrednumberone_files/home/dragos/testdir/
inflating: extracted/diyrednumberone_files/home/dragos/testfile
inflating: extracted/diyrednumberone_files/home/dragos/testfile2
[dragos@localhost ~]$ unzip diyrednumberone_files_enc.zip -d extracted/diyrednumberone_files_enc/
Archive: diyrednumberone_files_enc.zip
[diyrednumberone_files_enc.zip] home/dragos/diyrednumberone_files.zip password:
extracting: extracted/diyrednumberone_files_enc/home/dragos/diyrednumberone_files.zip
creating: extracted/diyrednumberone_files_enc/home/dragos/testdir/
inflating: extracted/diyrednumberone_files_enc/home/dragos/testfile
inflating: extracted/diyrednumberone_files_enc/home/dragos/testfile2
[dragos@localhost ~]$ ls -h extracted/diyrednumberone
home
[dragos@localhost ~]$ ls extracted/diyrednumberone/home/dragos/
testdir testfile testfile2
[dragos@localhost ~]$ ls extracted/diyrednumberone_files/home/dragos/
testdir testfile testfile2
[dragos@localhost ~]$ ls extracted/diyrednumberone/home/dragos/testdir/
innerfile
[dragos@localhost ~]$ ls extracted/diyrednumberone_files/home/dragos/testdir/
[dragos@localhost ~]$ ls extracted/diyrednumberone_files_enc/home/dragos/
diyrednumberone_files.zip testdir testfile testfile2
[dragos@localhost ~]$
Unzip command options
[dragos@localhost ~]$ unzip -h
UnZip 6.00 of 20 April 2009, by Info-ZIP. Maintained by C. Spieler. Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.
Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
Default action is to extract files in list, except those in xlist, to exdir;
file[.zip] may be a wildcard. -Z => ZipInfo mode ("unzip -Z" for usage).
-p extract files to pipe, no messages -l list files (short format)
-f freshen existing files, create none -t test compressed archive data
-u update files, create if necessary -z display archive comment only
-v list verbosely/show version info -T timestamp archive to latest
-x exclude files that follow (in xlist) -d extract files into exdir
modifiers:
-n never overwrite existing files -q quiet mode (-qq => quieter)
-o overwrite files WITHOUT prompting -a auto-convert any text files
-j junk paths (do not make directories) -aa treat ALL files as text
-U use escapes for all non-ASCII Unicode -UU ignore any Unicode fields
-C match filenames case-insensitively -L make (some) names lowercase
-X restore UID/GID info -V retain VMS version numbers
-K keep setuid/setgid/tacky permissions -M pipe through "more" pager
-O CHARSET specify a character encoding for DOS, Windows and OS/2 archives
-I CHARSET specify a character encoding for UNIX and other archives
See "unzip -hh" or unzip.txt for more help. Examples:
unzip data1 -x joe => extract all files except joe from zipfile data1.zip
unzip -p foo | more => send contents of foo.zip via pipe into program more
unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer
[dragos@localhost ~]$
Leave a Reply