Introduction to UNIX
Sakshi Education
D. Balakrishna, Research Associate, IIITH
Introduction
UNIX is an operating system which was first developed in the 1960’s. It is a stable, multi-user and multi-tasking system for desktops, laptops and servers. There are many different versions of UNIX although they share common similarities. The most popular varieties of UNIX are GNU/Linux, Sun Solaris and MacOS. A UNIX system also has the graphical user interface similar to windows which provides an easy to use environment. The UNIX operating system is made up of three parts Kernel, shell and programs. The kernel of UNIX is the heart of the operating system. It allocates the time and memory to programs and handles the data storage and communication to various system calls. The shell acts as an interface between the user and the kernel. When user logs in then it checks username and password given by user and then starts another program called the shell. The shell is command line interpreter. It interprets the commands given by user and performs respected task for that command. The commands are themselves programs, when they terminate, the shell gives the output prompt to user. The users can customize own shell and can use different shells on the same machine. By default the user will have tcsh shell. The tcsh shell has special features to help the user by inputting commands. For example file name completion that is by typing a part of the file name or directory name and then by pressing “Tab” key the tcsh shell will give complete name automatically. If the shell finds more than one name beginning with those letters then it will prompt you to type few more letters before pressing the tab key again. The shell keeps a list of the commands you have typed in. If we want to use same command before we used then by using up/down arrow keys can use the previous command. We can see history of previous commands by typing history on shell.
Files and Process
In UNIX everything is either a file or process. A file is a collection of data. The files are created by users using text editors. A process is an executing/running program identified by a unique PID (process identifier) number. File names are case sensitive, can contain any character including whitespace, except ‘/’ and there is no length limit.
Filesystem structure
All the files are grouped together in the directory structure. The filesystem is arranged in a hierarchical structure like an inverted tree. The top of the hierarchy is called root of the structure and written as / (slash).
/ Root directory
/bin/ Basic system commands
/boot/ Kernel Image, initrd and configuration files
/dev/ Files representing devices
/dev/hda – first IDE hard disk
/etc/ System configuration files
/home/ User directories
/lib/ Basic system shared libraries
/lost+found corrupt files the system tried to recover
/media Mount points for removable media
/media/usbdisk, .media/cdrom
/mnt/ Mount points for temporarily mounted file systems
/opt/ Specific tools installed by the sysadmin
/usr/local/
/proc/ Access to system information
/proc/cpuinfo, /proc/version
/root/ root user home directory
/sbin/ Administrator files
/sys/ system and device controls
/tmp/ Temporary files
/usr/ Regular user files
/usr/bin/, /usr/lib/, /usr/sbin/
/usr/local/ specific software installed by sysadmin
/var/ Data used by the system or system servers
/var/log, /var/spool/mail, /var/spool/lpd
Listing files and Directories
When you first login into the terminal your current working directory is your home directory. Your home directory is same as your user name for example in the figure3 “bala” is home directory and this is the place where your all personal files and subdirectories are stored. To find out what is your home directory type “ls” command on the terminal and it prints all the files present in the home directory as shown below figure.
The “ls” command lists the content of your current working directory except those files starting with a dot (.) and these files are known as hidden files, usually contain important program configuration information.
ls –a – Lists all the files including .* files that is hidden files also.
ls –l – Lists long listing files with type, date , size, owner and permission.
ls –S – Lists the biggest files are first.
ls –t – Lists the most recent files are first.
ls –r – Lists the files in reverse order.
ls –lt – Here options are combined to print long listing, most recent files at a time.
Making Directories:
“mkdir” is command used to create or make directories with given names.
Example: mkdir dir -- here it will create directory with name dir.
Changing to a different directory:
“cd” command used to change from current working directory to another directory.
Removing file and Directories:
“rm” and “rmdir” commands are used to remove files and directories.
Pathnames:
Pathnames enable you to work out where you are in a relation to the whole file system.
“pwd” (print working directory) command is used to find out the absolute path name or display the path of the current directory.
“~” (your home directory) – Home directories can also be referred to by the tilde (~) character. It can be used to specify paths starting from your home directory.
Copying files:
cp command is used copy file from one place to another place, that means which creates same file in another directory.
Moving files:
mv command is used to move a file from one place to another place. This has the effect of moving rather than copying the file, so it ends up only one file rather than two files.
Displaying the content of a file on the screen:
Grep command:
The grep command is used to scan the given files and displays the lines which match the given pattern. The grep command is case sensitive, so to ignore case distinction use the –i option. To search for a pattern or phrase you must enclose it in single quotes.
Manual pages:
man is the command is used to display manual pages about the command. It will contain detailed description of the command, syntax and usage. Most available manual pages are about UNIX commands and also about C functions, headers, data structures and about system configuration files. Manual page files are looked for in the directories specified by the MANPATH environment variable.
File access rights:
Each file has access right which may found by typing ls –l to check the access rights for given file. Also ls –lg gives additional information like the given file relates to which group.
In the left hand column string consists of the symbols ‘d’, ‘r’, ‘w’, ‘x’,’-‘. ‘d’ indicates it is a directory present at the left hand end of the string otherwise ‘-‘ indicates it is file. The remaining symbol indicates the permissions and access rights to the three groups.
Three types of access rights:
Three types of access levels:
Examples:
Changing the access rights:
Chmod (changing a file mode): Only the owner of a file can use this command to change the permissions of a file. The options of chmod as follows.
Redirection:
Most processes initiated by UNIX commands write to the standard output that is they write to the console and many take their input from the standard input that is they read it from the keyboard. There is also the standard error, where the processes write their error messages to the console.
Special devices: Device files with a special behavior.
Processes:
A process is an executing program identified by a unique PID (process Identifier). To see the information about processes then type command “ps” which gives the information about all processes running. A process may be in the foreground or background or suspended. In general the shell does not return any prompt until the current process has finished executing. The symbol “&” allows running the job in the background and returns the prompt straight away, so that the user can run other programs.
Kill a process:
It is sometimes necessary to kill the process, to continue further work. To kill a process or a job running in the foreground type “Control+C”. The processes can be killed by finding their pid numbers.
rsync (remote sync): rsync has been designed to keep in sync directories on two machines with a low bandwidth connection.
UNIX is an operating system which was first developed in the 1960’s. It is a stable, multi-user and multi-tasking system for desktops, laptops and servers. There are many different versions of UNIX although they share common similarities. The most popular varieties of UNIX are GNU/Linux, Sun Solaris and MacOS. A UNIX system also has the graphical user interface similar to windows which provides an easy to use environment. The UNIX operating system is made up of three parts Kernel, shell and programs. The kernel of UNIX is the heart of the operating system. It allocates the time and memory to programs and handles the data storage and communication to various system calls. The shell acts as an interface between the user and the kernel. When user logs in then it checks username and password given by user and then starts another program called the shell. The shell is command line interpreter. It interprets the commands given by user and performs respected task for that command. The commands are themselves programs, when they terminate, the shell gives the output prompt to user. The users can customize own shell and can use different shells on the same machine. By default the user will have tcsh shell. The tcsh shell has special features to help the user by inputting commands. For example file name completion that is by typing a part of the file name or directory name and then by pressing “Tab” key the tcsh shell will give complete name automatically. If the shell finds more than one name beginning with those letters then it will prompt you to type few more letters before pressing the tab key again. The shell keeps a list of the commands you have typed in. If we want to use same command before we used then by using up/down arrow keys can use the previous command. We can see history of previous commands by typing history on shell.
Files and Process
In UNIX everything is either a file or process. A file is a collection of data. The files are created by users using text editors. A process is an executing/running program identified by a unique PID (process identifier) number. File names are case sensitive, can contain any character including whitespace, except ‘/’ and there is no length limit.
- Regular files
- Directories - directories are also files listing set of regular files and mixture of other subdirectories.
- Symbolic links – Files referring to the name of another file.
- Devices and peripherals – Read and Write from devices as with regular files.
- Pipes – used to cascade programs.
- Sockets – used for Inter Process Communication.
Filesystem structure
All the files are grouped together in the directory structure. The filesystem is arranged in a hierarchical structure like an inverted tree. The top of the hierarchy is called root of the structure and written as / (slash).
/ Root directory
/bin/ Basic system commands
/boot/ Kernel Image, initrd and configuration files
/dev/ Files representing devices
/dev/hda – first IDE hard disk
/etc/ System configuration files
/home/ User directories
/lib/ Basic system shared libraries
/lost+found corrupt files the system tried to recover
/media Mount points for removable media
/media/usbdisk, .media/cdrom
/mnt/ Mount points for temporarily mounted file systems
/opt/ Specific tools installed by the sysadmin
/usr/local/
/proc/ Access to system information
/proc/cpuinfo, /proc/version
/root/ root user home directory
/sbin/ Administrator files
/sys/ system and device controls
/tmp/ Temporary files
/usr/ Regular user files
/usr/bin/, /usr/lib/, /usr/sbin/
/usr/local/ specific software installed by sysadmin
/var/ Data used by the system or system servers
/var/log, /var/spool/mail, /var/spool/lpd
Figure1: File system Structure
Command line interpreters
A command line interpreter is a computer program that reads singular lines of text entered by the user and interprets them in the context of given operating system. This interaction takes place by means command line interface and it is indicated by console or shell. Commands are input in a terminal and the results are displayed on the terminal console, no graphical environment is needed for this. There are different types of shells available for UNIX systems and some of them are following.
sh: The Bourne shell
Basic shell found on every UNIX systems.
csh: The C shell
It is popular with C –like syntax
tcsh: The TC shell
A C shell compatible implementation with evolved features.
bash: The Bourne shell
It is a sh shell with improved features.
Starting a Terminal: To open a unix terminal click on “Terminal” icon from Applications ? Accessories menus as shown in figure2.
Command line interpreters
A command line interpreter is a computer program that reads singular lines of text entered by the user and interprets them in the context of given operating system. This interaction takes place by means command line interface and it is indicated by console or shell. Commands are input in a terminal and the results are displayed on the terminal console, no graphical environment is needed for this. There are different types of shells available for UNIX systems and some of them are following.
sh: The Bourne shell
Basic shell found on every UNIX systems.
csh: The C shell
It is popular with C –like syntax
tcsh: The TC shell
A C shell compatible implementation with evolved features.
bash: The Bourne shell
It is a sh shell with improved features.
Starting a Terminal: To open a unix terminal click on “Terminal” icon from Applications ? Accessories menus as shown in figure2.
A terminal window appears with a prompt and waiting for you to entering the commands as shown in figure3.
Figure3: Terminal with prompt
~/.bashrc: Shell script read each time when a bash shell is started. You can use this file to define
~/.bashrc: Shell script read each time when a bash shell is started. You can use this file to define
- Your default environment variables.
- Your prompts.
- Greeting messages.
Listing files and Directories
When you first login into the terminal your current working directory is your home directory. Your home directory is same as your user name for example in the figure3 “bala” is home directory and this is the place where your all personal files and subdirectories are stored. To find out what is your home directory type “ls” command on the terminal and it prints all the files present in the home directory as shown below figure.
Figure4: List of files in home directory
The “ls” command lists the content of your current working directory except those files starting with a dot (.) and these files are known as hidden files, usually contain important program configuration information.
ls –a – Lists all the files including .* files that is hidden files also.
ls –l – Lists long listing files with type, date , size, owner and permission.
ls –S – Lists the biggest files are first.
ls –t – Lists the most recent files are first.
ls –r – Lists the files in reverse order.
ls –lt – Here options are combined to print long listing, most recent files at a time.
Making Directories:
“mkdir” is command used to create or make directories with given names.
Example: mkdir dir -- here it will create directory with name dir.
Changing to a different directory:
“cd” command used to change from current working directory to another directory.
- cd – cd with no argument always return to the home directory.
- cd . – (.) means the current directory.
- cd ~ – (~) moves to home directory.
- cd .. – (..) means the parent of the current directory that is it will take you to one directory up in the hierarchy.
Removing file and Directories:
“rm” and “rmdir” commands are used to remove files and directories.
- rm filename – it will remove the given file.
- rm –r filename– removes the directory with given directory name.
Pathnames:
Pathnames enable you to work out where you are in a relation to the whole file system.
“pwd” (print working directory) command is used to find out the absolute path name or display the path of the current directory.
“~” (your home directory) – Home directories can also be referred to by the tilde (~) character. It can be used to specify paths starting from your home directory.
Copying files:
cp command is used copy file from one place to another place, that means which creates same file in another directory.
- cp file1 file2 is the command used make a copy of file1 in the current directory and calls it file2.
- cp –i – this command asks for user for confirmation if the target file already exist.
- cp –r – dir1 dir2 command used to copy the whole content of the directory.
Moving files:
mv command is used to move a file from one place to another place. This has the effect of moving rather than copying the file, so it ends up only one file rather than two files.
- mv file1 file2 is the command used move or rename file1 to file2.
- mv –i – this command asks for user for confirmation if the target file already exist.
Displaying the content of a file on the screen:
- “clear” command is used to clear the text on the screen and the prompt will appear at the top of the window.
- “cat” command is used to display the content of a file on the console.
- “less” command is used to write content of a file onto the screen a page at a time and press the ‘space bar’ to move another page and ‘q’ to quit from reading.
- “head” command writes the first ten lines of a file to the console.
- “tail” command writes the last ten lines of file on the console.
- “wc” command is used to count number of lines or words or characters in a file.
Grep command:
The grep command is used to scan the given files and displays the lines which match the given pattern. The grep command is case sensitive, so to ignore case distinction use the –i option. To search for a pattern or phrase you must enclose it in single quotes.
- grep arm help.txt
- grep - i Arm help.txt
- grep ‘cross compile’ help.txt
Manual pages:
man is the command is used to display manual pages about the command. It will contain detailed description of the command, syntax and usage. Most available manual pages are about UNIX commands and also about C functions, headers, data structures and about system configuration files. Manual page files are looked for in the directories specified by the MANPATH environment variable.
- man ls
- man printf
- man stdio.h
- man fstab
File access rights:
Each file has access right which may found by typing ls –l to check the access rights for given file. Also ls –lg gives additional information like the given file relates to which group.
ls –l
ls –lg
In the left hand column string consists of the symbols ‘d’, ‘r’, ‘w’, ‘x’,’-‘. ‘d’ indicates it is a directory present at the left hand end of the string otherwise ‘-‘ indicates it is file. The remaining symbol indicates the permissions and access rights to the three groups.
- The left group of three gives the file permissions for the user that owns the file.
- The middle group gives the permissions for the group of people to whom the file belongs.
- The rightmost group gives the permissions for all others.
Three types of access rights:
- Read access (r) – indicates the read permission to read and copy the file.
- Write access (w) – indicates the permission to change the file.
- Execute rights (x) – indicates the permission to execute the file.
Three types of access levels:
- User (u) – for the owner of the file.
- Group (g) – each file also has a group attribute corresponding to a given list of users.
- Others (o) – for all other users.
Examples:
- -rwxrwxrwx – A file that everyone can read, write and execute.
- -rw-r--r-- - Readable and writable for file owner and only readable for others.
- drwx------ - Directory accessible by only by its owner
Changing the access rights:
Chmod (changing a file mode): Only the owner of a file can use this command to change the permissions of a file. The options of chmod as follows.
Examples:
- Syntax: chmod
- Chmod 644 file1 – read and write for user and only read for group and others.
- Chmod u-w – remove write permission from user.
- Chmod a-x – remove execute permission from all.
Redirection:
Most processes initiated by UNIX commands write to the standard output that is they write to the console and many take their input from the standard input that is they read it from the keyboard. There is also the standard error, where the processes write their error messages to the console.
- Redirecting the output to a file using > symbol.
- Appending to a file using >> symbol.
- Redirecting the input from a file using < symbol.
Special devices: Device files with a special behavior.
- /dev/null – The data sink. Discards all the data written to this file. It is useful to get rid of unwanted output data, typically log information.
- /dev/zero – Reads from this file always return \0 characters. It is useful to create a file with all zeros.
- dd if=/dev/zero of=disk.img bs=1k count=1024
- /dev/random – Returns random bytes when read happens. It is mainly used in cryptographic programs.
- /dev/urandom – For programs for which pseudo random numbers are fine.
Processes:
A process is an executing program identified by a unique PID (process Identifier). To see the information about processes then type command “ps” which gives the information about all processes running. A process may be in the foreground or background or suspended. In general the shell does not return any prompt until the current process has finished executing. The symbol “&” allows running the job in the background and returns the prompt straight away, so that the user can run other programs.
- ps – lists current processes
- ps –ux: lists all the processes belonging to the current user.
- Ps – aux: lists all the processes running on the system.
Kill a process:
It is sometimes necessary to kill the process, to continue further work. To kill a process or a job running in the foreground type “Control+C”. The processes can be killed by finding their pid numbers.
- Kill
- Kill %1 – kill the job number 1
- Kill 2652 – kill the process number 2652
- Kill -9 pid – Sends an immediate terminal signal. The system itself terminates all the processes. It is useful when a process is really struck.
- Kill -9 -1 – kill all the process of the current user. -1 means all the processes.
- Kill all command – kills all the jobs running.
rsync (remote sync): rsync has been designed to keep in sync directories on two machines with a low bandwidth connection.
- It copies only changed files. Files with same size are compared by checksums.
- Only transfer the blocks that differ with in a file.
- Can compress the transferred blocks.
- Preserves symbolic links and file permissions.
- Can work through ssh.
Published date : 28 Jul 2015 12:16PM