Master df Command in 10-Minutes

 Command Line  Comments Off on Master df Command in 10-Minutes
Feb 232015
 

Today let’s talk about df, the command line utility providing information on disk space used and remaining on your file systems.

Unsurprisingly, df stands for disk free.

Besides listing how much disk space is used and how much is free, df also displays how the file systems are mounted on your Linux desktop or server.

With df, you get disk use information on your local system as well as external drives attached to it.

The basic syntax of df is:

df [OPTION]… [FILE]..

One caveat I must add here is that df will not provide you with the size of directories or files. The command for that is du and we’ll cover that in a separate article.

Here are a bunch of df commands that you can grasp in less than 10 minutes.

Should you not provide any file name while running df, the space available on all mounted file systems will be displayed.

1. Basic df Command

When you run the df command without any options, the output provides disk utilization in 1K blocks.

larry@tammypc ~ $ df
Filesystem      1K-blocks      Used  Available Use% Mounted on
/dev/sda1      1918766120 157599888 1663675496   9% /
none                    4         0          4   0% /sys/fs/cgroup
udev              1925276         4    1925272   1% /dev
tmpfs              388172      1392     386780   1% /run
none                 5120         0       5120   0% /run/lock
none              1940848       788    1940060   1% /run/shm
none               102400        20     102380   1% /run/user
/dev/sdb1       244137920 205701856   38436064  93% /media/larry/HITACHI

In the above example, sda1 is the local drive and sdb1 is an external drive.

Apart from physical hard drives, df will list mounted file systems such as udev for /dev and tmpfs filesystem for /run and its subdirectories. Those with a grounding in Linux will quickly recognize that these file systems run in memory and are part of Linux.

2. Include All File Systems

The df -a command will include all file systems including those that have a size of 0 blocks, which are omitted by default (such file systems are usually special-purpose pseudo-file systems like automounter entries).

$ df -a
Filesystem      1K-blocks      Used  Available Use% Mounted on
/dev/sda1      1918766120 157529716 1663745668   9% /
proc                    0         0          0    - /proc
sysfs                   0         0          0    - /sys
none                    4         0          4   0% /sys/fs/cgroup
none                    0         0          0    - /sys/fs/fuse/connections
none                    0         0          0    - /sys/kernel/debug
none                    0         0          0    - /sys/kernel/security
udev              1925276         4    1925272   1% /dev
devpts                  0         0          0    - /dev/pts
tmpfs              388172      1408     386764   1% /run
none                 5120         0       5120   0% /run/lock
none              1940848       736    1940112   1% /run/shm
none               102400        16     102384   1% /run/user
none                    0         0          0    - /sys/fs/pstore
binfmt_misc             0         0          0    - /proc/sys/fs/binfmt_misc
systemd                 0         0          0    - /sys/fs/cgroup/systemd
gvfsd-fuse              0         0          0    - /run/user/1000/gvfs

3. Display for Specific File System

We can narrow down space used for a file system with the below command.

$ df /dev/sda1
Filesystem      1K-blocks      Used  Available Use% Mounted on
/dev/sda1      1918766120 157541184 1663734200   9%  /

4. Display in Human Readable Format

By default, df displays information in kilobytes.

Now in a world of gigabytes and terabytes hardly anybody uses kilobytes these days. So the df folks came up with the -h option.

Of all the df options, the most useful one is df -h because this gives the output in human readable format.

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       1.8T  151G  1.6T   9% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            1.9G  4.0K  1.9G   1% /dev
tmpfs           380M  1.4M  378M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.9G  736K  1.9G   1% /run/shm
none            100M   16K  100M   1% /run/user

I wouldn’t be surprised if df –h is the most used among the various df options.

5. Ignore Virtual File Systems

More often than not, we don’t need space use pertaining to virtual file systems.

What we are interested is are the partitions on the hard drives.

Here’s an example of how we can remove information pertaining to virtual file systems that exist only in memory:

df -h |grep ^/
/dev/sda1       1.8T  151G  1.6T   9% /
/dev/sdb1       233G  216G   18G  93% /media/larry/HITACHI

Continue reading »

Getting to Grips with Grep

 Command Line  Comments Off on Getting to Grips with Grep
Feb 022015
 

Familiarity with grep is a must for aspiring Linux systems administrators.

In my Linux lexicon, grep ranks among the most useful command line tools.

So why is grep such a useful tool?

Grep (which stands for global regular expression print) lets users find text or other patterns in files.

The good thing is that grep usually comes pre-installed with most Linux distributions.

Although grep might not be as frequently used as as cp or ls, it ranks among the top commands for systems administrators.

Grep – Syntax

The basic syntax for the oft-used grep command is:

grep [OPTIONS] PATTERN [FILE…]

When grep finds the pattern, it prints the lines containing the specified search string or pattern.

