Magnetic Field

Stories pertaining to Magnetic Resonance, Chemistry, Programming, and Life

Hard and Soft Linking in Linux

Sometimes it’s nice to have files accessible from more than one location on your computer, but to only actually have one copy of the file (it can save on drive space for really big files!). An example of where I have needed this most, actually, has been with the use of Dropbox on my laptop. Many times I’ve been working out of a folder somewhere on my computer and I’ve needed to make the files accessible to other computers in the research lab. I’ve had to physically copy and paste these directories to my Dropbox folder (or usb drive, before I discovered Dropbox) and then once all the edits had been made elsewhere, had to copy and paste them back to where they belong. If, however, I could place a link to the files in my Dropbox folder, both copy and paste steps could be avoided (as well as the simultaneous confusion of which files were updated, and in what order etc.).

In linux systems this “linking” process is called “symlinking” (softlinking) or “hardlinking.” The difference between the two is a little subtle and can be understood in that unix files have two parts the “file header” (which includes the name) and the “data” part. The header part contains the file name, permissions, etc, and also contains an inode number. This inode number is like the data part’s address on the hard drive…and points/associates the file name to the data it contains. One way to think about it is through this schematic:

You can imagine that linking to the file can be accomplished through one of two methods. One method is to simply have two filenames associated with the same inode. These two filenames (file header information) can reside in different physical locations on the hard disk and thus in two different locations in your folder tree of your computer. This process is called “hardlinking” and looks like this:

Hardlinking is great. If you delete filename1, filename2 still has the data stored in itself even though the file was originally created with filename1. Dropbox supports this linking feature. The only drawback to this kind of linking is that the link literally looks like and feels like the file in any file system–you can’t really tell the difference. So you better remember what was supposed to be the link! The filename doesn’t show up as a link because it has the same properties as a normal file… file header information (stored in the filename1, including the inode number), which links to the data.

Through command line, the way you create one of these hardlinks is through the following command:

ln file-to-be-linked.txt linking-file-name.txt

Now, the other way of linking files is a literal link: its a special unique file that has a file header portion with an inode link, but instead of that inode pointing straight to the file data inode, it points to the inode of its own file within which contains a “link” to the file you’re linking too. It’s much like a shortcut in Windows…and this is called soft-linking, or forming a symbolic link or “symlink.” It works in the following manner:

Obviously that “to.txt” file is the file header information that contains the inode number of where the data actually resides…so soft links are literally a special file that points to another file and the operating system treats it as such…its obvious that its a link and if you delete the symlink it just no longer points to the file anymore. And if you delete the original file and the symlink still links to it, the data is lost…it is not kept.

To access this kind of linking in the command line, you run the following command:

ln -s file-to-be-linked.txt name-of-link-to-file

Notice how I dropped the file extension off of the name of the link…you can do that because it is its own type of file that just points to the real thing. Dropbox also supports this kind of file linking. And it comes in handy particularly if you want the file to show up as a symlink.

Now you might be asking yourself why bother with the soft linking if hard linking works so well? The answer might be baffling: all directories in unix/linux systems are soft links that contain a name and point to a data file that contains all the file header information location of all the files associated with that directory. I mean, what are folders anyways? They aren’t “files” in that they don’t have data intrinsic to themselves. But they are “containers” that tell you what is associated with them. Brilliant? Yes. Immediately intuitive? No.

So a “directory” or “folder” has its own file header information which points to an inode number that contains all the addresses of all the files associated with itself. When you go to one of these addresses of the contained files you get pointed to the inode that contains all the data associated with that file. That’s how folders work–and if you want to use a symlink to point to a directory, you have to add a special flag to the linking command:

ln -sd directory-to-be-symlinked name-of-link

Of course, you can also hardlink a directory, then the code would just be:

ln -d directory-to-be-hardlinked name-of-link

I think I mostly use symlinking so I remember its a link and not the actual file, but I’m very appreciative that Dropbox supports this option. Makes changing files in different directories on my computer remotely very easy to do!

