Showing posts with label Zip Command. Show all posts
Showing posts with label Zip Command. Show all posts

Thursday, 28 November 2019

ZIP command in Linux with examples

ZIP is a compression and file packaging utility for Unix. Each file is stored in single .zip {.zip-filename} file with the extension .zip.

ZIP Command, Linux, LPI Study Materials, LPI Guides, LPI Tutorial and Material

◈ zip is used to compress the files to reduce file size and also used as file package utility. zip is available in many operating systems like unix, linux, windows etc.

◈ If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer.

◈ The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity). An entire directory structure can be packed into a zip archive with a single command.

◈ Compression ratios of 2:1 to 3:1 are common for text files. zip has one compression method (deflation) and can also store files without compression. zip automatically chooses the better of the two for each file to be compressed.
The program is useful for packaging a set of files for distribution; for archiving files; and for saving disk space by temporarily compressing unused files or directories.

Syntax :

zip [options] zipfile files_list

Syntax for  Creating a zip file:

$zip myfile.zip filename.txt

Extracting files from zip file 


Unzip will list, test, or extract files from a ZIP archive, commonly found on Unix systems. The default behavior (with no options) is to extract into the current directory (and sub-directories below it) all files from the specified ZIP archive.

Syntax :
$unzip myfile.zip

Options :

1. -d Option: Removes the file from the zip archive.  After creating a zip file, you can remove a file from the archive using the -d option.
Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c
hello5.c
hello6.c
hello7.c
hello8.c

Syntax :

$zip –d filename.zip file.txt
Command :
$zip –d myfile.zip hello7.c

After removing hello7.c from myfile.zip file, the files can be restored with unzip command

Command:
$unzip myfile.zip
$ls command
Output :
hello1.c
hello2.c
hello3.c
hello4.c
hello5.c
hello6.c
hello8.c
The hello7.c file is removed from zip file

2. -u Option: Updates the file in the zip archive. This option can be used to update the specified list of files or add new files to the existing zip file. Update an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive.

Syntax:

 $zip –u filename.zip file.txt

Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c

Command :
$zip –u myfile.zip hello5.c

After updating hello5.c from myfile.zip file, the files can be restored with unzip command

Command:
$unzip myfile.zip
$ls command
Output :
hello1.c
hello2.c
hello3.c
hello4.c
hello5.c
The hello5.c file is updated to the zip file

ZIP Command, Linux, LPI Study Materials, LPI Guides, LPI Tutorial and Material

3. -m Option: Deletes the original files after zipping. Move the specified files into the zip archive actually, this deletes the target directories/files after making the specified zip archive. If a directory becomes empty after removal of the files, the directory is also removed. No deletions are done until zip has created the archive without error. This is useful for conserving disk space, but is potentially dangerous removing all input files.

Syntax :

 $zip –m filename.zip file.txt

Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c

Command :
$zip –m myfile.zip *.c

After this command has been executed by the terminal here is the result:

Command:
$ls command
Output :
myfile.zip
//No other files of .c(extension) has been found

4. -r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

Syntax:

 $zip –r filename.zip directory_name

Suppose we have following files in my current directory (docs) are listed below:
unix.pdf
oracle.pdf
linux.pdf

Command :
$zip –r mydir.zip docs
Output :
  adding: docs/            //Compressing the directory
  adding: docs/unix.pdf   // Compressing first file
  adding: docs/oracle.pdf // Compressing second file
  adding: docs/linux.pdf  //Compressing third file

5. -x Option: Exclude the files in creating the zip. Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.

Syntax :

 $zip –x filename.zip file_to_be_excluded

Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c

Command :
$zip –x myfile.zip hello3.c

This command on execution will compress all the files except hello3.c

Command:
$ls command
Output :
myfile.zip //compressed file
hello3.c   //this file has been excluded while compressing

