Advanced CLI

Printing

Here are some basic commands for printing from the command line or interacting with a printer from the command line.

  • lpr - print files

    lpr -PprinterName document

An example usage of this command you are likely to use is to print files from the command line. For this you will need the -P flag with the printer name and the name of the document you would like to print.

  • lpq - print queue of printer

    lpq -PprinterName

For this command you simply need to use the -P flag with the printer name to get the queue for that printer queue status.

  • lprm - remove a job from the print queue

lprm -PprinterName jobNumber

Let’s say you accidentally printed a file you did not want to or printed multiple copies when you only needed one. With this command and using the -P flag with the printer name as well as the number of the job you would like to remove from the queue with the lpq command, you can remove a print request to a printer from the command line.

Learn how to print in our department environment here.

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 .tar archive can contain many files and folders in a single .tar file 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 .tar archive into a .tar.gz archive.

Less common:

  • .bz2 | .bz: similar to gzip, bzip2 only compresses single files and is most often used to further compress .tar archives into .tar.bz2 archives.

  • .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.

  • -c will create the tarball archive.

  • -z will tell tar that we will compress the tarball with the .gz extension.

  • -v will output all file names put into the archive to the terminal.

  • -f will 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 tell tar that we will compress the tarball with the .bz2 extension as in .tar.bz2 or just .bz2.

  • -J: will tell tar that we will compress the tarball with the .xz extension.

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.