glugt-mgl

 

Intermediate shell operation

Page history last edited by msk 2 yrs ago

I. Files and File Permissions

 

Files in linux

Linux files are setup as such that access to them is controlled. There are three types of access:

  1. read
  2. write
  3. execute

Each file belongs to a specific user and group. Access to the files is controlled by user (owner), group, and what is called "other". The term, other, is used to refer to someone who is not the user (owner) of the file, nor is the person a member of the group the file belongs to.

 

File Permission Characters

File names can be up to 256 characters long with "-", "_", and "." characters along with letters and numbers.
When a long file listing is done, there are 10 characters that are shown on the left that indicate type and permissions of the file. File permissions are shown according to the following syntax example: d rwx rwx rwx
There are a total of 10 characters in this example, as in all Linux files. The first character indicates the type of file, and the next three indicate read, write, and execute permission for each of the three user types, user(owner), group and other. Since there are three types of permission for three users, there are a total of nine permission bits. The table below shows the syntax:

 
1 2 3 4 5 6 7 8 9 10
File User (Owner) Permissions Group Permissions Other Permissions
Type Read Write Execute Read Write Execute Read Write Execute
d r w x r w x r w x
 
  • Character 1 is the type of file: - is ordinary, d is directory, l is link.
  • Characters 2-4 show owner permissions. Character 2 indicates read permission, character 3 indicates write permission, and character 4 indicates execute permission.
  • Characters 5-7 show group permissions. Character 5=read, 6=write, 7=execute
  • Characters 8-10 show permissions for all other users. Character 8=read, 9=write, 10=execute

There are 5 possible characters in these fields. They are:

  • r - read permissions
  • w - write write permissions
  • x - execute permissions
  • s - setuid (This is only found in the execute field)
  • If there is a "-" in a particular location, there is no permission. This may be found in any field whether read, write, or execute field.

 

The command for modifying the permissions of the files is chmod

 

Syntax : chmod [options] [filename]

 

Note :

             u - user (owner)

             g - group

             o - others

 

For example, chmod u+x myfile command gives the user (u) execute permissions (+x) ..

 

Some more examples are shown below

 

chmod u+x myfile Gives the user execute permission on myfile.
chmod +x myfile Gives everyone execute permission on myfile.
chmod ugo+x myfile Same as the above command, but specifically specifies user, group and other.
chmod 400 myfile Gives the user read permission, and removes all other permission. These permissions are specified in octal, the first char is for the user, second for the group and the third is for other. The high bit (4) is for read access, the middle bit (2) os for write access, and the low bit (1) is for execute access.
chmod 764 myfile Gives user full access, group read and write access, and other read access.
chmod 751 myfile Gives user full access, group read and execute permission, and other, execute permission.
chmod +s myfile Set the setuid bit.
chmod go=rx myfile Remove read and execute permissions for the group and other.

 

Note : Read permission has a value of 4, write permission has a value of 2 and execute permission has a value of 1. In the examples above, there are commands which depict permissions are numbers. The first digit is the permission for the user (owner), the second digit for the group and the third digit for others. '

For example, consider the permission number 751 .. 7 = 4 + 2 +1 .. the user (owner) has read, write and execute permissions ..

5 = 4 + 1 .. the group has read and execute permissions

1 .. others have only execute permissions

 

II. Searching in linux

 

1. FIND

find - search for files in a directory hierarchy
find searches the directory tree rooted at each given file name by  evaluating  the  given  expression from left to right, according to the rules of precedence until the outcome is known at which point
find moves on to the next file name.

Syntax :

find [path...] [expression]
The most basic usage is this:
find -name "*.txt"
Another useful test is -size, which lets you specify how big the files should be to match. You can specify your size in kilobytes and optionally also use + or - to specify greater than or less than.
For example:

find /home -name "*.txt" -size 100k

find /home -name "*.txt" -size +100k

find /home -name "*.txt" -size -100k

 

The -user option enables you to specify the user that owns the files you are looking for. So, to search for all files in /home that end with .txt, are under 100KB, and are owned by user paul, you would use this:

 

find /home -name "*.txt" -size -100k -user paul

 

 

You can flip any of the conditions by specifying -not before them. For example, you can add a -not before -user paul to find matching files owned by everyone but paul:

find /home -name "*.txt" -size -100k -not -user paul

 

2. GREP

grep, egrep, fgrep - print lines matching a pattern
Grep is a wonderful command in Linux and makes the Linux environment that much easier to process large files or large amounts of data.

Syntax:

grep [ -[[AB] ]num ] [ -[CEFGVBchilnsvwx] ] [ -e ] pattern | -ffile ] [ files...  ]
 
 

 

 

 

 

 

 

Comments (2)

Ankit said

at 5:19 pm on Jun 12, 2007

done by ankit!

sp2hari said

at 1:07 am on Jun 13, 2007

oops , i have added something about file permission in another section :(. Either this or that has to be removed

You don't have permission to comment on this page.