Monday, 17 June 2013

Understanding differences between dpkg and apt-get/aptitude tools.

dpkg only installs a package, so doing dpkg -i packageName.deb will only install this Deb package, and will notify you of any dependencies that need to be installed, but it will not install them, and it will not configure the packageName.deb because well...the dependencies are not there.

apt-get is a Package Management System that handles the installation of Deb packages on Debian-based Linux distributions. A Package Management System is a set of tools that will help you install, remove, and change packages easily. So apt-get is like a clever dpkg.

I like to think of the timeline this way:
  • They came up with a way to "store" the files of an application in a "package" so that it can be installed easily. So the Deb package (.deb extension file) was born.
  • They needed a tool to install these .deb files, so they came up with the dpkg tool. This tool, however, will just install the .deb file, but will not install its dependencies because it doesn't have those files and it does not have access to "repositories" to go pull the dependencies from.
  • Then, they came up with apt-get, which automates the problems in the previous point. Underneath the hood, apt-get is basically dpkg (so apt-get is a front-end for dpkg), but a clever one that will look for the dependencies and install them. It even looks at the currently installed dependencies and determines ones that are not being used by any other packages, and will inform you that you can remove them.
aptitude then came along, and it's nothing but a front-end for apt-get [so, it's a front-end of a front-end]. aptitude is a UI (user interface) for apt-get. If you want to see this UI, simply type aptitude in the terminal; that's aptitude, that's what it was originally created for. aptitude leverages the apt tools to provide more options and perks than apt-get. For example, aptitude will automatically remove eligible packages, while apt-get needs a separate command to do so. But, in the end, doing sudo aptitude install packageName.deb is the same as sudo apt-get install packageName.deb. There might be subtle differences here and there that I do not know about, but they will both look for the dependencies and do all that stuff.
Also, aptitude does not have Super Cow Powers.

How do I install already downloaded packages in Ubuntu?

You may wish to install a package you have downloaded from a website, rather than from a software repository. These packages are called .deb files. Because they may have been created for a different Linux distribution, you might find that there's dependency issues with Ubuntu, meaning that they may be uninstallable. 

To find a package which you have previously downloaded using Synaptic, aptitude or apt-get, look in /var/cache/apt/archives 

You can then install the package using apt-get or aptitude.  You can also use dpkg.

Simply type the following command to install a package using dpkg.
dpkg -i <packageName>

Note :  Though dpkg will resolve the dependencies and show you if they are not met, it will not automatically fetch and install them from repositories. In fact apt-get internally calls dpkg to install packages after resolving and downloading dependent packages.

Sunday, 16 June 2013

How to install packages in Ubuntu?

This post will explain how can you install packages in Ubuntu

  1. Vis Basic Graphical Method

    You can install your softwares using Ubuntu Software center which a basic GUI tool to install softwares. You can access it simply by typing Ubuntu software center in your dash board.

    You have softwares divided into various categories on the left panel like Games, System etc. You can directly search for your software on the top right corner search area.
  2. Via Advanced Graphical Method

    The Synaptic Package Manager offers a more advanced way of installing packages. If you have problems finding a suitable package with the Add/Remove tool, try using the search in Synaptic. This searches all of the packages in the available repositories, even the ones which don't contain programs. You can access it simply by typing synaptic package manager in your dashboard. Note you need to have root access(authentication) for this.

    This will also resolve and install dependencies (if any exists) for the software you are installing.
  3. By using the CLI(Command line interface)

    The apt-get program is a command-line package manager, which should be used if the Add/Remove tool and Synaptic ever run into problems. It provides an advanced interface to APT, the underlying package management system which Ubuntu uses, but is reasonably easy to operate. Power users may find that apt-get is quicker to use and more powerful than the graphical options above.

    Some commonly used commands are

    • apt-get install <package_name> :  This command installs a new package 
    • apt-get build-dep <package_name>  :  This command searches the repositories and installs the build 
      dependencies for <package_name>. If the package is not in the 
      repositories it will return an error. 
    • apt-get install <package1_name> <package2_name> <package3_name> : APT will accept multiple package names as a space delimited list. 
    • apt-get update : This is the equivalent of "Reload" in Synaptic or "Fetch updates" in Adept.
    • apt-get upgrade : This command upgrades all installed packages.  This is the equivalent of "Mark all upgrades" in Synaptic.
    • apt-get dist-upgrade : The same as the above, except add the "smart upgrade" checkbox. It tells
       APT to use "smart" conflict resolution system, and it will attempt to 
      upgrade the most important packages at the expense of less important 
      ones if necessary. 

