Links and metacharacters in Linux


Links and meta characters in Linux   We will continue our Linux journey by talking about two important components of the File System:
links (soft and hard links) 
metacharacters
This would be an important step to further understand how this OS works and what are the main concepts behind its functionality. I will try as best as I can to explain these two concepts so please post a comment if you think that things were not described properly.

   Besides the actual data contained in each file within the File System, the Operating System will also store specific information for each one. Files are described by multiple attributes and we’ve seen some of them when we’ve learned about the ls -la command in this article. Attributes like file type, group and user ID, file permissions, file size, last modification date, creation date, etc, must be stored somewhere in the File System. In Linux, all this information is stored within inodes (Index-nodes). An inode will keep all data regarding a particular file except its name and each one will be identified by a unique number. They also store a list with all data blocks that are part of the same file and where is each block located on the hard disk. We will later see how you can view and interact with inodes. Basically, for every file there will be an associated inode allocated.

A Linux partition is composed of several elements. In the following section I will try to explain the role of each component:
boot block – the location of the boot loader. We’ve described the Linux bootloader in a previous article.
super block – contains information like number of data blocks, mount-points, File system type, Size, Status, Information about other metadata structures, etc.
inode list – the list of inodes contained by a partition. Note that this is a finite number.
data blocks this is where file content is stored. A file can contain one ore more data blocks and inodes are the ones who store the information about where is each data block located.

In Linux, one file can be seen in multiple locations without multiplying its content. Practically, you can view the same resource in multiple locations but, all will have the same source file. This feature is handled in Linux using links. When talking about links in the UNIX/Linux Systems, we’ll need to specify the two concepts behind this technology:

soft link also known as symbolic link – it’s a redirection from one file to another. They have similar functionality as Windows shortcuts. To create a new symbolic link you’ll need to use the following command:

ln -s existing_file new_file
This command will create a new “shortcut” to the file that already exists in the File System.
hard link – an association between file name and inode. As we’ve talked previously, the name information is not stored in the inode but, instead it’s located in the directory which contains the file. If you want to create a new hard link for an existing file you’ll need to use the following command:
ln existing_file new_file
The command will basically create a new association which will contain another name and the existing inode.
To view the number of hard links for a file you can use the ls -l command (we’ve talked about this in a previous article)

Metacharacters are mainly used for filtering the output of a command. By parsing information before manipulating a command’s result, offers System Administrators a powerful way to manipulate data within the File System. You will see that there are special characters reserved in the OS which have a distinct functionality and can often be used to simplify administrative operations. Regex (regular expressions) are build using metacharacters so, this part may sound familiar. I will try to explain the role of each metacharacter within the OS, please add more if you think are relevant on this topic:

> Used to redirect the output of a command to file. If the file does not exist, it will create a new one, if the file already exists its content will be replaced
>> Redirects the output of a command but, unlike the > character, it will append the new results to an existing file.
< used to redirect input
* wildcard used to substitute 0 or more characters. Example ls -al f* will output any file which begins with f
wildcard used to substitute only one character. Example: ls -al f??e will output any file that starts with “f”, contains any two characters and then ends with “e”

[ ] will substitute any character between brackets. Example: ls -al f[iad]e will output fie fae fde
^ any other character except the ones indicated when used between []. Example: ls -al f[^a-c]e will not output fae, fbe ,fce
; used to separate multiple commands that are written on the same line
Predefined metacharacter class:
[[:alnum:]] – alphanumeric characters
[[:alpha:]]  alphabetic characters
[[:blank:]] – SPACE or TAB keys
[[:digit:]] – numeric characters
[[:upper:]] – upper letters
[[:lower:]] – lower letters
|| OR conditional execution
&& AND conditional execution
# used to specify comments within files
\ if you use this character in front of any other metacharacter, the output will use that particular symbol and not its special functionality. Example: 300\$ = 300$
. filters one character. Example d.g = dog, dag, deg
“$    preceeds the name of a variable to be expanded.

The variables are either assigned using  “=”  or are one of the pre-defined variables (which cannot be assigned to):
$0  name of the shell or the shell script being executed.
$# number of the positional parameters to the command
$1  the value of the first positional parameter passed to the command. $2 is the second positional parameter passed to the command. etc. up to $9.
$*    expands to all positional parameters passed to the command
$@   expands to all positional parameters passed to the command, but individually quoted when “$@” is used. ” from About.com

I’ve found this interesting article which describes regular expressions and metacharacters you can read it to discover more information on this topic. You can also use man bash command to discover more things about metacharacters.
That’s about it for this article folks, please share your thoughts about it and don’t forget to share it to others. Enjoy this wonderful day and stay tuned for the following articles from IT training day.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s