Search

Linux script to create colorful 2013 on the terminal

Here is a script with which you can create the number 2013, on your terminal using any character of your choice. The colors of the numbers will change randomly on every run of hte script.

2013.sh:



Save the script as 2013.sh, give it execute permissions and execute it.



You sholud see some thing like this on the terminal

Implementing an ioctl call (for kernel versions above 2.6.39)

In the post "Creating an ioctl command in linux " we saw the basics of ioctl, and how we can create our own ioctl commands. The functions used to create ioctls have undergone a change from the kernel version 2.6.39,and the previous functions are no longer valid.

Before 2.6.39, the ioctl calls would lock the BKL (Big kernel lock) and then execute the required functions. This was unnecessary with the new fine grained locking available in the kernel. Thus to improve the implementation of ioctl, a new operation called unlocked_ioctl was introduced and all the existing ioctls were migrated to this new implementation.

Thus stating 2.6.39 the older implementation is no longer available.

The changes required in implementation of the ioctl are.

The function in the driver has changed from





The inode is no longer passed as an argument.
The fops uses the name unlocked_ioctl



The basic working of ioctl remains same.
Here is a code that implements the above mentioned changes if the kernel version is more than 2.6.39, else uses the older interface.

Module: ioctl_basic.c



The steps of compiling and testing the module are same as shown in the post "Creating an ioctl command in linux "

colorful Christmas Tree on linux terminal

Here is a version 2 of the Christmas tree creation script. The script will create a colorful Christmas tree on the terminal. The colors of the tree will change on every run.

The script will take as input the character with which the tree has to be drawn, and any two characters that have to be used to decorated the tree.

Christmas_tree:



Save the script as christmas_tree.sh, give it execute permission and execute it.



You should see the following image on your screen.



Related Posts

Christmas Tree on linux terminal

script to find the mac address using linux

Here is a simple script we can use to find the mac address of the network card in our systems.
We will use the command ifconfig to find the mac address and in some distribution ifconfig is available only to the root hence we make use of "sudo" before ifconfig.

For wired connection



For wireless connection


c program in linux to find the maximum length allowed for a host name

The following is a C program to find out the maximum length allowed for a host name in the system We can make use of the function sysconf and pass the argument _SC_HOST_NAME_MAX
We need the header file unistd.h to use sysconf.



Save the program as get_max_hostname.c. Compile and execute the code.




c program in linux to find out the number of processors

The following is a C program to find out the number of processors configured on a system.
We can make use of the function sysconf and pass the argument _SC_NPROCESSORS_CONF.
We need the header file unistd.h to use sysconf.



Save the program as get_processors.c. Compile and execute the code.




C program in linux to find the current working directory

Here are three ways in which we can get the path to the current working directory using a c program,

1. we can make use of the function "getcwd".
Header file required : unistd.h
The syntax of getcwd is



buf: Is the character pointer in which the path of current working directory is stored. size: Is the maximum size of the path in bytes, if the size exceeds this then a NULL is returned

Here is a program that shows the usage of getcwd.



Save the program is get_cwd.c. Compile and execute it.



2.using get_current_dir_name.

Header file required : unistd.h

Syntax:



The function returns the absolute path to the current working directory. The function itself mallocs for an array big enough to hold the current directory name, thus we don't need to do malloc in the program. This function returns the value of the environment variable PWD, thus if it is not set the function might fail to return the current value.
Here is a program which uses get_current_dir_name to get the current working directory.



Save the file as get_current_dir.c. Compile and execute it.



3. The third way of getting the current working directory is using the command "pwd" along with the function "system". The system function executes which ever command is passed to it and prints the result on the screen. Thus we can execute the command system and pass the command "pwd" as the argument, the result of which will be the current working directory being printed on the screen.

The following program shows how the same can be implemented.



Save the file as get_system_pwd.c. Compile and execute the same.




C program in linux to find the username of a the currently logged in user

Here are three ways in which we can get the user name of the currently logged in user using a c program.

1. Using getlogin
Header file required : unistd.h
The synatx of getlogin is



The function returns a pointer to a character array containing the name of the logged in user.

The following is a program showing the usage of the getlogin



Save the file as get_login.c. Compile and execute it.

The output should be the username with which we have currently logged.

2.Using getlogin_r

Header file required : unistd.h

The syntax of getlogin_r is



