Home –  Archive
Monthly Archives: Jan 2012

Difference between Unix and Linux

difference between Unix and Linux

difference between Unix and Linux

When it comes to operating systems, Unix is the mother of operating systems. Members of rich Unix family are:

  • SVR4 (by AT&T)
  • BSD 4.4 (by University of California)
  • HP-UX (Hewlett-Packard)
  • Solaris (Sun Microsystems)

Mostly, Linux is considered to be a copy of UNIX. Lets hear the actual story now. Linux was actually a late addition to the family. It was written by Linus Torvalds back in 1991 and it was meant for IBM computers. As a matter of fact, in the world of operating systems, Linux has come up as a great operating system and it is welcomed with huge popularity. Commercial enterprise servers are running on Linux. Another cherry on the cake, Laptop and PC companies are also providing GNU Linux as a pre-installed OS on their systems so that individual users can also get a bite of it.

An extensive brain storming confusion is if Linux a kernel or Operating System. After some time, people just start ignoring this doubt as it never gets answered, well keep reading for the solution. What Linus Torvalds wrote was Linux kernel and it had a lot of features similar to UNIX one. Why it is confused with OS, actually commercially available distributions that provide graphical interface, compilers and other utilities along with Linux kernel are referred to as Linux Operating System.

Linux is, as they say, a UNIX-like kernel, because it has ‘some’ common features but still there are areas where they are not same. Difference between Unix and Linux can be understood by going through following points.

1. The difference in approach: There is a class of kernels that fall in Monolithic category, Monolithic ones are those which operate in one and only one process, they don’t have any other process for any kind of task. Another category is called Micro-kernel where core of the kernel is assigned one process while other processes are there for its side tasks like drivers, etc. Linux lies within Monolithic category with few exceptions in Micro-kernel.

2. Loadable kernel Modules: Unix systems are bound to have static links of new modules to be added or recently added. Linux differs here too, it supports new additions on the fly. For example, drivers can be loaded dynamically whenever they are needed. This feature is recognized as Loadable kernel Modules (LKM). This feature enables us to add any component dynamically without arising the need of compiling the whole kernel again. Unloading can also be performed in similar fashion. This adds to the flexibility of Linux.

3. Kernel threads: Kernel thread is an independent execution flow. It can be used to execute some user process or any kernel code. Most of the UNIX-like systems constitute kernel thread sets. Threads always operate in same address space, so it is not expensive to perform context switching on kernel threads in comparison to processes- this explains why Unix-like systems have kernel threads. Kernel threads are used, on linux, to execute kernel code.

4. Multi-threading: Multi-threaded applications are those which create multiple execution flows. These flows are also known as threads and are light weight processes. Nowadays all operating systems have support for multi-threading. Most of the UNIX-like operating systems LWP (read light weight process) are kernel thread based, while on the other hand Linux handles them a bit differently. In linux, LWP are created by calling the clone() function which leads to creation of separate processes. Same task can also be carried out with fork() function but clone() lets recently created processes share their memory, address space etc. Their working in shared environment gave them a name “threads”. Hence, multi-threading is supported by both of them but they differ in internal handling of it.

5. Preemption and non-preemption: We have a category of kernels which are able to pre-empt currently executing process and hence we call the preemptive kernels. Processes are run on a priority basis. If currently executing process is a low-priority one and a high-priority one process comes up, it can interrupt the current process and start executing itself. Non-preemptive kernels are those which don’t forcibly interrupt the current process even if a high-priority process has to wait. Linux based operating systems are non-preemptive while UNIX systems are fully preemptive. Linux based Realtime operating systems are found to be fully-preemptive.

Bottom line, despite taking the basic idea from UNIX it still differs in some aspects because it has inherited a lot from it and will always be considered a part of Unix family.

Linux Command Line tips that every Linux user should know

Below is the collection of Linux command line tips which I’ve found useful for Linux users. To get more information about the command mentioned below just open your terminal and type man <command>.