Attenuation and dB’s

It’s easy to not understand NMR verbiage. One second we talk about signal processing on the computer using terms like “apodization” and “digital filters,” but most spectrometers still do a lot of signal processing before the signal ever reaches the computer. That is, the signal is amplified, filtered, converted, and digitized before it ever reaches the user interface. This means that a lot must go right before it reaches the computer for you to have any intelligible signal whatsoever.

Of course, there’s also the electronics that prepare the pulse before the NMR signal is measured as well. That is, we have the pulse generator, and then pulse processor, followed by an amplifier that amplifies the signal to tens or even thousands of watts of power.

At both this transmitting end and at the receiving end, there’s a way to control how much power is going out and how much power is coming back in. These controllers that control the outgoing and incoming signal are called attenuators, and they have the special properties of reducing the signal power and not altering the signal in any other way. In a sense, you can think of an attenuator as an “amplifier” that “amplifies” the signal to a power less than one, that is amplifying to a power of something like .1, .5, etc.

Attenuators typically measure this reduction of signal in a unit called dB’s, or decibels. Decibels are an interesting and not immediately intuitive logarithmic unit derived by early telephone engineers seeking to define signal loss over different lengths of wires. The “bel,” named after the inventor of the telephone, Alexander Graham Bell, is ten decibels, and was equal to ten of the “Transmission Units” (or “TU”) in common use by engineers in the Bell Telephone company. A “TU” was defined as “ten times the base-10 logarithm of the ratio of the measured power over a reference power.”

We’re familiar with dB’s from the measurement of sound volume, where 0 dB’s is considered the threshold of hearing and 30 dB’s is like a quiet bedroom at night. Conversational speech at about 1m is 60 dB’s, a busy road about 80 dB’s, a chainsaw about 110 dB’s, and discomfort beginning at about 120 dB’s. It’s an exponential scale as the growth of sound power isn’t linear in the least. Going from 0-60 dB’s is not painful at all. But going from 100-160 dB’s could result in partial loss of hearing.

When the attenuator decreases the “volume” of the waveform in the NMR spectrometer, it does so by a special arrangement of resistors that decrease the amplitude of the waveform. This amplitude of the signal is, of course, the voltage. So attenuating the waveform attenuates the amplitude of the wave, and it does this by a series of resistors carefully arranged to attenuate at just the precisely chosen dB value.

Of course, attenuation is attenuating the power (which is proportional to the amplitude) and not the amplitude directly. How is the power related to the amplitude? Well, power in watts, is:

P = \frac{V^2}{R}

Thus, a change of the power in terms of dB’s is related to the change of voltage squared.

So, as already eluded to, the mathematical description of dB’s in terms of power is:

-x\text{dBs} = 10 log_{10}(\frac{P_1}{P_0})

I’ve written it with a negative sign in front of the x dB’s. This is because we’re using the equation in terms of attenuation, not boosting. The way to think about this is we’re “taking away” dB’s from the system, thus we have to include the negative sign. Now, if we want to write it in terms of voltage, we have to substitute:

P \propto V^2

And thus get:

-x \text{dBs} = 10 log_{10}(\frac{V^2_1}{V^2_0})

Or, simplified:

-x \text{dBs} = 20 log_{10}(\frac{V_1}{V_0})

In general, we use the above to calculate how much attenuation we get when we apply a certain number of dB’s of attenuation. It can be rearranged in the following form:

V_1 = V_0 10^{\frac{-x \text{dBs}}{20}}

One might ask why we would use dB’s for attenuation in NMR at all. The short answer is that when processing and receiving signal we sometimes need to span many orders of magnitude. Say we have a dense sample of water that we are doing 1H NMR on. That’s a huge signal compared to 13C NMR…and sometimes many orders of magnitude apart. dB’s are a good choice of scale in this kind of situation because you can throttle signal (or boost it) from as little as 1/1000% to its full scale.