The function returns the login name in the variable buf, as long as the name does not exceed size parameter passed to the function.
The following is an example showing the usage of getlogin_r



Save the file as get_login_r.c. Compile and execute it.



The function returns the login name in the variable buf, as long as the name does not exceed size parameter passed to the function.

3.Using cuserid

Header file required stdio.h

The syntax of userid is



The function returns the user name in the string which is passed as the argument.The string should have enough space to accommodate the name .

Following is an example showing the usage of the function cuserid.



Save the file as get_cuserid.c. Compile and execute it.


Linux Script to generate random numbers in a given range

Generating random numbers is common recruitment in number of scripts. bash by default gives a variable $RANDOM which when accessed gives a random number between 0 and 32767.



There might be situations where we might need random numbers between a range of smaller numbers or between zero and a smaller number than 32767. Here is a script which can be used to generate random numbers between any range of numbers.

It takes as input the lower limit and the upper limit of the random numbers required and then generates the required number of random numbers with in the range.

random_numbers



save the script as random_numbers.sh,give it execute permissions.

Now execute the script to generate the random numbers



Thus we can see we can generate any number of random numbers with in any given range, using the script.

Deadlock creation and avoidence in pthreads

In the post we saw how we can use mutex to prevent simultaneous usage of the same resource by multiple threads. But usage of mutexes have to be done very carefully to make sure that the threads don't end up in a dead lock.

A deadlock is a scenario when one thread is waiting for a resource which is held by the second thread, while the second thread is waiting for a resource held by the first thread. Thus causing both the threads to wait for each other infinitely.

To show how this situation might occur, in the following example we have used two mutexes,read_mutex and write_mutex. To gain access to the file both these mutexes have to be locked. In the code, the write thread locks write_mutex and then after a little delay tries to locks read_mutex. The read thread on the other hand locks read_mutex first and then tries to lock write_mutex.

Thus by the time the write tries to access read_mutex, it has already been locked by read thread and by the time read thread tries to lock write_mutex, it has already been locked by the write thread. Thus both the threads end up waiting infinitely for each other to unlock the mutexes and process ends up in a deadlock.



Save the file as mutex_deadlock.c, and compile it by passing "-lpthread" flag.



There will be no output as both the threads endup in a deadlock state, to get out of the execution use "cntrl + c".

To avoid such simple deadlocks the easiest method is to make sure that all the threads access the locks in a specific order. In the above example if it is made compulsory that every thread should lock write_mutex first and then try to lock read_mutex, then the deadlock situation will not occur as only one thread will get access to write_mutex first and the same thread will be able to access read_mutex too.

The above code modified to avoid deadlock is shown below. Note that the order of acquiring lock by both the threads is the same, thus able to prevent deadlock.



Save the code as mutex_nodeadlock.c, compile it using "-lpthread" flag.



Thus we can successfully avoid a potential deadlock situation, by making use of the rules order of locks.

Using pthread_mutex to lock and synchronize threads

pthreads are used to split a process into multiple threads and execute them concurrently. The threads should be independent of each other as far as possible, but at times the threads could be dependent on each other and they might be working on the same data.

Let us say we want to crate two threads,one to read from a file and the other to write into the same file. It is obvious that we can not read from a file unless we don't write into it, and at any given time the file should be allowed to be accessed by only one of the two threads. That is while it is being written it should not be allowed to be read and while it is being read, it should not be opened for write.

The following program implements the above mentioned two threads, but does not do any thing to synchronize the actions of the two threads.



Save the adove file as pthread_file.c. Compile it using "-lpthread" falg



We get a segmentation fault every time we execute the above program. This is because, the read thread tries to access the file which is being created by thread1 "write" and thus is still not available. When read thread accesses the file it still has not been created and thus we get a segmentation fault.

The part of the program in which multiple threads try to get access to the same resource, in this case the file, is termed as the critical section and ideally a critical section should be executed by only one thread at a any given time.

To prevent the access of the file by multiple threads at the same time we can make use of a mutex. Mutex stands for mutual exclution and as the name implies it keeps the threads mutually exclusive.

Access to the critical section is controlled by locking the mutex. That is whichever process locks the mutex first will be allowed to enter the critical section first and the other process can not enter the critical section unless the mutex is not unlocked by the first process.

Applying this to above example, both read and write threads will have to get a lock on a common mutex before getting access to the file. If we can make sure that the write thread always gets the access to the lock first then, the read thread will have to wait for the write thread to finish its operations on the file and unlock the mutex,before it can read it. Thus making sure that the segmentation fault never ocurrs.

