Categories
Linux

Ubuntu & Virtualbox

Ubuntu 16.04 (kernel 4.14), Virtualbox

I was trying to install Oracle VirtualBox either from their repo or from the Ubuntu repo but both failed to compile the Kernel Module.

I tried various guides before actually reading the error log whereupon I found the “libelf-dev” package was required (at least for installing virtualbox-5.2 from Oracle directly!).

Categories
Linux

courses:edx.org

Handy Bash Shortcuts

Keyboard Shortcut Task
CTRL-L Clears the screen
CTRL-D Exits the current shell
CTRL-Z Puts the current process into suspended background
CTRL-C Kills the current process
CTRL-H Works the same as backspace
CTRL-A Goes to the beginning of the line
CTRL-W Deletes the word before the cursor
CTRL-U Deletes from beginning of line to cursor position
CTRL-E Goes to the end of the line
Tab Auto-completes files, directories, and binaries

Categories
Linux Networking

tcpdump notes

sudo tcpdump -i eth0 port 67 or port 68 -nev and “ether host <<MAC ADDRESS>>”

 

ether host <mac address>

verbose:

-vv

 

packet size for full details:

-s 1500

prevent name resolution:

-n

Categories
Linux

courses.edx.org : bash and scripting linux

 

Character Description
# Used to add a comment, except when used as #, or as #!when starting a script
 \ Used at the end of a line to indicate continuation on to the next line
; Used to interpret what follows as a new command
$ Indicates what follows is a variable

 

 

Categories
Linux

courses.edx.org: PDF viewing, editing

Modifying PDFs with pdftk

At times, you may want to merge, split, or rotate PDF files; not all of these operations can be achieved while using a PDF viewer.  A great way to do this is to use the “PDF Toolkit”, pdftk, to perform a very large variety of sophisticated tasks. Some of these operations include:

  • Merging/Splitting/Rotating PDF documents
  • Repairing corrupted PDF pages
  • Pulling single pages from a file
  • Encrypting and decrypting PDF files
  • Adding, updating, and exporting a PDF’s metadata
  • Exporting bookmarks to a text file
  • Filling out PDF forms

In short, there’s very little pdftk cannot do when it comes to working with PDF files; it is indeed the Swiss Army knife of PDF tools.

Using pdftk

You can accomplish a wide variety of tasks using pdftk including:

Command Usage
pdftk 1.pdf 2.pdf cat output 12.pdf Merge the two documents 1.pdf and 2.pdf. The output will be saved to 12.pdf.
pdftk A=1.pdf cat A1-2 output new.pdf Write only pages 1 and 2 of 1.pdf. The output will be saved to new.pdf.
pdftk A=1.pdf cat A1-endright output new.pdf Rotate all pages of 1.pdf 90 degrees clockwise and save result in new.pdf.

Encrypting PDF Files

If you’re working with PDF files that contain confidential information and you want to ensure that only certain people can view the PDF file, you can apply a password to it using the user_pw option. One can do this by issuing a command such as:

$ pdftk public.pdf output private.pdf user_pw PROMPT

When you run this command, you will receive a prompt to set the required password, which can have a maximum of 32 characters. A new file, private.pdf, will be created with the identical content as public.pdf, but anyone will need to type the password to be able to view it.

 

Using Additional Tools

You can use other tools, such as pdfinfo, flpsed, and pdfmod to work with PDF files.

pdfinfo can extract information about PDF files, especially when the files are very large or when a graphical interface is not available.

flpsed can add data to a PostScript document. This tool is specifically useful for filling in forms or adding short comments into the document.

pdfmod is a simple application that provides a graphical interface for modifying PDF documents. Using this tool, you can reorder, rotate, and remove pages; export images from a document; edit the title, subject, and author; add keywords; and combine documents using drag-and-drop action.

For example, to collect the details of a document, you can use the following command:
$ pdfinfo /usr/share/doc/readme.pdf

 

Converting Between PostScript and PDF

Most users today are far more accustomed to working with files in PDF format, viewing them easily either on the Internet through their browser or locally on their machine. The PostScript format is still important for various technical reasons that the general user will rarely have to deal with.

From time to time you may need to convert files from one format to the other, and there are very simple utilities for accomplishing that task. ps2pdf and pdf2ps are part of the ghostscript package installed on or available on all Linux distributions. As an alternative, there are pstopdf and pdftops which are usually part of the poppler package which may need to be added through your package manager. Unless you are doing a lot of conversions or need some of the fancier options (which you can read about in the man pages for these utilities) it really doesn’t matter which ones you use.

Some usage examples:

Command Usage
pdf2ps file.pdf Converts file.pdf to file.ps
ps2pdf file.ps Convertsfile.ps to file.pdf
pstopdf input.ps output.pdf Converts input.ps to output.pdf
pdftops input.pdf output.pdf Converts input.pdf to output.ps

 

Categories
Linux

courses.edx.org: Linux Printing with lp

 

Using lp

lp and lpr accept command line options that help you perform all operations that the GUI can accomplish. lp is typically used with a file name as an argument.

Some  lp commands and other printing utilities you can use are listed in the table.

Command Usage
lp <filename> To print the file to default printer
lp -d printer <filename> To print to a specific printer (useful if multiple printers are available)
program | lp
echo string | lp
To print the output of a program
lp -n number <filename> To print multiple copies
lpoptions -d printer To set the default printer
lpq -a To show the queue status
lpadmin To configure printer queues

