Linux Learnings

dave@mule:~$ arr=($(ipcs -p | awk -F ' *' '$3 ~ /^[0-9]+$/ {print $3}' ))
dave@mule:~$ for i in "${arr[@]}"; do :; ps aux | grep -v grep | grep -e $i; done

This code firstly creates an array of the 3rd column’s numerical values from the [ ipcs-p ] command and then in the second command it iterates over the array and filters out the [ps aux] results using [grep] to display only entries matching the item in the array.

It’s not terribly efficient but I’m just starting out here… ūüôā

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 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
ps2pdf to file.pdf
pstopdf output.pdf Converts to output.pdf
pdftops input.pdf output.pdf Converts input.pdf to 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
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 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 textfile.txt Convert a text file to PostScript (saved to
enscript -n -p textfile.txt Convert a text file to n columns where n=1-9 (saved in
enscript textfile.txt Print a text file directly to the default printer : 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 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 (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 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.