Home Computers Linux secrets: How to create a link with in

Linux secrets: How to create a link with in

Linux Secrets: How to Create a Link with ln

As any Linux programmer goes about their daily business they’ll create new programs and give them nice sensible names.

However, for the users it may be a laborious job having to type out a long file name all of the time – they wants something short and snappy – a nice little tla (three letter abreviation) for example. So, the question is – how can the two needs be reconciled? The answer is to use Linux Links.

An Example Script – the Disk Usage Checker

The starting point is a simple, yet useful, script – this will report any disk with more that 90% usage, and can be created very easily:

$ cd ~/bin
$ cat > check_disk_usage
grep -v “Mounted on” |
awk ‘{
if ($5 > 90) {print $1″ – Alarm”}

$ chmod +x check_disk_usage
For the Linux programmer this has been named perfectly as check_disk_usage, but the user may want something simpler and easy to type – for example cdu; therefore, the next step is to create a link

Creating a Linux Link

The link is created by using the command:

$ ln check_disk_usage cdu
If the directory is examined then two new files will be shown:

$ ls -1 c*
The files will have the same size and creation date/time:

$ ls -l c*
-rwxrwxrwx 2 bainm bainm 84 2008-12-05 15:19 cdu
-rwxrwxrwx 2 bainm bainm 84 2008-12-05 15:19 check_disk_usage
And even the inodes are the same:

$ ls -i1 c*
1002029 cdu
1002029 check_disk_usage
In this case a hard link has been created and, as far as the system is concerned, they are the same file; however, there is a problem with using a hard link – it doesn’t always work when the file and the link are in different directories (especially if those directories are on different disks). The solution is to use a symbolic link.

Symbolic Links

Rather than being a direct link (as with the hard link) the symbolic link simply holds the path to the target file; it’s created by using the -s option with ln:

$ rm cdu
$ ln -s check_disk_usage cdu
This time the inodes are different:

1002025 cdu
1002029 check_disk_usage
And the link is shown as such in the directory listing:

lrwxrwxrwx 1 bainm bainm 16 2008-12-05 15:48 cdu -> check_disk_usage
-rwxrwxrwx 1 bainm bainm 84 2008-12-05 15:19 check_disk_usage

How to Determine a File Type on Linux

When presented with a number of file names the typical human being will make certain assumptions. For example, they will make the quite logical assumption that:

a file with a .txt file extension will be a text file
a file with a .png file extension will be an image file

However, in many cases they may be wrong:

the file may have been saved incorrectly
the file may have been renamed and given the wrong file extension

Fortunately Linux makes no such assumption. Linux has the “file” command.

The Linux File Command

The file command carries out three types of test on a file:

file system tests – is the file empty, a socket, executable, etc?
magic number tests – do the file contents have a predetermined format? These are normally listed in the file /usr/share/file/magic
language tests (C, PHP, etc)

File will stop at the first classification that it comes across and any file that cannot be classified from these tests is labeled as ‘data’.

Using the Linux File Command

The file command is easy to use and the file type testing can be examined. It’s just a matter of opening a Linux or Cygwin terminal and typing

touch test.txt #where, of course, test.txt is a file that doesn’t exist yet
file test.txt
File will report that the file is empty. Next it can be given some text and it can be tested again:

echo “This is not empty” > test.txt
file test.txt
This time file will report back that the file type is “ASCII text” (as shown in figure 1 at the bottom of this article).

Working with Other File Types

While it’s useful seeing that a particular file contains text, the file command can return even more useful information. For example:

file 01\ Icon.png
The output will be something like:

01 Icon.png: PNG image data, 40 x 40, 8-bit/color RGB, non-interlaced
And just to check that file is not just using the file extension to identify the file type:

cp 01\ Icon.png 01\ Icon.ugg
file 01\ Icon.ugg
The response, of course, will be the same:

01 Icon.ugg: PNG image data, 40 x 40, 8-bit/color RGB, non-interlaced
This information can now be used to manage the files on a computer, or even just to report what files are there.

Creating an HTML Web Page Using the File Command

The file command can be used to create a web page containing all of the image files in a directory. It can’t do it directly, but the file command can be used as part of a Linux script. The starting point is to identify all of the image files:

for f in $files
do file “$f” |
grep “image data” |
awk -F: ‘{print $1}’
Here all of the file names have been loaded into a variable and then each is examined with the file command. The grep command filters any unwanted (non-image) files, and awk is used to extract the file name from the output (as can be seen in figure 2). The next step is to format the output correctly:

for f in $files
do file “$f” |
grep “image data” |
awk -F: ‘{
print “

print $1”

done > images.html

The end result is an HTML file that can be viewed by using a web browser (as shown in figure 3). And so, with just a few simple commands the Linux application developer is able to create a script that will identify the file types in any directory and, in this case, to create an HTML output from those results.