The lpoptions utility can be used to set printer options and defaults. Each printer has a set of tags associated with it, such as the default number of copies and authentication requirements. You can execute the command lpoptions help to obtain a list of supported options. lpoptions can also be used to set system-wide values, such as the default printer.

 

 

Command Usage
lpstat -p -d To get a list of available printers, along with their status
lpstat -a To check the status of all connected printers, including job numbers
cancel job-id
OR
lprm job-id
To cancel a print job
lpmove job-id newprinter To move a print job to new printer

 

Working with enscript

enscript is a tool that is used to convert a text file to PostScript and other formats. It also supports Rich Text Format (RTF) and HyperText Markup Language (HTML). For example, you can convert a text file to two column (-2) formatted PostScript using the command: enscript -2 -r -p psfile.ps textfile.txt. This command will also rotate (-r) the output to print so the width of the paper is greater than the height (aka landscape mode) thereby reducing the number of pages required for printing.

The commands that can be used with enscript are listed in the table below (for a file called ‘textfile.txt’).

Command Usage
enscript -p psfile.ps textfile.txt Convert a text file to PostScript (saved to psfile.ps)
enscript -n -p psfile.ps textfile.txt Convert a text file to n columns where n=1-9 (saved in psfile.ps)
enscript textfile.txt Print a text file directly to the default printer 

 

Categories
Linux

courses.edx.org: z Command Family

Command Description
$ zcat compressed-file.txt.gz To view a compressed file
$ zless <filename>.gz
or
$ zmore <filename>.gz
To page through a compressed file
$ zgrep -i less test-file.txt.gz To search inside a compressed file
$ zdiff filename1.txt.gz
filename2.txt.gz
To compare two compressed files
Categories
Linux Uncategorized

courses.edx.org : tr, tee, wc, cut

The tr utility is used to translate specified characters into other characters or to delete them. The general syntax is as follows:

$ tr [options] set1 [set2]

The items in the square brackets are optional. tr requires at least one argument and accepts a maximum of two. The first, designated set1 in the example, lists the characters in the text to be replaced or removed. The second, set2, lists the characters that are to be substituted for the characters listed in the first argument. Sometimes these sets need to be surrounded by apostrophes (or single-quotes (‘)) in order to have the shell ignore that they mean something special to the shell. It is usually safe (and may be required) to use the single-quotes around each of the sets as you will see in the examples below.

For example, suppose you have a file named city containing several lines of text in mixed case. To translate all lower case characters to upper case, at the command prompt type cat city | tr a-z A-Z and press the Enter key.

Command Usage
$ tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ Convert lower case to upper case
$ tr ‘{}’ ‘()’ < inputfile > outputfile Translate braces into parenthesis
$ echo “This is for testing” | tr [:space:] ‘t’ Translate white-space to tabs
$ echo “This   is   for    testing” | tr -s [:space:] Squeeze repetition of characters using -s
$ echo “the geek stuff” | tr -d ‘t’ Delete specified characters using -d option
$ echo “my username is 432234” | tr -cd [:digit:] Complement the sets using -c option
$ tr -cd [:print:] < file.txt Remove all non-printable character from a file
$ tr -s ‘n’ ‘ ‘ < file.txt Join all the lines in a file into a single line

tee:
tee takes the output from any command, and while sending it to standard output, it also saves it to a file. In other words, it “teesthe output stream from the command: one stream is displayed on the standard output and the other is saved to a file.

For example, to list the contents of a directory on the screen and save the output to a file, at the command prompt type ls -l | tee newfileand press the Enter key.

Typing cat newfile will then display the output of ls –l.

 

wc
wc (word count) counts the number of lines, words, and characters in a file or list of files. Options are given in the table below.

By default all three of these options are active.

For example, to print the number of lines contained in a file, at the command prompt type wc -l filename and press the Enter key.

Option Description
–l display the number of lines.
-c display the number of bytes.
-w display the number of words.

cut
cut is used for manipulating column-based files and is designed to extract specific columns. The default column separator is the tab character. A different delimiter can be given as a command option.

For example, to display the third column delimited by a blank space, at the command prompt type ls -l | cut -d” ” -f3 and press the Enter key.

 

Categories
Linux

courses.edx.org : grep

Command Usage
grep [pattern] <filename> Search for a pattern in a file and print all matching lines
grep -v [pattern] <filename> Print all lines that do not match the pattern
grep [0-9] <filename> Print the lines that contain the numbers 0 through 9
grep -C 3 [pattern] <filename> Print context of lines (specified number of lines above and below the pattern) for matching the pattern. Here the number of lines is specified as 3.

 

 

Categories
Linux Uncategorized

courses.edx.org : sort, uniq, paste, join

Syntax Usage
sort <filename> Sort the lines in the specified file
cat file1 file2 | sort Append the two files, then sort the lines and display the output on the terminal
sort -r <filename> Sort the lines in reverse order

To remove duplicate entries from some files, use the following command:

sort file1 file2 | uniq > file3

To count the number of duplicate entries, use the following command:

uniq -c filename

PASTE:
To paste contents from two files one can do:

$ paste file1 file2

The syntax to use a different delimiter is as follows:

$ paste -d, file1 file2

Common delimiters are ‘space’, ‘tab’, ‘|’, ‘comma’, etc.

JOIN:
To combine two files on a common field, at the command prompt type

join file1 file2

and press the Enter key.

SPLIT:
By default split breaks up a file into 1,000-line segments. The original file remains unchanged, and set of new files with the same name plus an added prefix is created. By default, the x prefix is added. To split a file into segments, use the command

$ split