Things a Linux user must learn

  • Learn bash: No need to refer a lengthy bash guide or something else. Just read the complete man page of bash (man bash).
  • Learn vim: You might be using Emacs or Eclipse for your work all the time but nothing can compete vim.
  • Learn ssh: Learn the basics of passwordless authentication.
  • Learn basics of bash job management: Using &, Ctrl-C, fg, bg, Ctrl-Z, jobs, kill.
  • Learn basic commands for file management: ls and ls -l, less, head, tail and tail -f, ln and ln -s (learn the differences between hard links and soft links), chown, mount, chmod, df, du (du -sk *).
  • Learn basic commands for network management: dig, ifconfig.
  • Learn how to use grep, find and sed.
  • Learn how to use aptitude or yum (depends on the distro) to find and install packages.

For daily use

  • In bash, you may use Ctrl+R to search in command history.
  • In bash, you may use Ctrl+W to delete the last word, and Ctrl+U to delete the complete line.
  • Use cd – command to go back to the previous working directory.
  • Learn how to use xargs.

$ find . -name \*.py | xargs grep some_function

$ cat hosts | xargs -I{} ssh root@{} hostnameX

  • Use pstree -p command to get see the process tree.
  • Learn various signals. For example, to suspend a process, use kill -STOP [pid]. Type man 7 signal in terminal for complete guide.
  • If you want to keep running a background process forever then you can use nohup or disown.
  • Use netstat -lntp command to see what the processes are listening. You should check about lsof also.
  • In your bash script you can use subshells to group commands.

# do something in current dir

(cd /some/other/dir; other-command)

# continue in original dir

  • Trimming of strings: ${var%suffix} and ${var#prefix}. For example if var=foo.pdf, then echo ${var%.pdf}.txt prints “foo.txt”.
  • The output of a command can be treated like a file via <(some command). For example, compare local /etc/hosts with a remote one: diff /etc/hosts <(ssh somehost cat /etc/hosts)
  • Know about “here documents” in bash.
  • Learn how to redirect both standard output and standard error via: some-command >logfile 2>&1.
  • You should know about ASCII table (with hex and decimal values). Type man ascii in terminal.
  • While working remotely via ssh, you should use screen or dtach to save your session.
  • For web deveopers use of curl and curl -I, wget etc is useful.
  • To convert HTML page to text file: lynx -dump -stdin
  • If you must handle XML, xmlstarlet is good.
  • In ssh, learn how to port tunnel with -L or -D (and occasionally -R). Also learn how to access web sites from a remote server.
  • If you were typing a command but then changed your mind, Press Alt+shift+3. It will add # at the beginning and enter it as a comment.

Data processing

  • Learn about sort and uniq.
  • Learn about cut, paste, and join.
  • Learn how to get union, intersection and difference of text files.

cat a b | sort | uniq > c # c is a union b

cat a b | sort | uniq -d > c # c is a intersect b

cat a b b | sort | uniq -u > c # c is set difference a – b

  • Summing all numbers in the second column of a text file, code given below is probably 3X faster and 3X shorter than equivalent Python.

awk ‘{ x += $2 } END { print x }’

  • Learn about strings and grep command.
  • To split files into different parts learn about split (to split by size) and csplit (to split by a pattern).

System debugging

  • To know the status of your disk, cpu or network use iostat, netstat, top (or the better htop), and (especially) dstat.
  • To know your system’s memory status use free and vmstat command.
  • Use mtr which is a network diagnostic tool.
  • To find out which process or socket is using bandwidth, try iftop or nethogs.
  • You may use ab tool which is helpful for quick checking of web server performance.
  • For more serious network debugging take use of wireshark or tshark.
  • Learn how to use strace, and that you can strace a running process (with -p). This is helpful if your program is failing, hanging, or crashing, and you don’t know why.
  • Use the ldd command to check shared libraries.
  • Learn how to connect to a running process with gdb and get its stack traces.
  • Knowledge of /proc is very helpful. Examples: /proc/cpuinfo, /proc/xxx/smaps, /proc/xxx/exe, /proc/xxx/cwd, /proc/xxx/fd/.
  • When debugging why something went wrong in the past? To know about this use the sar command. It collects, reports and saves system activity information.

PS: I think I have missed some tips because they didn’t come in my mind at the moment. If you know some good command line tips then please share them in the comment. Thank you :)

