Filters (Linux)


One kind of program frequently used in pipelines is called filters. Filters take standard input and perform an operation upon it and send the results to standard output. In this way, they can be combined to process information in powerful ways.

List of Unix filter programs

Blue indicates I have blogged about these :



[1]  Commands like  cp and echo are not filters. They disregard the standard input stream and rely solely on the arguments found after the command

[2] Also, in some cases arbitrarily long lists of parameters cannot be passed to a command. In that case one has to use xargs.


Cp Command (Linux)

cp has three principal modes of operation. These modes are inferred from the type and count of arguments presented to the program upon invocation.

[1] When the program has two arguments of path names to files, the program copies the contents of the first file to the second file, creating the second file if necessary.

[2]When the program has one or more arguments of path names of files and following those an argument of a path to a directory, then the program copies each source file to the destination directory, creating any files not already existing.

Note: For this case I have found that the destination directory needs to exist. else there is an error 

$ cp _datafolder1/1040.csv _datafolder2/900.csv _mydata3
cp: target ‘_mydata3’ is not a directory

Works fine when directory already exists : 

agoswami@agoswami-msft2 /cygdrive/e/azureml/12Dec/TR
$ mkdir _mydata3

agoswami@agoswami-msft2 /cygdrive/e/azureml/12Dec/TR
$ cp _datafolder1/1040.csv _datafolder2/900.csv _mydata3

agoswami@agoswami-msft2 /cygdrive/e/azureml/12Dec/TR
$ ls -l _mydata3/
total 555156
-rwxrwx—+ 1 agoswami Domain Users 474014440 Dec 17 13:01 1040.csv
-rwxrwx—+ 1 agoswami Domain Users 94459824 Dec 17 13:01 900.csv

[3]When the program’s arguments are the path names to two directories, cp copies all files in the source directory to the destination directory, creating any files or directories needed. This mode of operation requires an additional option flag, typically r, to indicate the recursive copying of directories. If the destination directory already exists, the source is copied into the destination, while a new directory is created if the destination does not exist.



In terms of options, the useful ones are:

  • R or r (recursive) – copy directories recursively



[1] Creating a copy of a file in the current directory:

cp prog.c prog.bak

This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces its contents with the contents of the prog.c file.

[2] Copy two files in the current directory into another directory:

cp jones smith /home/nick/clients

This copies the smith file to the smith.jr file. Instead of creating the file with the current date and time stamp, the system gives the smith.jr file the same date and time as the smith file. The smith.jr file also inherits the smith file’s access control protection.

[3] Copy a directory, including all its files and subdirectories, to another directory:

cp -R /home/nick/clients /home/nick/customers


Binary tree in order, without using recursion

An interesting question I came across recently:

Pb:  Write a function to print binary tree in order, without using recursion. You are given a stack object, which has methods pop and push

If you are well versed in recursion, this problem may be tricky to think through at first.

Basically one needs to develop insight about a few things :

  1. The terminating condition.
  2. What to do once you have pop’d a node.






Numpy Features

The numpy documentation is quite hairy and long-winded. I am listing down the main topics from the documentation.

Bold indicates I have tried out this feature e2e myself. I intend to to keep track of progress, as I try out the different features. Hence this list for bookkeeping.

Numpy basics