To implement a mutex in pthreads we have to use a datatype of type pthread_mutex_t.
e.g.



To lock the mutex we use the function pthread_mutex_lock(Address of mutex variable)
e.g.



To unlock the mutex we use the functions pthread_mutex_unlock(address of mutex variable)
e.g.


The implementation is shown in the code below.

pthread_mutex.c



save the code as pthread_mutex.c and compile by passing the "-lpthread" flag and execute it.




Using pthread_self to find the thread id

Every thread created using pthread_create gets assigned a uniue thread id, with which it is recognized.

Note: Refer to creation of threads using pthreads in the post " "

The parent gets to know the thread id after the pthread_create is executed sucessfully, but while executing the thread if we want to access the thread id we have to use the function pthread_self.

The function pthread_self does not take any arguments, and returns the thread id, which is of kind pthread_t.

In the following program we are creating a thread which executes the function "hello". In the function "hello" we have used pthread_self to find out the thread id and then printed the same. Note that pthread_t is an unsigned long data type and hence we have to use %u while printing using printf.



Save the file as self_pthread.c

Compile it using gcc by passing the "-lpthread" flag.



We can see that the thread_id printed by the main function, parent, and the thread are the same, indicating that the value returned by pthread_self is the correct value.


pthread error: undefined reference to `pthread_create'





The above error is a common error encountered by a number of people who begin writing programs using pthreads. The POSIX libraries required for pthread functions are not linked by the compiler unless we don't ask or it to be linked. Thus we see the above error.

The simple workaround is to pass the option "-lp" to the compiler while compiling the code.
Example:



Now we should not get the above error.

Creating a christmas tree on linux terminal

Here is a script that,when executed, will draw a christmas tree using the characters choosen by the user on the linux terminal.

The script will prompt the user to enter the character using which the tree has to be drawn. Then it will prompt the user to enter two characters that will be used to decorate the tree. The last input it will ask for is the character using which the base of the tree has to be drawn.

chritstmas_tree.sh



Save the script as chritstmas_tree.sh and give it execute permissions



You should see the following on your screen.



Related Posts

Colorful christmas tree on linux terminal

Creating an outline of triangle shape on linux terminal, using any characeter

This post is in response to the comment #2 in the post Creating a triangle of characters on terminal

The following script will create a blank triangle of characters on the linux terminal, but unlike the script in "" the triangle will be empty, i.e. only the outline has the characters chosen by the user and inside is left blank.



Save the script as blank_triangle.sh and give it execute permissions



Now execute it



Thus we can see that the script is able to draw the blank triangle of any character user wants and any number of rows (limited by the number of rows in one terminal screen )


Linux script to find prime numbers in a range of numbers

In the post " " we saw how we can use the command factor to find whether a number is prime of not. We can extend the same script further to generate all the prime numbers in a given range of numbers.

To generate a range of numbers we can use the command "seq" . The usage of seq is shown in the post " ".

We can use seq in a for loop to iterate over one number at time.



Save the script as seq_for.sh, give it execute permissions and run the script.



Thus we can see that using seq along with for loop we can work on a range of numbers, one number at a time.

This script can be combined with the script in " " to generate all the prime numbers in a range of numbers.

We request the user to enter two numbers to indicate the lower and upper limits of the range of numbers between which the user wants to generate the prime numbers. The lower limit can not be 1 because the number 1 does not have any factors and the command "factor" does not return any thing for 1. Thus we need to make sure the user enters a number bigger than 1. If the user enters 1, then we will prompt the user again to enter number greater than 1.



Then we pass these two numbers to seq in a for loop



In each iteration of the for loop we use the factor command to find out if a number is prime or not.



The full script looks as below.



Save the script as range_prime.sh,give it execute permissions and run it.



Thus we can see the script is able to generate a list of all the prime numbers between 2 and 20.

Linux Script to find prime factors using the factor command

The command "factor" takes as input any integer number and returns the prime factors of the that number.

Example :



We can use this command to find wheter a given number is prime or not. If we pass a prime number as argument to the command factor, it returns the number itself as its prime factor. As shown above for the number 131.
We can pipe this output to grep,search for the number passed to factor in the output. If the output is comprised only of the input number then the given number is prime.
The following script does the same


Save the above script as prime.sh, and give it execute permissions



Now execute the script and see the output


makefiles: Complex conditional execution

While doing the conditional execution in makefiles, using only if and ifneq allows us to check for one condition but to be able to check multiple conditions one after another, we can use either if or ifneq after the else to check for further conditions.
Syntax:



If the ifneq condition fails then the flow will go to the else and check the condition specified after the else. We can use as many else statements as we require.
Let us say we have three "c" programs, hello_1.c, hello_2.c and hello_3.c. We need to compile them depending on which user is running the make command. hello_1.c should be compiled if we are running make as root, hello_2.c if we are running make as "user1" and for any other user hello_3.c. This can be achieved by the following makefile.



In the above makefile, first variable "user" is compared with "root" then with "user1". If both the conditions turn out to be false then the statements after the final else gets executed, which sets the targets to hello_3.c as required.

If we run the above makefile as root then we will get the output



If we run it as "user1" we will get the output



If we run it as any other user we will get the output


Conditional execution using ifneq

Conditional execution can also be done using ifneq which is the opposite of ifeq.

The syntax of ifneq is



ifneq(,) returns true when and are not equal. If and are equal it jumps to the else statement.

Using the above logic, the example used in the post "Conditional execution using ifeq" can be re written using ifneq as below.



Thus ifneq checks if the current user is not root, which if true then sets the target to $(pf)_1, else it sets it to $(pf)_2.

If we execute the make command as root we will get the following output



If we do the same as any other user other than root, we will get the following output




Makefies : Conditional execution using ifeq

Conditions can be added in makefiles using "if" conditions, to ensure the commands get executed only under certain conditions.

The syntax of if in a makefile is



Let us say we have two files hello1.c and hello2.c which we want to compile using makefile. But the condition for compiling these files is that if the user who is compiling is root then hello_1.c should be compiled,else for any other user hello_2.c should be compiled. We can use the if condition in makefile to achieve this.

This can be done using the following makefile





Above statement assigns the current user name to the variable "user" using the command "whoami".

Then we use the "if" condition and check the user name and assign the target based on the result of the condition check.

If we execute the make command as root we will get the following output



If we do the same as any other user other than root, we will get the following output




seq : To generate a sequence of numbers

seq can be used to generate a sequence of numbers separated by any kind of separator as required.



By default seq generates the range of numbers starting from 1. We can generate the list from any starting number by mentioning the stating number also as an argument.



By default the numbers are generated separated by new lines. We can change the separator by using the option -s



We can make the number to be of equal length using the option -w



The increment by default is by one number,this can be changed by passing the increment number as the second argument.



The number 2 as the second argument indicates that the increment is to be by two numbers.

We can also generate a reverse seq of numbers




Makefiles: Using variables

Prev: make is smart We can use variables in makefile to prevent typing the same strings over and over and also make the file more easy to understand.

Defining variables: Let us say we have a makefile to compile three independent executables hello_1, hello_2 and hello_3



In the above makefile we use the string "cc -o" repeatedly. Instead of typing the whole string every time we can create a variable for it.
We can create a variable of any name by using the "=" sign. The left hand would be the name of the variable and the right hand side would be the value of the variable.
Any where in the file if we want to use the value of the variable we just have to use the name of the variable in the format $() and make will automatically replace the variable with its value while executing.



In the above file have created a variable CC and used it whenever we needed cc -o.
Note that variables are case sensitive thus "cc" "CC" and "Cc" are all different.
We can also use variables for prefixes of various strings. Like in the example above "hello" is like a prefix to a number of strings, thus we can use a variable for it.



Recursive expanded variables : We can create recursive variables by using one variable while declaring another.



In the above example "targets" is a variable, which is a list of all the targets. The targets variable uses the variable "pf" , thus to find out what $(targets) refers to make will have to expand pf creating a recursion among variables any depth of such recursion is allowed.
The limitation of defining variables using "=" is that a variable can not refer to itself in recursion.



In the above example, the second definition of variable "targets" uses the "targets" itself in recursion. Such usage will result in an infinite loop, which make successfully detects and throws the error.



Simply expanded variables This can be prevented by making use of simply expanded variables which are defined using ":=". The use of ":=" makes the expansion of all the variables in the assignment only once and stop, thus preventing the infinite loop.

Thus in the above example the second definition should be



Now make will not throw any error and will successfully compile the targets.

Using shell in declaration : We can also execute shell commands while defining variables.



In the above example we executed the command "pwd" and assigned the result to the variable "opd" which we used while compiling to indicate the path where the compiled executable needs to be stored.


Makefiles:make is smart

Prev: Multiple independent targets

Till now to compile a c program we have passed the command to the makefile to compile a given c code. But make has built in intelligence and can work even without the command.

Let us say we have ta file

hello.c



To compile this file using make we just need to create the makefile with the contents

makefile



make looks at the targets and if no dependencies and commands are mentioned in the makefile,it automatically looks for a file with the name .c i.e. in the above example hello.c and compiles it to create the executable.



If we want to make sure that the correct files are being compiled before actually generating the executable we can just print the commands that make is going to run by passing the option "--just-print".



In the above case the executable will not get generated, but it only prints the commands that will be executed.

We can also print the folders in which make is running the commands from using the option -w



Next: Makefiles: Using variables

Makefiles: Multiple independent targets

Prev: Multiple dependent targets

The make command when executed with out any arguments always tries to create the first target. But in one makefile we can specify as many independent targets as we want and then choose to create the specific targets .

Let us say we have two programs

hello_1.c



hello_2.c



The above two are independent programs which can be compiled on the command line using



The same can be done by creating a makefile with two targets "hello_1" and "hello_2".

makefile:



Now if we run the command make



We can see that if use the command make with no arguments the first target i.e. hello_1 gets crated. Now if we want to create the second target we will have to pass the second target to the make command as an argument.



Thus we can specify any specific target we want to create as the argument to the command make.

If we want to option of creating all the independent targets with out specifying every target separately we add a target to the makefile and mention all the targets as the dependencies.

makefile:



Now we can create hello_1 and hello_2 by running make with all as the target.



Thus we can use makefile with multiple targets and selectively create the required targets or create all of them together.

Next: Makefiles: make is smart

Makefiles: Dependent targets

Prev: Makefiles : Introduction The makefiles can have as many targets as we want, and they could be dependent on another or independent.

Let us say we have the following codes
hello_main.c



print_hello.c



The first program is calling a function declared in the second program. To compile these codes we can run the command



This can be broken down into the following steps
Creating the object code



This will generate the object files "hello_main.o" and "print_hello.o"

Now we can club the object codes into one executable



This will generate the final executable hello.

For creating the executable hello, hello_main.o and print_hello.o are the prerequisites.
Thus the makefile entry for target hello is



For creating the object codes, hello_main.o and print_hello.o are the targets and hello_main.c and print_hello.c are the prerequisites

Thus the make file entries for the two object codes is



Combining the above two we can create the full makefile

makefile:



Note the order in which the entries have been added. The final target is the first entry, the prerequisites for the final target are created by making them as targets in the subsequent entries.

Now run the command make



We can see that make starts compiling from the first independent target hello_main.o then it compiles print_hello.o then it create the final executable using the two object files.

Now open the program print_hello.c and modify the print statement



Save the program and run the command make again.



We can see that make compiled only the modified program and did not compile hello_main.c which was not modified after the executable "hello" was created.

The above split was shown just show the concept of dependent targets. We can compile the same code using the shorter makefile

makefie:



If you misspell any of the prerequisite names then make will throw an error and fail. For eg. if we create the above makefile with the file name hell_print.c



On running make we will get the error



The above error indicates that the problem is with the file name "hell_print.c" as the makefile is unable to find a file with that name. Next: Makefiles :Multiple Independent targets

Makefiles: Introduction

Makefiles: Introduction Makefiles are the files used by the utility make. make helps in compiling a source code into an executable or automate execution of commands listed in the makefile.

Let us say we have a file "hello.c"

hello.c



The usual compilation of this in linux using gcc is



We can use a makefile to do the same compilation
The syntax of writing a makefile is



A makefile is comprised of

Target : The executable which has to be generated. In the above case the target is the executable hello.

Prerequisite: The source files required to generated the target. In the above example the source is hello.c

Command: If the prerequisite is available which command will convert it into the target ? The command in the above example is "cc -o hello.c -o hello". The command always has to be preceded by a single tab space

Create a file by the name "Makefile" and enter the contents

Makefile



Now open a terminal and cd to the folder where the makefile is stored.
Run the command



We can see that make has run the command and generated the executable just as we did from the command line.
You might get the following error if you have omitted the tab space before the command.



If you name the makefile wrongly You might get the error



The makefile can also be named with any other name we want but in that case we need to use the option "-f" with the command make. If we want to name the above makefile as my_makefile then we need to use the command make as

After the makefile has generated the executable if we run the command make again we will get the message



This is a special property of the make utility. It will generated a new executable of the mentioned target only if the target does not exist or the source file has been modified after the target has been generated. Next : Makefiles: Dependent targets

Extracting pages from a pdf file using linux command line

pdftk is a tool which we can use to split or extract pages from a pdf document.

To install pdfktk on debian based systems



Let us say we have a pdf file,temp.pdf, with 100 pages in it. We want to extranct the pages 20 to 30 and create a new pdf . We can do it using pdftk



If the command returns to the prompt with no errors, we will have a new pdf file in the same directory with the name new.pdf.

We can pick specific pages instead of a range by just passing the page numbers.

To create a new pdf by extracting pages 21,25, and 28 we can use.



We can also select only even/odd pages by passing the option even/odd respectively

To extract all the odd numbered pages we can use the range as 1-end and use the keyword odd.




Truncate:Reduce file size from command line in linux

We can change the size of a file from the command line using the command truncate.

For e.g.



We can change the size of temp1.txt to any size we need.



But be careful as the truncation simply removes the content of the file to reduce its size.

Running .dat files in linux and using vlc

.dat files which are generally vedio files are played easily in windows media player, but in linux not all players support .dat files.

We can use one of the following methods to run a .dat file

1. using vlc
vlc by default throws error when we try to run a .dat file.



To run a .dat in a vedio cd file using vlc do the following



The file in the cd should start playing automatically.

2. We can use the gxine player to run .dat file



Once the installation is done a new player called "gxine" will appear under sound and video menu which will directly play the .dat file with out any problems.

3. We can install windows media player using wine and run the .dat file from the windows media player.


du: Finding the file/folder size from command line in linux

To find the amount of space occupied by any file or folder in the system we can use the command du.

Let us say we have a folder test which contains the files file1,file2,file3,temp

To find the size of the folder test



The first column Indicates the size of the folder and the second column gives the name of the folder. By default du also lists the sizes of only the sub folders and not of files.
In case we want to have a look at the size of the files too we can use the option "-a"



The size column if displayed with no suffix can be difficult to read for bigger sized files, to make the reading easier we can use the option "-h" which sill convert it into more human readable form



The suffix K indicating the size is in Kilo bytes. For Mega bytes it will be M for giga G etc.
If we just want the size of the folder and not its sub folders we can use the option "-s"



We can also pass file names to find the size of a file instead of a folder.



Thus we can easily find the space occupied by any file or folder on the system using the du command

Different ways to find the linux kernel version

Here are two ways to find out the currently running kernel version of linux

1. Using uname to find the linux kernel version



2. Using sysctl to find the linux kernel version



3. Using proc entry to find the linux kernel version


No manual entry for pthread_create

While learning pthreads, if you try to have look at the man pages of pthread and get the error



Then you need to install the package manpages-posix-dev. If you are running debian based systems you can run the command.



Once the package is installed you should be able see the man pages for all posix related functions.

Finding the filesystem type of a partition in linux

To find the type of filesystem a partition or a media is of we can use the commands "df" or "mount".

Using df



The column with the heading "Type" gives the filesystem type. So the partition /dev/sda1 is of type ext4

To find the filesystem type of a specific partition pass the partition as the argument.



Using mount



As listed the partition /dev/sda1 is of type ext4.
We can find the specific partition type by combining mount with grep.




Finding the current runlevel of linux

To find the current runlevel of the system we can use the following commands





The output indicates that the current runlevel is "2" , followed by the date and the time. If there was any other runlevel used before it would be listed after "last=".
The second command that can be used is



The second number is the current runlevel. The first number is the previous runlevel.

readlink: Finding file to which a softlink pointing to

readlink is a command that can be used on a softlink to find out the file to which it is actualy pointing to. For example if we have a file "temp" which is a softlink, we can run readlink of temp as follows



This indicates that readlink is a softlink of the file temp. If we want the full path to the file "test" we can pass the option "-f"



Thus we can find the full path to the file to which the softlink is pointing to .

Creating a soft link.

symbolic links are used in linux to create a link to a folder or a file which is located in a different location, so that the same file or folder can be accessed from both the locations.
Softlinks are created using the command



Example:

If we have a file "temp1" in the path



And if we want to create a link to this file in the path



We can do it as follows.



Now if we do a long listing of the new link that was created we can see that it is a soflink by the way it gets listed



The first letter, l, in the long listing indicates that the file is a softlink and the last column indicates the file to which it is a softlink

The new link file is just a pointer to the original file and not a new copy of it and hence it occupies just enough space required for a link, which is very very small, rather than for the whole file.
The file can be opened and modified from either of the locations, and the changes will be reflected in files at both the locations.

Removing grub rescue entries from grub menu

The grub2 by default creates an entry for recovery mode in its menu,using which we can log into single user mode and trouble shoot various problems in the system.

But if we do not want this entry we can remove this entry from the grub by changing the grub configuration.

Open the file



Note : Super user privileges are required to modify the file

Edit the line



Uncomment the line by removing the "#" in front of the line.

Now update the grub by running



Next time the system is rebooted the grub menu will not have the recovery entries.

Making the grub beep on displaying the menu

In grub2, when the menu is displayed it by default does not make any sound to alert the user about the display.
This behavior can be changed so that the grub creates a beep sound as soon as the grub menu gets displayed, so that the user, if he or she is not looking at the monitor, gets notified about the display.
Open the file



and go to the line



Uncomment the the line by removing the "#" in the beginning.

Save and quit the file.
Now run the command



Now reboot the system and we should be able to hear a beep when the grub menu gets displayed.

Pointer to structure from its member pointer: container_of

In c programming, we use structures to store variables of different data types in one kind of variable. Each variable in the structure is termed as a member of the structure. The memory allocation for these members is always sequential.
Example



In the above structure the addresses allocated for members "i" and "ch" will be sequential in the memory.

Using this property of the structure, if we know the pointer to a member of the structure we can retrieve the pointer to the structure with the help offsetof function.
In the post "offset of" we saw how we can retrieve the offset of a member with in the structure.

As the size occupied by each data type is fixed once we know the offset of the member and its pointer, we can subtract the offset from the pointer to get a pointer to the structure.

For example in the above structure if the pointer to the "char ch" is 1000. The offset of function will return 4 for "ch" as it is after an integer in the structure.
Thus the pointer to the structure would be "1000-4 = 996".

The same is shown in the example code below.

We allocate memory for one structure,test1, of type "temp" and assign values to "i" and "ch".



Then we assign the pointer of ch to a char pointer.

Then we subtract the offset of "ch" from the pointer and assign the return value to a new pointer to a structure,test2, of type temp.



If the pointer assigned to test2 is the correct , we should be able to access the value of "i" using test2.
Thus we print the value of "i" using test2



Here is the full code

struct_ptr.c



Compile and execute it.



Thus we can see that we were able to retrieve the pointer to the parent structure from the pointer to the member.

The same steps are used in the container_of function which is used in the linux kernel code.



By passing pointer to the member of the structure in ptr and the structure type as argument type and the name of the member of which ptr is the pointer to we can retrieve the pointer to the parent structure just as we did in the example above.

Example usage in kernel code :

File leds.c



By passing a pointer to the cdev structure,the structure type as "versatile_led" and the member of the structure as cdev we are able to get a pointer to the structure that contains the cdev and hence get access to other members of the structure.


unlink: Command to delete files

The command unlink can be used to delete files.

Example : To delete a file called temp



But unlink lacks any of the other options that come with the command "rm" which is also meant to delete files and unlink can only delete file and does not work on folders.

offsetof: Using the offsetof funcion in c

The "offsetof" function in c is useful in determining the offset of members with in a structure or union.
It is defined in the header file "stddef.h".
The syntax is



Arguments:



Example :

Let say we have a structure foo



The structure "foo" has a integer member which generally would of 4 bytes on a 32 bit system and a chat member which would be one byte.
Thus the offset of the member "ch" in the structure "foo" would be the number of bytes occupied by "int" which is "4".

Here is a example code which uses "offsetof" to find the offset of ch in the structre foo.



Compile the code and execute it



Try changing adding new members to the structure foo and see how the result changes depending on the members added.

Try with the structure :





The offset is the size of the array which is 4 X 10 = 40.

Answers to linux history quiz

1.In which year was the linux kernel released under GNU GPL license for the first time?
Ans.1992

2.The penguin was chosen as the official mascot for linux in
Ans.1996

3. The linux foundation is the merger of
Ans. OSDL and Free standards group

4. Which famous computer scientist said this in 1992 "Linux is obsolete"?
Ans. Andrew S. Tanenbaum

5. Linux 1.0 was released in
Ans. 1994

6. When did microsoft first contribute to linux source code ?
Ans. 2009

7. Which of the following is a linux based company and was started in 1994 ?
Ans. Redhat

8. Which of the following companies invested a billion dollars in to linux in the year 2001 ?
Ans. IBM

9. In which year did Linus Torvalds appear on the cover of Business week ?
Ans. 2005

10. Which of the following Linux Desktop environment (GUI) was started in the year 1997 ?
Ans. GNOME

Linux history quiz

Quiz On history of Linux

Quiz On history of Linux

A simple quiz on history of Linux
  1. In which year was the linux kernel released under GNU GPL license for the first time?

  2. 1991
    1992
    1993
    1996

  3. The penguin was chosen as the official mascot for linux in

  4. 1992
    2000
    1994
    1996

  5. "The linux foundation" is the merger of

  6. OSDL and Free standards group
    GNU and OSDL
    The linux users and unix users group
    Many small linux supporting groups

  7. Which famous computer scientist said this in 1992 "Linux is obsolete" ?

  8. Andrew S. Tanenbaum
    Alan Cox
    Ken Thompson
    Richard Stallman

  9. Linux 1.0 was released in

  10. 1999
    1994
    1996
    1992

  11. When did microsoft first contribute to linux source code ?

  12. 2002
    1995
    2009
    2011

  13. Which of the following is a linux based company and was started in 1994 ?

  14. Ubuntu
    Redhat
    IBM
    Apple

  15. Which of the following companies invested a billion dollars in to linux in the year 2001 ?

  16. Redhat
    Canonical
    IBM
    Google

  17. In which year did Linus Torvalds appear on the cover of Business week ?

  18. 2000
    2003
    2005
    1999

  19. Which of the following Linux Desktop environment (GUI) was started in the year 1997 ?

  20. GNOME
    KDE
    Unity
    Enlightment

Answers to the uiz are available at : Answers to linux history quiz

expand: To convert a tab space to space characters

expand: Command used to convert the tab space to separate space characters.

Example:

Let us say we we have a file "temp" with the following contents.

temp:



In each line we have one tab space, which is equivalent to 8 space characters. To convert this one tab space to 8 space characters we can run the command expand on the file.



The new file "temp_1" looks the same as the file "temp" but if we open the file to edit it, we will encounter 8 space characters instead of tab on every line.

To change the number of space characters from 8 to any number of our choice we can use the option "-t".



We can see that the tab has been replaced by single space. The number that you specify after "-t" will be the number of spaces that will put instead of the tab space.

We can also restrict the command to work only at the beginning of a file by passing the option "-i"

Example :

Let us modify the file temp and add a tab at the beginning of the first line.

temp:



Now use expand on the file along with option "-i"



We can see that only the tab at the beginning of the first line has been removed,but the rest of the tabs are intact.

tr: To translate characters

The command "tr" can be used to translate/change or delete characters from the input.

By default tr takes two arguments, the first one being the set of characters to be searched for and the second argument being the set of characters to be replaced with . The default input is the standard input.

Example:



The command waits for user to enter a string and then hit the return key. In the above example on entering "xyx" the character x has been replaced by character y which was what was passed as arguments to the command.

We can also pass multiple characters as arguments and each characters will be considered separately and not as a string and each of the characters in argument1 will be replaced by the corresponding character in the argument2

Example:



If the string length of argument1 is greater than that of argument2 then the last character of argument2 is applied to all the characters of argument1 that are after the string length of argument2.

Example :



As we can see both y and z are replaced by b.

We can change this behavior by passing the option "-t" which will truncate the characters of first argument to the same as the length of argument2.



Thus we can see, "y" and "z" both are not replaced by character.

The command can be applied on files by using the input redirection.

Example:

Let say we have a file,temp, with following contents

temp:





We can see that all o have been replaced with O.

Instead of translating, we can delete characters too by passing the option "-d" .

Example:



Thus we can see that all the digits in the file have been deleted.

Other examples:

We can user "tr" to convert all the characters in a file to upper case too.



Using tr over a range of characters



Deleting repetitive characters




Follow by Email