Edit: I found some useful tips from reddit users which they gave after reading this post.

1. ifconfig is deprecated, alternative of it is ip.
2. Use of aliases is also an important thing which I forgot to mention.

Cheat Sheet

Download or bookmark the cheat sheet given below. It is very useful.

linux command line cheat sheet

Noticed “rm -rf / – make computer faster” under file commands in the cheat sheet? Don’t ever do that, that will delete all of your files. Do it on your enemy’s system :P

How to read a CSV file in Perl?

read csv file perl

read csv file perl

Perl is used for manipulating text files and in this article I will teach how to read a CSV file in Perl? First of all let’s start with

What is a CSV file?

A comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form. As a result, such a file is easily human-readable (e.g., in a text editor).

CSV is a simple file format that is widely supported by consumer, business, and scientific applications. Among its most common uses is to move tabular data between programs that naturally operate on a more efficient or complete proprietary format. For example: a CSV file might be used to transfer information from a database program to a spreadsheet. -Wikipedia

Using Perl to read a CSV file

Now suppose we have a CSV file which has 4 rows and each row has fields which are separated by comma as given below:

Linux,Unix,1,Ubuntu
RHEL,Kubuntu,2,Fedora
Mint,Puppy,3,ArchLinux
Mandriva,OpenSUSE,4,CentOS

If you want to extract out the data from 3rd column and calculate the sum of all the numbers then the approach to do this will be as follows:

Read the first line of the CSV file and extract out the 3rd column. Assign the extracted value to a scalar variable and then read the next line, extract out the value of 3rd column, add it to the scalar variable which contains the first value and store result in the same variable. Then read the 3rd line and so on…

Below is the Perl code for this purpose using split function.

  #!/usr/bin/perl
  use strict;
  use warnings;
 
  my $filename = $ARGV[0];
  my $total = 0;
 
  open(my $FH, '<', $filename) or die "Couldn't open $filename $!";
 
  while (my $fetchline = <$FH>){
    chomp $fetchline;
    my @elements = split "," , $fetchline;
    $total += $elements[2];
  }
 
  print "$total\n";

Save the file as text.plx and if the CSV file name is filename.csv then you may run the code as

$ perl text.plx filename.csv

In this case we get 10 (1+2+3+4) as output.

In this article we used split function to extract out the columns from CSV file using comma (,) as separator. But this code is error prone because of 2 reasons.

Reason 1: If the filename.csv contains something like

Linux,Unix,1,Ubuntu
RHEL,Kubuntu,2,Fedora
Mint,"Puppy,Linux",3,ArchLinux
Mandriva,OpenSUSE,4,CentOS

Notice the 3rd row. The 2nd field of it has a comma “Puppy,Linux”. If we use the same approach of splitting fields by comma separator then it will throw an error because split function will split out Puppy and Linux since they are separated by a comma. Then it will do something like 1+2+Linux+4 which is wrong.

Reason 2: If the filename.csv contains something like

Linux,Unix,1,Ubuntu
RHEL,Kubuntu,2,Fedora
Mint,"Puppy,
Linux",3,ArchLinux
Mandriva,OpenSUSE,4,CentOS

Here the 3rd row contains multi-line fields. When the above code will be used it will treat the CSV file as it contains 5 lines instead of 4.

So we can conclude that if the CSV file is in good format AND if it does not contain field separators within quote AND if there are no multi-line fields in CSV file then our code (which we have written above) will work perfectly and will give correct result. Take use of Text::CSV_XS module which is used for reading and writing CSV files, it will deal with those error prone situations very efficiently.

