mkdir -p is your friend

mkdir -p is a command second only to touch in succinct utility.

touch creates a file if it does not exist, or updates its timestamp if it does. It’s handy if you want to write to a file without checking for its existence, as otherwise you’d need to determine whether or not append is the correct mode. It’s also handy for setting flags for yourself on the filesystem.

mkdir -p creates a path if it does not exist, or does nothing if the path already exists. mkdir -p /foo/bar/baz will create /foo, /foo/bar, and /foo/bar/baz for you. Conversely, mkdir -p /usr/local/bin will not complain because those directories already exist.

Why would you need this? A couple reasons that came up for me tonight:

  • You cannot redirect output to a file if the file is in a directory that does not yet exist
  • You cannot create a symbolic link in a directory that does not yet exist

Startup order of services on RHEL or CentOS

The startup order of services on Red Hat Enterprise Linux (and very likely other flavours) is determined by the numerical prefix on the symbolic links in /etc/rc3.d/ (for run level 3), /etc/rc5.d/ (for run level 5), and so on.

When adding a service (chkconfig –add my_service –level 35), you can specify startup order by including a chkconfig clause in the header:

The above service should start at run level 3 and 5 as the 59th service to run (the numbering is sparse, so there isn’t necessarily as a 58th service).

The 80 specifies the shut down order.

Obviously, do not rename the symbolic links yourself. Use chkconfig to manage them.

cpio: chown failed – Invalid argument

I ran into this error while installing the IBM Java RPM on Red Hat Enterprise Linux:

error: unpacking of archive failed on file /opt/ibm/java-x86_64-60: cpio: chown failed - Invalid argument

The issue is due to /opt/ibm being an NFS mount on the system. There are known issues with running the chown command on NFS 4. One workaround is to specify NFS protocol version 3:

In my case, the NFS mount was specified in /etc/fstab, so I modified the relevant line there to say:

Linux Basics: Navigate with ls, cd, and pwd

The Linux command line can be a bit intimidating at first, but it gets much easier once you learn a few basic building blocks. The power of the command line lies in combining many basic commands in interesting ways.

Open up a Linux terminal (or, if you want to follow along on Windows, Cygwin). The darkness of the abyss stares at you, but it’s really not as unfriendly as all that.

First things first. Where am I? Type “pwd” without quotes and hit enter:

You should see something like “/home/leonsp”. That’s your home directory, similar to your Documents folder on Windows. “pwd” stands for “print working directory”, and is a handy way to find out your current location.

Let’s try going somewhere else. Enter “cd ..”, followed by “pwd”.

Two dots means one directory up. You just changed the directory to one directory up from /home/leonsp, which is /home.

Let’s get back to your home directory. There’s a few ways to do this. The following commands will all do the same thing:

~ is convenient shorthand that refers to the home directory of the current user — you.

What’s in all these directories? Let’s list the contents using the “ls” command:

Not much will show up, as your home directory starts out with few files in it. It will, however, have a bunch of hidden files. Let’s list all of these:

By convention, Linux treats all filenames starting with a dot as hidden. They will only show in the listing when you ask to see them.

.bash_history, .bash_profile, and .bashrc are configuration files for my shell, Bash. Which shell you have mostly affects scripting (or automation), which is a more advanced topic. Bash, ksh, and sh are similar, while csh and tcsh are a bit different.

. and .. will show up everywhere. Single dot “.” refers to the current directory, and two dots “..” refer to the parent directory.

It’s possible to get a long, more detailed listing:

We’ll get into what these columns mean later.

Enable NFS services for HADR

You might need to enable network file system (NFS) services when configuring the High Availability Disaster Recovery (HADR) or Database Partioning (DPF) DB2 features. For example, with HADR, you might want to set up a slave database that shares a backup with the master database through NFS.

These commands should enable NFS services on most distributions of Linux:

Some of the services might not exist on your distribution. Additional steps may be needed.

IBM Smart Business Cloud for Test & Development might be one of the environments where you’d have to do this.

iptables pitfall

An important thing to remember about rulesets in /etc/sysconfig/iptables is that they are chains. The first rule is applied, followed by the second, and so on. It’s the opposite of CSS that way. More specific rules should go first, while all-encompassing rules should go last.

I was trying to open the usual DB2 ports on RHEL. For some reason, nothing was working.

It turned out that this line was at fault:

This rejects all incoming traffic not otherwise allowed. The line has to go last in the file, after all the open port definitions.

device br0 already exists

Here’s how you solve either of the following errors on Ubuntu (and possibly Debian):

Removing the device specs from /etc/network/interfaces and restarting the network doesn’t actually remove the device if already active. You need to do it manually.

List the active devices:

And then do this to any that you don’t want there, such as br0, eth0, eth1, etc:

This deactivates the device. At this point you’ll need to restart the network layer twice:

And you should be sitting pretty. On some systems, networking is known as network, as in:

Disabling PHP in a specific directory

To disable the PHP processor in a given directory, put the following in an .htaccess file. If one doesn’t exist, create it.

This assumes an Apache server. PHP on IIS may involve different steps.

Files starting with a . are hidden by default on *nix OSes. To see them in listings, use ls -a.

History meme

Substantial content is in the pipes, but in the meantime here’s Arve Bersvendsen’s history meme:

In my cygwin:

And on a Linux machine I administrate:

I can survive in vi if pressed, but nano is my text-mode editor of choice. I do serious Linux development in Eclipse, Kate, or Kdevelop.