ZIP command in Linux with Examples
ZIP command
Zip command is actually a tool that is commonly used to compress the files and folders. It helps in reducing the size of large files and save it in a compressed format.
Syntax
zip < options> < new-zipfile-name> < source-file>
To Compress files and folders
ZIP command is used to compress two files and one folder into a single file.
Example
[user1@linuxhelp Desktop]$ zip new.zip test.txt test1.txt test
adding: test.txt (deflated 8%)
adding: test1.txt (stored 0%)
adding: test/ (stored 0%)
Here, the test.txt, test1.txt files and test directory is compressed and created as new.zip file.
Compress folders with its sub directories
To compress folders along with its sub directories using ZIP command, use the following.
Example
[user1@linuxhelp Desktop]$ zip -r folder.zip test
adding: test/ (stored 0%)
Here, the folder test with its sub-folders is also archived but it can’ t able to view the sub-folders archived there.
Compress folders which is in different location
To compress folders which are in different location using this command.
Example
[user1@linuxhelp]$ zip -r new.zip /home/user1/Desktop/new/ /home/user1/Music/test/
adding: home/user1/Music/test/ (stored 0%)
adding: home/user1/Music/test/file3 (stored 0%)
adding: home/user1/Music/test/file3 (stored 0%)
adding: home/user1/Music/test/file2 (stored 0%)
adding: home/user1/Music/test/file1 (stored 0%)
The file located in another location is compressed to another location.
Separation of folders and sub folders
By default it will not zip entire folder content such as sub-folders and files.
Example
[user1@linuxhelp Desktop]$ zip new.zip test/ adding: test/ (stored 0%) [user1@linuxhelp Desktop]$ rm -rf new.zip [user1@linuxhelp Desktop]$ zip new.zip test/* adding: test/file1 (stored 0%) adding: test/file2 (stored 0%) adding: test/file3 (stored 0%)
Compress folders fast
To compress folders at faster rate we can use -1 option.
Example
[user1@linuxhelp Music]$ zip new.zip -1 -r /home/user1/Music/test/
adding: test/ (stored 0%)
adding: test/file3 (stored 0%)
adding: test/file2 (deflated 29%)
adding: test/file1 (stored 0%)
Here, the files are transferred a bit faster when using -1 option.
To exclude the files while compressing
To exclude a single file alone while compressing -x option is used.
Example
[user1@linuxhelp Music]$ zip -r new.zip test/ -x test/file1.txt
adding: test/ (stored 0%)
adding: test/file3 (stored 0%)
adding: test/file2 (stored 0%)
Here, it has compressed all files except file1.
Listing the contents in zip file
To view the contents present inside the zip file -l option is used with unzip command, less command or zip info command.
Example
[user1@linuxhelp Music]$ ls -l -rw-rw-r-- 1 user1 user1 456 Feb 28 06:36 new.zip [user1@linuxhelp Music]$ unzip -l new.zip Archive: new.zip Length Date Time Name --------- ---------- ----- ---- 0 02-28-2016 05:51 test/ 0 02-28-2016 05:51 test/file3 0 02-28-2016 05:51 test/file2 --------- ------- 0 3 files
Updating a file into the zip file
To update a single file into the existing zip file using -u option.
Example
[user1@linuxhelp Music]$ zip -u new.zip test/file2
adding: test/file2 (stored 0%)
Here, the file2 is updated to the test directory
Deleting file without extraction
To delete single file without extracting the file.
Example
[user1@linuxhelp Music]$ zip -d new.zip test/file2
deleting: test/file2
Here, the file2 alone is deleted in the test directory.
Updating all files into the file
To update only the last modified file inside the directory to be compressed.
Example
[user1@linuxhelp Music]$ vim file2
[user1@linuxhelp Music]$ zip -fr new.zip test/
freshening: test/ (stored 0%)
freshening: test/file2 (deflated 29%)
Here, only the file2 is compressed since I have modified only that file at last.
To extract the zip file
To extract the archived file into the current location is that just to use the unzip command followed by the zip file name.
Example
[user1@linuxhelp Music]$ unzip new.zip
Archive: nnn.zip
Here, the zipped file is unzipped with that of the contents.
To extract a single directory
To extract a single directory from the compressed file -d option should be used.
Example
[user1@linuxhelp Music]$ unzip new.zip -d d1
Archive: new.zip
Creating: d1/test/
Here, the d1 directory alone is extracted out with that of contents inside.
Comments ( 0 )
No comments available