How to install rpm package on linux ?

install rpm package

install rpm package

RPM was the most famous way of installing packages on a Red Hat based linux box, nowadays other distros are also using RPM to support their software, Suse and Mandriva fall in the zone. RPM stands for Red Hat Package Manager. RPM lets you install a package, upgrade and uninstall the same. It also allows a querying functionality which lets one know about already packages installed on the system.

RPM packages are those with a .rpm extension. RPM packages contains the actual software to be installed and other files that are needed to carry out its installation. RPM packages are distributed distro specifically. Any attempt to install one distro-specific package on another distro might result in undesired consequences. You have been warned !

Graphical installation tools install a package in few clicks but here discussed is old school command line method which has its own legacy.

From here on, you need to have root’s privileges.

To install rpm package, rpm command is used along with -i option which clearly stands for “install”:

rpm -i package.rpm

Packages are subjected to regular upgradations, to update an installed rpm package, engage following code.

rpm -U package.rpm

On a fine day, everything will go well and the rpm package will be installed on your system ready to be launched from command line. Installed software, rather than creating its own directory, goes into pre-created linux directories. Executable files find their path to specific bin directories. To shoot the program from command line, the whole path is not needed just the program name is sufficient.

If things go south during installation, it can be very irritating. Most famous error that comes up is due to failed dependency. Dependencies are the packages that are needed for installing package to mover further in installation process. They are just software that are needed for proper functioning of the package being installed. While installing, the database is checked for needed packages, if not present, it stops the installation process due to failed dependency.

To rectify, one way is to install/upgrade those missing packages (don’t worry it will tell you which packages are not present ) then continue with the current installation. Some times, this error is as meaningless as ‘ay’ in ‘okay’. An entry is made in database only when the package is installed via rpm. If any other method is used, the entry is not made and despite presence of files, “failed dependency” error shows up.

When you are sure that the needed files are already on your system, you can skip the dependency check and install rpm package anyway.

rpm -i package.rpm --nodeps –nodeps option installs it without checking for dependency.

If you try to fool your system by going with –nodeps right away, the program won’t work at all.

Removing the rpm package

rpm command along with -e option is used to remove a package installed with rpm.

rpm -e package.rpm -e is for “erase”

Note: While installing rpm package we included the whole package name but at the time of removing- only the program name can be used ignoring the extension.

rpm -e package This code works the same as above.

It removes all the files related to that package and also deletes the entry from database. Manually removing individual files will not affect database which can lead to dependency errors in future.

Making a query to database:

As mentioned earlier in the very article, the database knows what packages are installed on the system. The database can be queried to check if certain package exists on the database. For a single package query, command goes like:

rpm -q package

If the package is present, it returns the version of package installed, else, it says the package is not installed.

To list all the rpm packages installed on the system, run :

rpm -qa

To check for certain package in the list, “grep” can be used. We will cover that later, stay tuned.

How to find the CPU information from terminal in Linux?

lscpu output

You can use lscpu command or can view the file /proc/cpuinfo to get the CPU information from terminal.

Man page description of lscpu command:

lscpu gathers CPU architecture information like number of CPUs, threads, cores, sockets, NUMA nodes, information about CPU caches, CPU family, model and stepping from sysfs and /proc/cpuinfo, and prints it in human-readable format. Alternatively, it can print out in parsable format including how different caches are shared by different CPUs, which can also be fed to other programs.

$ lscpu

or

$ cat /proc/cpuinfo

Output:

How to delete all files of a directory in Linux?

How to delete all files of a directory in Linux?

To delete all files of a directory in Linux there are 2 methods available.

Suppose you want to delete all files of the directory dirname whose path is /home/usr/dirname then to delete all files of this directory you may use the 2 methods give below:

Method 1: Use rm command