Your search expression could be text, numbers or metacharacters.

Let me illustrate the use of grep with some examples.

Here’s how you’d use grep to find the word “salmon” in a long recipe file.

bobby@centos-gal $ grep salmon Myrecipe-file.txt

Now that was a basic illustration of how we can use grep.

Let’s now consider a slightly more complex example involving grep.

When you’re analyzing Apache error_logs of a RedHat or any other Linux server, you might want to check the total number of wp-login attacks (a serious issue that WordPress bloggers encounter).

bobby@centos-gal $ tail -500 /var/log/httpd/error_log | grep -w wp-login -c
85

Grep – Various Options

Grep has several options that expand its capabilities.

Let’s examine a few of the key ones.

Case Insensitive Search
With the below -i option, you can do a case insensitive search:

bobby@centos-gal $ grep -i "search_string" File_Name

Invert Search
You can also use grep to print out lines that do not match a pattern via the -v option.

This is known as invert-search and your output will show only non-matching lines.

bobby@centos-gal $ grep -v "search_string" File_Name

Continue reading »

Avoid Dependency Hell with Gdebi

 Command Line, Linux  Comments Off on Avoid Dependency Hell with Gdebi
Jan 312015
 

If you’re running a Debian-based Linux system, I suggest you keep gdebi in mind.

While Debian has a humungous collection of packages that runs into tens of thousands, there are occasions when a particular software you want might not be part of the default distribution.

Under such circumstances what do you do?

You either fuggedaboudit or you download the program to your computer and install it via dpkg -i, all the while praying for a miracle that you won’t run into dependency issues with your local.deb package.

$ sudo apt-get update
$ sudo dpkg -i local.deb

But if you run into dependency issues with the program, you are back to square one because the dpkg command does not address dependency problems of your local.deb package.

Use gdebi

A better option to install local.deb packages without worrying about dependency issues is to use gdebi.

An extremely useful tool, gdebi allows you to install local.deb packages without worrying about resolving and installing the necessary dependencies.

Since gdebi is not installed by default with most distributions including LinuxMint 17, you’ll have to install it on your own.

How to Install gdebi

Installing gdebi is child’s play if you know how to type. 😉

Look how simple it is:

$ sudo apt-get update
$ sudo apt-get install gdebi

On distros like LinuxMint, you have the option to install gdebi via the Software Manager.

Once you have gdebi installed, it’s no sweat. Continue reading »

How to Slay at Will in Linux

 Command Line  Comments Off on How to Slay at Will in Linux
Jan 312015
 

For Linux administrators, slay is a convenient tool to kill all processes belonging to a user.

Slay comes in handy under various situations.

For instance, as an administrator you notice a particular user running processes he is not permitted to.

Or your room-mate is downloading a huge file or doing something else in the background that’s hogging a lot of system resources.

Maybe you’re looking to delete a user but find that he still has lot of processes running.

There could be any number of reasons to use slay.

We owe thanks to Chris Ausbrooks for first writing this shell script. Subsequently, Pawel Wiecek rewrote this for Debian.

You should be able to find slay in the official repositories of major distributions.

Installing Slay

Installing slay is easy as pie.

On Ubuntu and its derivative distributions like LinuxMint, just run the below command:

sudo apt-get install slay

If you’re running Fedora, CentOS or RedHat, use the below command:

sudo yum install slay

No, we haven’t forgotten users of Arch Linux and its derivatives. Go with the below command to install slay on your system:

sudo pacman -S slay

Using Slay

Here’s the syntax for slay:

slay [-signal] name [name…]

When you run the slay command, it sends the signal (KILL by default) to all processes belonging to a particular user or group of users.

When you use -clean as a signal name, a “clean kill” is done. In other words, processes are first sent TERM signal and after 10 seconds those processes that haven’t terminated yet are killed with KILL.

Here’s an example of using slay for all processes belonging to user jason:

$ sudo slay jason
slay: Done.

In the example, all processes belonging to user jason are terminated.

Go ahead, install slay on your system and familiarize yourself with this useful tool.

Slay Related Commands:
TCPKill – When only the Nuclear OPtion will do

How to Add EPEL Repository to CentOS

 Command Line, Linux  Comments Off on How to Add EPEL Repository to CentOS
Jan 312015
 

So you’ve installed CentOS on your computer and have it up and running without any issues.

Good job!

Now we can move to another key step in your CentOS learning adventure – Adding more repositories.

For purposes of this post, it doesn’t matter whether you’ve installed the older CentOS 6.6 or the newer CentOS 7.0.

What is a Repository?

As a newbie, you might be forgiven for wondering what the heck a repository is in Linux.

Think of a repository as a central location from where you can download software packages to your system.

Why are repositories important?

