≡ Menu

grep command

I install many packages for Debian / Ubuntu Linux from both stable/testing environment but some time I need to find out installed package is from stable or testing environment. I can use dpkg status file to get this information. But Debian comes with perl script called apt-show-versions which lists available package versions with distribution.

apt-show-versions parses the dpkg status file and the APT lists for the installed and available package versions and distribution and shows upgrade options within the specific distribution of the selected package. apt-show-versions uses caching for the status information of installed and available packages. If you run apt-show-versions as root the cache is updated as needed. If you run as non-root uses the newest available information, but can’t update the cache. If you run as root with the option -i the cache is initialized or updated only.

This is really useful if you have a mixed stable/testing environment and want to list all packages which are from testing and can be upgraded in testing.

Install apt-show-versions

Type the following command at shell prompt:
$ sudo apt-get install apt-show-versions
Just type command apt-show-versions:
$ apt-show-versions

java-common/testing uptodate 0.23
libperl5.8/testing upgradeable from 5.8.7-10 to 5.8.8-2
sysutils/testing upgradeable from 2.0.0-1 to 2.0.1
autoconf/unstable uptodate 2.59a-8

To upgrade all packages in testing you can type command:

# apt-get install $(apt-show-versions -u -b | fgrep testing)

Find out a list of all available versions of postgresql database server:

$ apt-show-versions -a -p postgresql

Reading an IP address in shell script required many time. However, different Linux distribution stores IP address in different files. If you are looking to run script under different UNIX like OSes such as Solaris or FreeBSD then you need to use the ifconfig command. The ifconfig command is not just used to configure a network interface, but it can be use to obtained information such as network IP, netmask and much more.
[click to continue…]

Installing software from a source code is common practice in UNIX and Linux world. Some time this is preferred method because it gives all power and flexibility you need to optimize your software such as MySQL, PHP, and Apache etc. However, uninstalling files installed from a source code tar ball is a big headache.

Two methods can be used to uninstall files:

Method # 1: make command

Use command make uninstall or equivalent supported command, Read INSTALL or README file in source code file to find out more about this method.

# make uninstall

Sure, this method sounds very easy but not supported by all tar balls.

Method # 2: find command

(a) Make a list of all files on the system before installing software i.e. a pre-installation list of all files on your system.

find /* > packgetlist.b4

(b) Now install the software (use configure & make to compile it)

make install

(c) Now make a list of all files on the system after installing software i.e. postinstall list

find /* > packagelist.after

(d) Next, compare both lists using the diff utility to find out what files are placing where. This list can be use to uninstall all files installed using source tar ball.
diff packagelist.b4 packagelist.after > package.uninstall.list

(e) After some time if you wish to uninstall files then you need to get list of files from package.uninstall.list file. Use following small for loop at shell prompt to remove all files:

for i in $(grep ">" package.uninstall.list | awk '{ print $2 }')
/bin/rm -fi $i

A note about binary packages

If you are using Debian / Ubuntu Linux, use following command to uninstall binary packages:
sudo apt-get remove {package-name}
If you are using Redhat / RHEL / Fedora / CentOS / Suse Linux, use following command to uninstall binary packages:
rpm -e {package-name}
yum remove {package-name}

Linux commands to help you navigate

As a Linux system administrator, you will need to find files in directories all over the file system. Especially those coming from a Windows background, often lost themselves while navigating file system.

Linux and other UNIX (BSD) OS offers an excellent collection of utilities, which can be use to finding the files and executables, remember you cannot memorize all the commands and files ;)
Commands to help you navigate:

  • file: Determines file types
  • which: Locates an executable in your PATH
  • whereis: Locates binaries and man page
  • find: Find the file
  • grep: Search for text/string in the named file name
  • strings: Find text string in a binary file

The which command

It is useful to locate a command. Some opertating system such as Solaris/HP-UX (even linux) have multiple homes. So you wanna find out which version you are going to use by order of the directories in your PATH variable. Try out following commands:
$ which ls
$ which vi
$ which vim

The file command

You would like to find out if a command is a shell script or a binary file or simply cannot recognize file by its extension then use file command to determine file type.
$ file /usr/sbin/useradd

/usr/sbin/useradd: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.0, dynamically linked (uses shared libs), stripped

Let us try another example:
# file /etc/shadow

/etc/shadow: ASCII text

But wait sec, you don't have to type full command path:
$ file $(which adduser)

/usr/sbin/adduser: perl script text executable

The whereis command

It locates binaries and man pages. When you get message command not found then use whereis command to locate binary file. For example ifconfig command:
$ ifconfig

bash: ifconfig: command not found

Now locate ifconfig binary, enter:
$ whereis -b ifconfig

ifconfig: /sbin/ifconfig

So let us try the full path, enter:
$ /sbin/ifconfig

The grep command

The grep command can search for text or strings such as IP address, domain names and lots of other stuff inside a text file. Often new Linux sys admin forgets to configuration file names. However, you can use grep to find out those configuration file name. For example, find out the file containing IP address
# grep -R "" /etc/* | less

Find out kernel driver module bttv configuration file name, so that you can remove the driver:
# grep -R "bttv" /etc/* | less<

The strings Commands

The grep command is useful to search a text file, if you would like to find text string in a binary file then use strings command.
# strings /usr/bin/users

You might think this is stupid idea to search inside binary file for text string. Well, no it is not a stupid idea. For example, you would like to quickly find out if internet service supports tcpd access control facility via /etc/hosts.allow and /etc/hosts.deny files (read as tcp wrappers) or not. Let us find out if sshd server support tcpd or not:
# strings $(which sshd)| grep libwrap

libwrap refuse returns

The find Command

Use find command to find the files. Find all files belonging to the user charvi:
# find / -user charvi

Remove all core dump files
# find / -name core -exec rm -i{}\;

Please see more find command examples here and here. For more info please read the man pages of find, grep, file, which.