6. -v Option: Verbose mode or print diagnostic version info. Normally, when applied to real operations, this option enables the display of a progress indicator during compression and requests verbose diagnostic info about zip file structure oddities.
When -v is the only command line argument, and either stdin or stdout is not redirected to a file, a diagnostic screen is printed. In addition to the help screen header with program name, version, and release date, some pointers to the Info-ZIP home and distribution sites are given. Then, it shows information about the target environment (compiler type and version, OS version, compilation date and the enabled optional features used to create the zip executable.

Syntax :

 $zip –v filename.zip file1.txt

Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c

Command
$zip -v file1.zip *.c
Output :
adding: hello1.c    (in=0) (out=0) (stored 0%)
  adding: hello2.c    (in=0) (out=0) (stored 0%)
  adding: hello3.c    (in=0) (out=0) (stored 0%)
  adding: hello4.c    (in=0) (out=0) (stored 0%)
total bytes=0, compressed=0 -> 0% savings

Thursday, 7 June 2018

Zip Command Examples in Unix / Linux

Zip Command, Linux Certification, Unix Certification, Linux LPI

zip is used to compress the files to reduce file size and also used as file package utility. zip is available in many operating systems like unix, linux, windows etc.

If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer.

The syntax of zip command is

zip [options] zipfile files_list

The options of zip command are:

-d : Removes the file from the zip archive
-u : Updates the file in the zip archive
-m : Deletes the original files after zipping.
-r : Recursively zips the files in a directory
-x : Exclude the files in creating the zip
-v : verbose mode
-1 : Compresses the files faster
-9 : Compresses the files better
-f : freshen only changed files.
zipfile : creates the zip file with name as zipfile.zip
files_list : list of files to be zipped.

Zip Command Examples:


The files in my current directory are listed below:

docs/linux.pdf
docs/oracle.pdf
docs/unix.pdf
linux-virtual-server.bat
unix-server.dat

Here docs is a directory which contains the files linux.pdf, unix.pdf and oracle.pdf. We will see how to use zip command with examples.

1. Creating a zip file


The zip command in unix or linux system creates an archive with the specified files. This is shown below:

> zip archive linux-virtual-server.bat unix-server.dat
  adding: linux-virtual-server.bat (deflated 80%)
  adding: unix-server.dat (deflated 80%)
> ls
archive.zip  docs  linux-virtual-server.bat  unix-server.dat

The above command creates the zip file with name archive.zip

2. Extracting files from zip


To extract files from the zip, use the unzip command in unix system. This is shown below:

> unzip archive.zip
Archive:  archive.zip
  inflating: linux-virtual-server.bat
  inflating: unix-server.dat
> ls
archive.zip  linux-virtual-server.bat  unix-server.dat

3. Removing file from a zip file


After creating a zip file, you can remove a file from the archive using the -d option. To remove the file unix-server.dat from the archive, run the below zip command:

> zip -d archive.zip unix-server.dat
deleting: unix-server.dat

> unzip archive.zip
Archive:  archive.zip
  inflating: linux-virtual-server.bat

4. Update existing zip file


You can update the files in already created zip file. If any of the files are modified after zipping, you can fresh the zip file with only those modified files using the -f option.

> zip -f archive.zip
freshening: linux-virtual-server.bat (stored 0%)

Another way is using the -u option. This option can be used to update the specified list of files or add new files to the existing zip file.

> zip -u archive.zip  linux-virtual-server.bat temp
updating: linux-virtual-server.bat (deflated 79%)
  adding: temp (stored 0%)

5. Recursively zip files in directory.


To zip a directory recursively, use the -r option with the zip command. This example is shown below:

> zip -r dir_archive docs
  adding: docs/ (stored 0%)
  adding: docs/unix.pdf (stored 0%)
  adding: docs/oracle.pdf (stored 0%)
  adding: docs/linux.pdf (stored 0%)

6. Excluding files in zipping


Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.

zip exclude_archive * -x linux-virtual-server.bat

The above command zips all the files in the current directory except the file linux-virtual-server.bat

7. Faster compressing


You can compress the files very fast using the -1 option with zip command. An example is shown below with and without using fast compression.

> zip -1 fast_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 79%)

>zip normal_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 80%)

If you use fast compression, the archive file created will occupy more space (size) when compared to normal compression.

8. Better compression.


To reduce more amount of size the files occupied, you can use the -9 option with the zip command. This gives a better compression.

> zip -9 better_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 81%)

Compare the deflated percentages in the example 7 and 8.

Friday, 27 April 2018

SSH/Zip Command Examples in Unix / Linux

Linux, Linux Command, SSH, SSH Command, UNIX, Unix Command, Zip, Zip Command

SSH Command 


SSH client utility in unix or linux server is used to logging into a remote host and execute commands on the remote machine. The rlogin and rsh commands can also be used to login into the remote machine. However these are not secure. The ssh command provides a secure connection between two hosts over a insecure network.

The syntax ssh command is


ssh [-l username] hostname | user@remote-hostname [command]

Let see the examples of ssh command.

SSH Command Examples:


1. Logging to a remote server

You can login to a remote server from the local host as shown below:

localhost:[~]> ssh -l username remote-server
username@remote-server password:
remote-server:[~]>

Alternatively you can use the below ssh command for connecting to remote host:

localhost:[~]> ssh username@remote-server
username@remote-server password:
remote-server:[~]>

Note: If you are logging for the first time, then it will prints a message that host key not found and you can give yes to continue. The host key of the remote server will be cached and added to the .ssh2/hostkeys directory in your home directory. From second time onwards you just need to enter the password.

2. Logging out from remote server

Simply enter the exit command on the terminal to close the connection. This is shown below:

remote-server:[~]>exit
logout
Connection to remote-server closed.
localhost:[~]>

3. Running remote commands from local host