When you first install a Linux distribution (CentOS, RedHat, Oracle Linux, Scientific Linux etc), you usually get only a limited set of packages with it. But later you might feel the need for a software package that might not available in the CentOS base repository. So you look to third party repositories for the packages you want.

A repository often contains thousands of software packages ranging from browsers to RSS readers to text editors to more complex stuff like IDEs (integrated development environments).

The beauty of Linux is its flexibility, which lets you add or delete any package you want later.

When you download a package from a repository using commands like yum install package_name (for RedHat, CentOS and Fedora systems) or apt-get install package_name (for Debian, Ubuntu and LinuxMint systems), they are downloaded from a repository and you get dependency resolution automatically. Dependencies are other programs that must be installed for your desired package to work properly.

So when you install the Libre Office package from a repository, you also automatically get other software components that are required for Libre Office to work well.

Bear in mind that packages and repositories are distribution specific.

For instance, the popular EPEL repository is meant for users running Fedora, CentOS or Red Hat and not for those who have deployed Debian, Ubuntu or LinuxMint on their servers or desktop computers. Ditto with packages. Those meant for Debian and its derivatives will not work on CentOS, RedHat or Fedora and vice versa.

Good as the base CentOS and Red Hat Enterprise Linux repositories are, they are not comprehensive. As a matter of fact, no single repository is ever comprehensive enough.

So there arises the need to install other repositories like EPEL (an acronym for Extra Packages for Enterprise Linux), IUS, ELRepo, ATrpms etc.

Before you go about installing other repositories, first check what’s installed on your computer with yum repolist all.

The below command should provide a list of all repositories (both enabled and disabled) on your CentOS system:

michael@centos-guru ~]# yum repolist all

Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: ftp.osuosl.org
* extras: mirror.cc.columbia.edu
* updates: mirror.es.its.nyu.edu
repo id repo name status
base CentOS-6 – Base 6,518
extras CentOS-6 – Extras 36
updates CentOS-6 – Updates 565
repolist: 7,119

Now that you know what repositories are available on your computer you can install whichever repository you want.

Let’s install EPEL, a well supported repository for CentOS, Fedora and RedHat Linux distributions

Responsibility for maintaining EPEL rests with the Fedora Special Interest Group.

How to Install EPEL

Installing EPEL repository on a CentOS or RedHat desktop or server is easy when you follow the below steps.

EPEL on CentOS 6.6

Before installing the EPEL repository, make sure you’re on root and then run the below commands (depending on whether you’re running 32-bit or 64-bit CentOS and the version):

For 32-bit CentOS 6.x Systems

$ wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6*.rpm

Continue reading »

What Packages are Installed on My Linux System?

 Command Line, Linux  Comments Off on What Packages are Installed on My Linux System?
Jan 062015
 

It’s always good to keep an inventory of what packages are installed on your Linux server or desktop.

Unfortunately there’s no single command here that does the trick on all Linux systems.

Different Linux distributions require you to type different commands to display installed packages.

Let’s take a deko at commands for a bunch of key distributions.

RedHat, CentOS & Fedora

For RPM based distributions like RedHat and CentOS, run the below command to get a list of installed packages:

$ rpm -qa

Alternatively, you could run:

yum list installed

If you want to save the output to a text file for later reference or printing, issue either of the below commands.

$ rpm -qa > Installed_Packages.txt

or

$ yum list installed > Installed_Packages.txt

My recommendation is to run the commands frequently (say once a month) and keep a copy of the output with the dates saved in the file.

Debian, Ubuntu & Linux Mint

Since a lot of folks these days are running Linux Mint or Ubuntu on their desktops, let’s see the appropriate command for these Apptitude distributions.

$ dpkg -l

If you want to save the output to a file, run the below command:

$ dpkg -l > Installed_Packages-Jan-5-2015.txt

Arch Linux & Manjaro

While Linux Mint is the clearly the flavor of the season on the desktop side, Arch Linux is not without its fan base, particularly among experienced users.

By the way, Arch Linux and Manjaro are Pacman based distributions.

$ pacman -Q

To retrieve a list of the files installed by a package, here’s what you need to run:

$ pacman -Ql package_name

OpenBSD & FreeBSD

Here’s the command to get the list of packages installed on your FreeBSD system:

Pkg is the package management tool for both FreeBSD and OpenBSD.

$ pkg info

For older versions of FreeBSD (i.e. pre 10.x), use the below command:

$ pkg_info

OpenBSD

$ pkg_info

If you’re looking for the list of programs (not packages) installed on your system, try:

$ compgen -c

I’ve tested compgen –c on Linux Mint 17.1 and CentOS 7. It worked fine on both.

As always, be sure to check the man pages for each of these distribution for the variety of options.

Related Installed Packages Information:
FreeBSD PkgPrimer