Saturday, May 28, 2011

Linux File Systems

In Linux, file system begins with root (/). This is at the top of the file system which means it is the starting point directory structure . Under root (/) there exists other files/folders.

Important Directories



Home Directory

/root

This is the root's home directory ie superuser's home directory.
/home/username

This is a directory in Linux system where each user a special directory. This is the default location when the user logs into the system. Eg: If a user of the system is called student, the home directory of tom will be /home/student

User Executables

/bin
This directory contains the most important programs that the system needs to operate, such as the shells, ls, grep, and other essential things.

/usr/bin
This directory contains applications for the system's users. Utilities like compilers, firefox browser and openoffice go into /usr/bin.

/usr/local/bin
Common executable application files ie software compared from source code goes here

In some cases it really doesn't make much difference if you put the program in /bin or /usr/bin .

System Executables

/sbin, /usr/sbin

This is where system programs are installed.  Most system administration programs are stored in these directories. You must run these programs as the root user. These include fdisk, tools to make partitions, certain network tools, etc

Mountpoints

The different physical storage devices  must be attached to some directory in the file system tree before they can be accessed. This attaching is called mounting, and the directory where the device is attached is called the mount point.

/mnt

Filesystems that are non-removable media will be mounted here

Eg:harddisk

/media

Filesystems that are removable media will be mounted here

Eg:cdrom

Configuration

/etc

The configuration files are stored in this directory .In /etc/ we can find the system's password file, group lists, user skeletons, cron jobs etc.

Temporary files

/tmp

This is the default location to place files for temporary use. When you install a program, it uses /tmp/ to put files during installation that won't be needed once the program is installed. The /tmp directory allows all users access to additional space to meet short term needs without charging the space against their quota. All files placed in this directory are deleted automatically after a few days.

Bootloader

/boot

This is the directory where Linux keeps information that it needs when booting up.

Server Data

/var

This contains administrative files such as log files, locks, spool files, and temporary files used by various utilities

/srv

This contains server data's such as databases and webpages

System Information

/proc

This is a virtual directory which contains information about the kernel. It doesn't contain 'real' files but runtime system information.

/sys

This gives information about hardware

Shared Libraries

/lib, /usr/lib

Important system libraries are stored here. 

Friday, May 27, 2011

process, kill

A process is a program in execution. It has several properties including priority. In Linux a process is identified by its PID (Process ID).

To view the information about active process we use 'ps'. This command gives status and properties of process.

syntax

ps [options]

 -e              Select all processes

 -u             Selects the processes whose user name or ID is in userlist.

 -o            user-defined format.

Eg: ps -eo pid,comm,%cpu

Output:

.
....
1516 evolution-excha  0.0
1557 notify-osd       0.0
1573 update-notifier  0.0
1611 firefox-bin     17.2
1671 plugin-containe  2.2
1935 gnome-terminal   1.2
.............
.......
..

ps -eo pid,s,user

Here 's' gives process state codes

D    Uninterruptible sleep (usually IO)
R    Running or runnable (on run queue)
S    Interruptible sleep (waiting for an event to complete)
T    Stopped, either by a job control signal or because it is being traced.
W    paging (not valid since the 2.6.xx kernel)
X    dead (should never be seen)
Z    Defunct ("zombie") process, terminated but not reaped by its parent.
Kill

'kill' command sends a signal to processes, causing them to
terminate.

kill PID

Eg: kill 2016

This command will terminate the process whose PID is 2016

Eg:kill 9 2016

This will forcefully terminate the process whose PID is 2016

Regular Expressions - regex

A regular expression (regex) is a string for describing a search pattern. Regular expression generally uses special characters called meta characters to represent something.

^  represents beginning of the line

$ represents end of the line

grep '^I'  test3.txt

Here the lines starting with 'I ' letter from the file 'test3.txt' will be displayed in the screen

grep '6$' marks.txt

Here the lines ending with '6'  from the file marks.txt will be displayed on the screen

To find the lines that starts with either letter 'a' or 'r' then use

grep -i '^[ar]' test.txt

To find the lines that doesn't starts with either letter 'a' or 'r' then use

grep -i '^[^[ar]]' test.txt

Also you can use '\<' and '\>' to search a particular word

Eg: If you want to replace the word 'in' with 'out' and if you use the following command

sed 's/in/out/gi' sample.txt

then this will replace all 'in' with 'out'. If the sample.txt has a word 'initial' it will be also replaced as 'outitial'. Here we wanted replace only 'in' word and so we have to alter the command

sed 's/\/out/gi' sample.txt

This will replace only the full words 'in' and not 'in' included in other words

sed - Stream Editor

'sed' is a stream editor.  A stream editor is used to perform basic text
transformations on an input stream. It doesn't alter the file contents.

Format is

sed OPTIONS... [SCRIPT] [INPUTFILE...]

sed  's/He/She/' letter.txt

Here 'letter.txt' is a filename where 'He' will be replaced by 'She' and displayed in the screen. 's' is a separator.

'sed' makes a maximum of one change per line

To have multiple changes per line we have to use as follows

sed  's/He/She/g' letter.txt

Also 'sed' searches are case sensitive and hence in-order to make search in-case sensitive we have to use the command as follows

sed  's/He/She/gi' letter.txt

To make many changes on a file, we can use the command as

sed -e  's/He/She/gi' -e  's/handsome/beautiful/gi' letter.txt

To make changes only on particular lines then use

sed '2,6s/He/She/g' letter.txt

Here entire letter.txt will be displayed on the screen but only lines '2' and '6' will be replaced by 'He' to 'She'.

We can also give starting and ending word  and search and replace the word in lines between words

sed  '/Dear/,/waiting/s/He/She/gi' letter.txt

Thursday, May 26, 2011

Logging into a Linux System

In Linux system we should remember that 'root' is the superuser.

We can log in to the root by giving following information

Login User:root

Password: Will be the password given at the time of installation

To log in to sub-user we can either log in to other virtual console or can switch in the same console using 'su' command.

Eg: Consider that you have logged in as root in console 1 (tty1) . Now you have to log in  as a sub user (Eg: student1)

If you want to login in other console 2, then press 'CTRL+ALT+F2'. You will get a shell prompt asking for username and password.

If you want to log in to sub user (student1) in same console 1 then there are 2 types of log in.

1. Log in by changing your current working directory

2. Log in without changing your current working directory

- Log in by changing your current working directory

Consider that you are logged in as root and your working directory will be root and you want to switch to user 'student1'. Then you have to use the following command

su - student1

Now you are logged in as 'student1' and your current working directory will be home (~)  folder of 'student1'.

Note: Since you are switching from 'root' to 'student1' the shell prompt will not ask for password to log in since root will be having full permission to access other users. However if you want to switch from subuser  to other subuser or root (eg student1 to student2) shell prompt will be asking for password to log in.

- Log in without changing your current working directory

Consider that you are logged in as root and your working directory will be root and you want to switch to user 'student1'. Then you have to use the following command

su  student1

Now you are logged in as 'student1' ,  however your current working directory will be unchanged that is it will be root folder
Powered By Blogger

Followers