Advanced CLI
Tar
File and folder compression can be useful for sending lots of files to
another person or computer in a convenient archive package. There are a
few different file extensions that you will need to understand to wrap
your head around using the tar command in the terminal.
Most common:
.tar: also called a tarball, a.tararchive can contain many files and folders in a single.tarfile for easier transport. This does not compress the files in any way..gz: gzip can only compress a single file and is most often used to further compress a.tararchive into a.tar.gzarchive.
Less common:
.bz2|.bz: similar to gzip, bzip2 only compresses single files and is most often used to further compress.tararchives into.tar.bz2archives..xz: this is generally used for Linux architecture files like kernel archive files. It is slow and resource hungry, but can compress to a smaller size depending on the circumstances.
Tar compression and decompression can be a doozy, but we are here to
help! The most useful tools you have at your disposal when you are
trying to compress or decompress a .tar or .tar.gz file are the man tar manual page and your favorite search engine.
Here are a few basic, useful commands.
Compression
tar -czvf archiveName.tar.gz ~/Desktop/thisFile.txt ~/Downloads/thatFolder moar.jpeg
This will compress the two files (thisFile.txt and moar.jpeg) and
the folder (thatFolder) into an archive named archiveName.tar.gz.
-cwill create the tarball archive.-zwill telltarthat we will compress the tarball with the.gzextension.-vwill output all file names put into the archive to the terminal.-fwill name the output archive.
Decompression
tar -xzvf anotherArchive.tar.gz
This will extract all the files and folders from anotherArchive into
the current working directory.
-x: will extract from the archive.
If you want to extract the archive contents into a new folder, you can
add the -C flag like so:
tar -xzvf anotherArchive.tar.gz -C folderToExtractTo
This command will extract the contents of anotherArchive.tar.gz to a
new folder folderToExtractTo.
Other flags
-j: will telltarthat we will compress the tarball with the.bz2extension as in.tar.bz2or just.bz2.-J: will telltarthat we will compress the tarball with the.xzextension.
Zip
Like a .tar.gz, a .zip file can compress many files and folders into
one archive to make sending or storing lots of data easier.
Creating a .zip from the command is much more simple than creating tar
files:
Compression
zip archiveName.zip thisFile.py ~/Documents/thatDocument.docx
This will create an archive named archiveName.zip containing
thisFile.py from the working directory and thatDocument.docx from
the Documents folder.
If the archive will contain a folder you will need the -r flag.
zip -r folderArchiveName.zip ~/Documents/thisFile.java
This will create an archive named folderArchiveName.zip containing the
entire Documents folder and thisFile.java from the working directory.
Decompression
unzip archiveName.zip
This simple command will extract and decompress the contents of
archiveName.zip into the current directory. The archive will remain
the same.
Virtual environments
In the CS department, we have some permissions in place to block
installation of many outside programs and modules without admin
privileges to avoid bloat and viruses on the computers. Using python
Virtual environments can be helpful when developing a program that you
need more permissions for. In a virtualenv, you can pip install
whatever you want, including the program you are writing if you have that
set up.
virtualenv command
1. To start, you need to build a virtual environment. This command will
create a folder called ENV containing the virtual environment files.
ENV can be any name you want.
virtualenv ENV
2. Before you can pip install anything, you have to activate the
virtualenv to work in it. Once you activate the virtual environment, you
can use it from anywhere.
source ENV/bin/activate
3. Deactivate to stop working in it
deactivate
More info can be found in virtualenv’s user guide.