$ rm /home/usr/dirname/*

or

$ cd /home/usr/dirname/
$rm *

Note: The above method will not delete files from sub-directories of dirname. To remove the files from sub-directories use

$ rm -r /home/usr/dirname/*

Use f option to force delete the files.

rm -rf /home/usr/dirname/*

Method 2: Use Find command

$ find /home/usr/dirname -type f -delete

or

$ cd /home/usr/dirname
$ find . -type f -delete

Note: Using the above method will delete all the files from sub-directories of dirname but it will not delete the sub-directories.

Edit: find and delete all files and folders (sub folders) [credit: ubungu]

find path_to_find -name ‘*test*’ -exec rm -rf {} \;

Types of Filesystem, differences and dealing with them using Perl

If you are looking to write code in Perl which will work on multiple platforms then you must have the knowledge of filesystem of each platform. Filesystems are categoriesed in 3 parts.

1. Filesystem of Unix

FFS (Berkeley Fast File System) is the ancestor of all current filesystems of Unix variants. The filesystem has been extended by different vendors according to their needs. Like for better security they have been extended to provide support for POSIX ACL (access control list).

In Unix filesystem the root is denoted by / (forward slash). The path of any directory in Unix filesystem starts from / and goes deeper in the system. The directory names or file names in Unix filesystem are case-sensitive.

Path Example: /root/home/user/Desktop/file.txt

2. Filesystem of Windows

Operating systems which are based on Windows supports 3 filesystems: FAT (File Allocation Table), NTFS (NT FileSystem), FAT32 (Advance version of FAT).

FAT filesystem is not case-sensitive. It uses \\ (backward slash) as path separator. In the FAT systems the direcotries and files contains some flags, these flags are known as attributes like “Read Only”. NTFS supports unicode feature.

Path Example: C:\\home\\user\\Desktop\\file.txt

3. Filesystem of Mac OS

Classic Mac OS used HFS (Hierarchical File System). The version 8.1 of Mac OS uses HFS+ which is an advance version of HFS. The current filesystem looks similar to Unix filesystem. The method of showing paths is same in both OS. The difference between these is that Unix file system is case-sensitive whereas HFS+ is not case-sensitive.

Summary

OS and FileSystem Path Separator Filename length Absolute path format Relative path format Unique features
Unix (Berkeley FFS and others) / OS-dependent number of chars /dir/file dir/file OS-variant dependent additions
Mac OS (HFS+) / 255 Unicode chars /dir/file dir/file Mac OS legacy support, BSD extended attributes
Windows based OS (NTFS) \\ 255 Unicode chars Drive:\\dir\\file dir\\file File encryption and compression
DOS (basic FAT) \\ 8.3 Drive:\\dir\\file dir\\file Attributes

 

Using Perl to deal with different Filesystems

By now we know all the filesystems differ from each other and to deal with these in Perl we use File::Spec module. This module is used to hide the differences between these filesystems. Let us learn how we use this module. We will use the catfile method to get the path of the file.

Example:

use File::Spec;
my $path= File::Spec->catfile(qw{home user Desktop file.txt});

In this way the scalar variable $path get sets to home\user\desktop\file.txt in a Windows operating system. and in Unix based system it gets set to home/user/Desktop/file.txt. The File::Spec module also contains methods like curdir and updir to get the current directory (.) and up-directory (..).

You may also use the moudle Path::Class which is an another good wrapper. You will have to install it before using.

Below is an example of how it works:

use Path::Class;
my $file = file (qw{home user Desktop file.txt});
my $dir = file (qw{home user Desktop});

The $file and $dir are scalar variable which contains the path to the file.txt file and Desktop directory respectively.

Here’s the path

print $file;
print $dir;

The output will depend on operating system. If it is Unix then it will yield

home/user/desktop/file.txt and home//user//desktop

and in Windows based systems it will give

home\\user\\desktop\\file.txt and home\\user\\desktop

Here $file and $dir are objects which have several methods which can be applied on them for some use. Like absoulte method gives the absolute path and slurp method slurps through the file’s contents.

my $abspath = $file->absolute;
my $content = $file->slurp;
$file->remove(); #delete the file

When you want to write a code for a particular system and you want to make this system understand the path of other operating system then you may use foreign_file() and foreign_dir() methods. These will return the path based the argument which you have to specify explicitly.

use Path::Class qw(foreign_file foreign_dir);
my $foreignfile = foreign_file('Win32', qw{home user Desktop file.txt});
my $foreigndir = foreign_dir('Win32', qw{home user});

Now $foreignfile will contain home\\user\\desktop\\file.txt even if the code is run from a Unix based system. This approach is very handy and useful. In the next article we will continue and expand our discussion.

How to install software in linux from source file (.zip, .tar.gz, .tar.bz2) ?

Before getting your hands on it, here is a heads up, installing package from source is not the most preferred method. Few might find in a difficult method. It is recommended for experienced Linux users or those who have a thing for exploring more. Generally mostly used software are shipped with the OS and the rest can be easily installed from their respective package managers.

Moving forward, standing by the definition of open source, some packages are in the form of source code. They are distributed this way. One can download the source file for the desired application, it is then unpacked, compiled to turn it into binary. Upon completion you would find that it is not that tough job. Most of the software you would require are still distributed in source form.

The source files all over the Internet are found (not only) in the zip file for tarball. Extensions like “tar.gz” or “tar.bz2” or “.zip” are a common view. It is recommended to follow this procedure from your home directory. From now on, it is assumed that the downloaded file resides in “src” in home directory. Lets create one:

mkdir $HOME/src

Navigate to “src” direcotry through “cd” command.
cd $HOME/src

Lets see what we have in their by using “ls” command which is used for listing directory contentss.
ls

We can see our recently downloaded source file. We now need to unpack it. Different methods are employed to unpack different kinds of files.

For .zip :
unzip

For .tar.gz :
tar -xvzf

For .tar.bz2 :
tar -jxvf

Now you will see a new directory having all source files. Use ls to see if it exists.
ls

Now go in that direcotry.
cd

At this stage, few applications have an INSTALL file and some don’t.
If it is there(use ls command to check) then you can read it by “more” command.
more README

Lets proceed with installation, it is completed in three stages:

  • Configure
  • Compile
  • Install

For pre-installation configuration:
./configure
This will check your system for essential requirements and is responsible for creation on “makefile”. “MakeFile” is needed by “make” utility and directs it for how the compilation should be carried out.

If you encounter an error, forums are always at your disposal. Be sure to search for it before posting. Also include the output you are getting in the post along with your system specifications. OR you can post in here in comments as we will be pleased to help you with that.

When you are done with configuration phase, next comes Compilation. Use “make” to compile it, compilation is carried out by following the instructions in “MakeFile”.
make

Final step, install binaries in the system. To do so, you need to be root. “su” command is used to switch user. Enter the password when prompted.
su

To finalize the installation, execute:
make install

And voila ! Now you know how to install software in linux. You might want to check the software documentation.

Understanding file permissions and access rights in Linux

In Linux everything is a file. To set access rights on the specific files we use chmod command. To see what are the access rights/ permissions of a specific file we use ls -l command. Below we will learn how to see permission of a file and change it.

Note: To change permissions of a file you must be login as root user.

Suppose you have a file named as linuxstall.txt in a directory named as LinuxStall which is in /tmp. Use ls -l command to see its permissions as:

ls -l /tmp/LinuxStall/linuxstall.txt

The output will be as

-rwxr--r--. 1 root root 0 Jan 5 15:39 /tmp/LinuxStall/linuxstall.txt

Let us first understand what does the above line means by breaking it down in different parts.

The left most bit can be of two types either d or .
If it is d then it means that it is a directory.
If it is then it means that it is a file.

Here in our example it is means it is a file.

Part 1: r w x (Shows owner’s access rights)
Explanation:
First bit is which means it is a file, if it is d then it means it is directory.
First bit is r which means read i.e the owner can open this file.
Second bit is w which means write i.e the owner can edit this file.
Third bit is x which means execute i.e the owner can execute this file.

Part 2: r – – (shows group’s access rights)
Explanation:
First bit is r which means read i.e the group members can open this file.
Second bit is which means group members can not edit this file.
Third bit is which means group members can not execute this file.

Part 3: r – – (shows other users which are neither owner nor group members)
Explanation:
First bit is r which means read i.e the other users can open this file.
Second bit is which means other users can not edit this file.
Third bit is which means other users can not execute this file.

 

Changing file permission using chmod command

chmod stands for change file mode bits

There are two methods of changing file permission using chmod command.

Syntax: chmod permission path_of_file

Method 1: Numerical Method

In this method we use numeric bits which corresponds to r w x. The numeric values for these are

r = 4, w = 2, x = 1

chmod 444 /tmp/LinuxStall/linuxstall.txt

will change the file permission of file linuxstall.txt to

-r – – r – – r – – which means owner, group, others can only read this file.

chmod 555 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r – x r – x r – x which means owner, group, other can read and execute the file.

chmod 666 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r w – r w – r w – which means owner, group, other can read and edit the file.

chmod 644 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r w – r – – r – – which means owner can read and edit the file, group and other can only read the file.

Hence we saw that first numeric bit is for owner, second numeric bit is for group and third one is for other users. You can set the numeric bits to 1,2,4 for execute, write and read purpose respectively.

Method 2: Alphabetical Method

There are 4 alphabets associated with it, they are u,g,o and a.

u stands for owner

g stands for group

o stands for others

a stands for all

To add permission we use + and to remove permission we use

Example 1: To grant read permission to all group users

chmod g+r /tmp/LinuxStall/linuxstall.txt

Example 2: To remove read permission from group users

chmod g-r /tmp/LinuxStall/linuxstall.txt

Example 3: To grant read,write,execute permission to owner

chmod u+rwx /tmp/LinuxStall/linuxstall.txt

Example 4: To remove execute permission from other users

chmod o-x /tmp/LinuxStall/linuxstall.txt

Example 5: To grant read and write permission to all users

chmod a+rw /tmp/LinuxStall/linuxstall.txt

Noticed use of a? It sets permission for all users.

 

Conclusion: We saw that each file in Linux has access rights associated with it. It is divided in 3 parts, one is owner, second is group users and third are other users. We can use chmod command to change the file permission. There are two methods, one is numerical and other is alphabetical method. Numerical method of setting permission is preferred though.

How to run a Linux Shell Script?

Have you ever wondered how you can run a script in Linux?
Every script that needs to run should have a execute permission. By default it is refrained from such permission. One has to assign execute bit manually. In order to do so execute following command:

#chmod +x SomeScriptFile
OR
#chmod 0755 SomeScriptFile

ls command can be used to check permissions on files:
# ls -l /bin/uname

Output:
-rwxr-xr-x. 1 root root 25948 Feb 8 2011 /bin/uname

[uname is being used for an example, it can be replaced with any script]

To run script:
$ ./SomeScriptFile [Enter]

You can also execute by specifying its full path
$ /full/path/here/SomeScriptFile.sh

For executing it in ksh shell:
$ ksh SomeScriptFile

For executing in bash shell :
$ bash SomeScriptFile

Illustration:

Creating a test shell script test.sh, any text editor can be used.
#!/bin/bash
echo “ Hello, Free World”
echo “ You are at : $(pwd)”

After saving this file, set the execute bit:
$ chmod +x test.sh
$ ./test.sh

./ can be omitted if current directory has its entry in PATH variable. It is advised to have your own bin directory. Add this directory to PATH variable using export command. Move your recently created script in this directory and save all the hassle of writing whole path, now you can execute this command from anywhere. This procedure is shown below:

$ mkdir $HOME/bin
$ export PATH=$PATH:$HOME/bin
$ mv test.sh $HOME/bin
$ test.sh