Sometimes it is necessary to run the unix commands on the remote server from the local host. An example is shown below:

localhost:[~]> ssh user@remote-host "ls test"
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

The ssh command connects to the remote host, runs the ls command, prints the output on the local host terminal and exits the connection from remote host.

Let see whether the ls command actually displayed the correct result or not by connecting to the remote host.

localhost:[~]> ssh user@remote-host
user@remotehost password:
remotehost:[~]> cd test
remotehost:[~/test]> ls
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

4. Version of the SSH command

We can find the version of SSH installed on the unix system using the -V option to the ssh. This is shown below:

> ssh -V
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008

5. Debugging the SSH Client

When we are not able to connect to the remote host, it is good to debug and find the exact error messages that causing the issue. Use the -v option for debugging the ssh client.

ssh -v user@remote-host
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to remote-host [172.22.200.140] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/identity type -1
debug1: identity file /home/user/.ssh/id_rsa type -1
debug1: identity file /home/user/.ssh/id_dsa type 2
debug1: loaded 3 keys
..........
..........

6. Copying files between remote host and local host.

We can use the scp command to copy the files securely between the local host and remote host using the ssh authentication.

To copy the file from local host to remote hosts /var/tmp/ directory, run the below scp command.

scp filename user@remote-host:/var/tmp/

To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, run the below scp command.

scp user@remote-host:/usr/local/bin/add.sh.

Zip Command


Zip is used to compress the files to reduce file size and also used as file package utility. zip is available in many operating systems like unix, linux, windows etc.

If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer.

The syntax of zip command is


zip [options] zipfile files_list

The options of zip command are:

-d : Removes the file from the zip archive
-u : Updates the file in the zip archive
-m : Deletes the original files after zipping.
-r : Recursively zips the files in a directory
-x : Exclude the files in creating the zip
-v : verbose mode
-1 : Compresses the files faster
-9 : Compresses the files better
-f : freshen only changed files.
zipfile : creates the zip file with name as zipfile.zip
files_list : list of files to be zipped.

Zip Command Examples:


The files in my current directory are listed below:

docs/linux.pdf
docs/oracle.pdf
docs/unix.pdf
linux-virtual-server.bat
unix-server.dat

Here docs is a directory which contains the files linux.pdf, unix.pdf and oracle.pdf. We will see how to use zip command with examples.

1. Creating a zip file

The zip command in unix or linux system creates an archive with the specified files. This is shown below:

> zip archive linux-virtual-server.bat unix-server.dat
  adding: linux-virtual-server.bat (deflated 80%)
  adding: unix-server.dat (deflated 80%)
> ls
archive.zip  docs  linux-virtual-server.bat  unix-server.dat

The above command creates the zip file with name archive.zip

2. Extracting files from zip

To extract files from the zip, use the unzip command in unix system. This is shown below:

> unzip archive.zip
Archive:  archive.zip
  inflating: linux-virtual-server.bat
  inflating: unix-server.dat
> ls
archive.zip  linux-virtual-server.bat  unix-server.dat

3. Removing file from a zip file

After creating a zip file, you can remove a file from the archive using the -d option. To remove the file unix-server.dat from the archive, run the below zip command:

> zip -d archive.zip unix-server.dat
deleting: unix-server.dat

> unzip archive.zip
Archive:  archive.zip
  inflating: linux-virtual-server.bat

4. Update existing zip file

You can update the files in already created zip file. If any of the files are modified after zipping, you can fresh the zip file with only those modified files using the -f option.

> zip -f archive.zip
freshening: linux-virtual-server.bat (stored 0%)

Another way is using the -u option. This option can be used to update the specified list of files or add new files to the existing zip file.

> zip -u archive.zip  linux-virtual-server.bat temp
updating: linux-virtual-server.bat (deflated 79%)
  adding: temp (stored 0%)

5. Recursively zip files in directory.

To zip a directory recursively, use the -r option with the zip command. This example is shown below:

> zip -r dir_archive docs
  adding: docs/ (stored 0%)
  adding: docs/unix.pdf (stored 0%)
  adding: docs/oracle.pdf (stored 0%)
  adding: docs/linux.pdf (stored 0%)

6. Excluding files in zipping

Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.

zip exclude_archive * -x linux-virtual-server.bat

The above command zips all the files in the current directory except the file linux-virtual-server.bat

7. Faster compressing

You can compress the files very fast using the -1 option with zip command. An example is shown below with and without using fast compression.

> zip -1 fast_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 79%)

>zip normal_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 80%)

If you use fast compression, the archive file created will occupy more space (size) when compared to normal compression.

8. Better compression.

To reduce more amount of size the files occupied, you can use the -9 option with the zip command. This gives a better compression.

> zip -9 better_archive linux-virtual-server.bat
  adding: linux-virtual-server.bat (deflated 81%)

Compare the deflated percentages in the example 7 and 8.