Showing posts with label CentOS. Show all posts
Showing posts with label CentOS. Show all posts

Solved : How to zip directories in Linux

In this post we will see how you can use zip command in Linux to compress directories and files and unzip them. zip is also very useful if you have to further uncompress the files on windows machine as by default windows allows you to open zip files without installing any extra program.

First let's install zip. Below we will show installation instruction for both Ubuntu and Redhat linux/CentOS.

Ubuntu :

Update the package repo:

root@CloudVedas:~# sudo apt-get update

Now let's install zip and unzip packages:

sudo apt-get install zip unzip -y


Update the package repo:

root@CloudVedas:~# sudo yum makecache

Install the zip and unzip packages

root@CloudVedas:~# sudo yum install zip unzip

Once the zip and unzip packages are installed we can now see their usage.

The basic syntax of the zip command is below:

zip -r <output_file>.zip  <dir1> <dir2> ... <dirn>

Let's see some examples :

We will zip three directories

root@CloudVedas:~# zip -r dir1 dir2 dir3

  adding: dir1/ (stored 0%)

  adding: dir1/test11 (stored 0%)

  adding: dir1/test13 (stored 0%)

  adding: dir1/tet12 (stored 0%)

  adding: dir2/ (stored 0%)

  adding: dir2/test21 (stored 0%)

  adding: dir2/test22 (stored 0%)

  adding: dir2/test23 (stored 0%)

  adding: dir3/ (stored 0%)

  adding: dir3/test31 (stored 0%)

  adding: dir3/test32 (stored 0%)

  adding: dir3/test33 (stored 0%)


When we list the contents we can see our zipped file .

root@CloudVedas:~# ls -lrth

total 4.0K

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir1

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir2

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir3

-rw-r--r-- 1 root root 1.8K Aug 29 13:55


You can also zip all the directories in the present working directory using this simple bash script.

for dir in $(ls -d */); do zip $dir; done 

View contents of a zip file 

If you want to see the contents of a zip file without unzipping it you can use either vi or vim .

root@CloudVedas:~# vim


root@CloudVedas:~# vi

With the above commands you can easily see the list of files and directories inside the zip.


Now let's see how you can unzip a file. Be careful when you are unzipping a file with out any extra options as it will overwrite the existing contents if same name exist. Best is to unzip the file in a different directory and then copy the required files and directories as needed in the destination folder.

root@CloudVedas:~/unziptest# unzip


   creating: dir1/

 extracting: dir1/test11

 extracting: dir1/test13

 extracting: dir1/tet12

   creating: dir2/

 extracting: dir2/test21

 extracting: dir2/test22

 extracting: dir2/test23

   creating: dir3/

 extracting: dir3/test31

 extracting: dir3/test32

 extracting: dir3/test33

root@CloudVedas:~/unziptest# ls

dir1  dir2  dir3


If you want to see other useful options on unzip you can use below command

unzip -hh

So above we have seen how you can use zip and unzip in Linux. zip is a good utility for smaller files and directories but if you need better compression you should use tar.gz. Check out our post on most useful tar command examples if you want to know more about tar.gz and tar.bz2 or other compression methods in Linux.

If you want to transfer this file out of the Linux server check our post on how to transfer files to and from linux .

Do let us know in comments section if you have any query or suggestion about this post.

Solved: Create virtual environment with python 3 in linux

Once you have installed python 3.6 as described in the last post you can create virtual environment to work with it.
  • First let’s install the package of virtual environment for python 3.
sudo yum install -y python3-venv
sudo apt-get install -y python3-venv 
  • Create a directory for you virtual environment and get inside it.
mkdir virtualenvscd virtualenvs
  • Now let’s create our virtual environment.
python3.6 -m venv cloudvedasenv
  • Finally activate the new virtual environment. Notice the new environment name in brackets once you run the source command. That means you are now inside the virtual environment.
[ec2-user@cloudvedas ~]$ source cloudvedasenv/bin/activate
(cloudvedasenv) [ec2-user@cloudvedas ~]$
  • If you want to install a package inside the virtual environment you can use pip .
(cloudvedasenv) [ec2-user@cloudvedas ~]$ pip install zappa requests flask

Solved: Install Python 3 in Linux and set it as default

In this post we will show you how to install python 3 in a Linux box. We have Centos 7 installed in our server.
Check installed python version
Most of the Linux versions by default have python installed. So let's check the current version.
[ec2-user@cloudvedas ~]$ python --version
 Python 2.7.5
[ec2-user@cloudvedas ~]$
As we can see from the above output, currently we have Python 2 installed and we need Python 3 so let's install it.
Install pre-requisites
Before we install python 3 let's first update the yum.
[ec2-user@cloudvedas ~]$ sudo yum -y update
Also, install yum utils.
[ec2-user@cloudvedas ~]$ sudo yum -y install yum-utils
And at the end install the CentOS development tools which will help you build and compile software from source code.
[ec2-user@cloudvedas ~]$ sudo yum -y groupinstall development
Install python 3.6.4
The standard yum repos does not have the latest python release, so we will install IUM (Inline with Upstream Stable) which will have the latest packages.
[ec2-user@cloudvedas ~]$ sudo yum -y install
Now let's install Python 3.6.
[ec2-user@cloudvedas ~]$ sudo yum -y install python36u
Next we will install pip, which will manage software packages for Python:
[ec2-user@cloudvedas ~]$ sudo yum -y install python36u-pip
Finally, we will install the development packages.
[ec2-user@cloudvedas ~]$ sudo yum -y install python36u-devel
Let' check our  python version
[ec2-user@cloudvedas ~]$ python3.6 -V
Python 3.6.4
[ec2-user@cloudvedas ~]$
Update default python
But if you still run the normal command to check python version it will show the Python 2 version if it was installed by default.
[ec2-user@cloudvedas ~]$ python --version
 Python 2.7.5
 [ec2-user@cloudvedas ~]$
To reflect the new python version by default you create an alias in .bashrc file as described below in the last line of the file.
[ec2-user@cloudvedas ~]$ sudo vi ~/.bashrc

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then . /etc/bashrc fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# User specific aliases and functions
alias python=python3.6
After making the changes save the file. Now, logout and login again, it should show you the new version.
[ec2-user@cloudvedas ~]$ python -V
Python 3.6.4
[ec2-user@cloudvedas ~]$
Check all installed python versions
Do remember that the default python 2 will still remain in the system as lot of system binaries have dependency on it.
To check all the python versions in your box you can check the installed packages as below.
[ec2-user@cloudvedas ~]$ yum list installed | grep -i python
If you want to create virtual environments in python read our next post.