Sunday, 9 June 2013

Interview Question #13 synchronization on static function/method.

Let me clarify the question with an example. You have the following class.

public class Counter{
private static int count = 0;

public static synchronized int getCount()
  return count;

public synchronized setCount(int count)
   this.count = count;


You have multi threaded environment. Can two threads simultaneously access
or process getCount() and setCount() methods? 
Even if we have following case.

Counter myCounter = new Counter();


First of all you should not access static functions/block using class instance. Though it is perfectly valid to do so. Why is it valid? Each of the class instances(objects) have class information and hence can hence access it. Lets get to our original question on synchronization.

Answer is yes! Two threads can simultaneously operate/process on the two functions getCount() and setCount(). This is because when synchronized method setCount() is called thread acquires an object level lock where as when getCount() method is called it being a static method(owned by class rather than it's instances) acquires a class level lock.

When you use synchronized method or a block lock is obtained on the monitor if the corresponding instance. So in above example for setCount() method lock obtained will be on the myCounter instance where as for method getCount() which is a static or class method lock is obtained on the Class object. You can get this object by using .class keyword or getClass() method. This Class object will be same for all the instances of that class.

Saturday, 1 June 2013

How to count number of files in a linux directory?

Sometime you copy a lot of files from a directory or another media to some other directory and you wish to know how many files are copied. Here is how you can get to know how many files you have in your directory.

  1. Navigate to the directory you wish to count files from.
  2. Type in the following command ls -1 | wc -l
  3. You must get the file count as the output


 ls command

   ls command is used to list contents of the directory.  You can see the manual of the command by typing man ls in your terminal. Frequently used options with ls command are
  • -a, --all
                  do not ignore entries starting with .
  • -d, --directory
                  list directory entries instead of contents, and do not dereference symbolic links 
  • -l     use a long listing format
  • -1     list one file per line

 wc command

wc command is used for word count.
You can see the manual of the command by typing man wc in your terminal. Frequently used options with ls command are
  • -l, --lines
                  print the newline counts
  • -w, --words
                  print the word count

 Understanding ls -1 | wc -l

 We have used ls -1 in the command to find number of files in a directory. This will return the directory contents listed one file per line. This we pipe it to wc -l command which will print the newline counts. 

Note : If you use ls -l instead of ls  -1 as in ls -l | wc -l instead of ls -1 | wc -l. You will get count 1 more than the the actual number of files as this will also print one more line like 
total 116
drwxr-xr-x 3 aniket aniket  4096 May 31 22:08 Desktop
drwxr-xr-x 2 aniket aniket  4096 May  1 20:25 Downloads


Total line tells you  the total size of your directory in blocks (1024 bytes) on the hard disk, which is 116 in our example.

Screen shot:

he above method will count symbolic links as well as subdirectories in targetdir (but not recursively into subdirectories).

Excluding subdirectories

If you want to exclude subdirectories, you can do.
find yourDir -type f  -maxdepth 1 | wc -l

-type f ensures that the find command only returns regular files for counting (no subdirectories).

By default, the find command traverses into subdirectories for searching. -maxdepth 1 prevents find from traversing into subdirectories. If you do want to count files in the subdirectories, just remove -maxdepth 1 from the command line.

Note that the find command does NOT classify a symbolic link as a regular file. Therefore, the above find -type f command does not return symbolic links. As a result, the final count excludes all symbolic links.

To include symbolic links, add the -follow option to find.
find targetdir -type f -follow  -maxdepth 1 | wc -l 
t> UA-39527780-1 back to top