Sunday, June 10, 2012

Tutorials on Unix by Me

Time Stamps In Unix


“ Time Stamps in Unix“
Area : UNIX
Author: Rahul Miglani

As a beginner we must know that , we have 3 different time stamps in Unix,Each file has three time stamps, which record the last time that certain operations were performed on the file ,You can search for files whose time stamps are within a certain age range, or compare them to other time stamps.
1: Modify Time: This is last time when the actual contents of the file were modified.
We can get list of files modified n days using find command.
find . –type f –mtime +n -exec ls –l {} \;
-n : files modified between today to n-1 days.
n : files that was modified on that particular n day.
+n : files modified from n+1 days back .
Needless to say that we get modified time of file using ls –lt command 
Another example would be : Finding 15 Days old files 
find *.* -mtime -15  
You can play with these files now : Deleting these 15 Days old files
find *.* -mtime -15 -exec rm -rf {}\;

2: Change time: Change time is something like altering the label of the package (file) whereas modify time is altering the content of the file.
So in Unix we can say, change time is last time change in inode of the file and inode changes when we update the file, change permission,rename file, change in owner etc ..
So whenever mtime change , ctime also does (change in content of file also update inode),but ctime change some extra time as explained above.
We use –ctime in find to get list of files changed in particular days back.
ls –lc command is used to get the change time of a particular file.
Here file was zipped (renamed), means inode information of the file has been modified.
username@servername:username> ls -lt TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Dec 16 14:18 TestScript.sh.gz
username@servername:username> ls -lc TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Jan 25 07:29 TestScript.sh.gz
Here ls –lc gives the time when the file was zipped.
Another example would be the use of find command with ctime
find *.* -ctime -15  
or may be you want to delete these files.
find *.* -ctime -15 -exec rm -rf {}\;

3: Access Time: It’s a time the file was last accessed means the file was read. Last time we read that file.

username@servername:username> ls -lu TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Jan 25 07:43 TestScript.sh.gz
Example : Finding files which were accessed with in 15 days.
find *.* -atime -15  
You can play with these files now : Deleting these 15 Days old files which were accessed (Not Modified)
find *.* -atime -15 -exec rm -rf {}\;
NOTE : With find you can choose to type -print or not with the find command, because the basic principle of find is obviously to print what ever it is doing so you may choose to type -print .To know more about the find command you can type man find on unix , to know what has been written about the find in Unix.
Summary :
Access Time (atime ) : This is the time that the file was last accessed, read or written to.
Modify Time (mtime ) :This is the last time the actual contents of the file were last modified.
Change Time (ctime ):This is the time that the inode information (permissions, name, etc., the metadata, as it were) was last modified
Enjoying Playing with the times in Unix.

Friday, June 8, 2012

SQL : SYNONYMS


A synonym is an alias for a database object (table, view, procedure, function, package, sequence, etc.). Synonyms may be used to reference the original object in SQL as wel as PL/SQL. 

They can be used to hide ownership and location of the database objects they refer to and minimize the impact of moving or renaming the database objects.  

There are two types of synonyms:
  • private
    Private synonyms exist only in a specific user schema. The owner of the synonym maintains control over availability to other users.
  • public
    A public synonym is available to all users

Example
In your database you have a schema called HRM. This schema contains a table called EMPLOYEES. To query the table you use the following SELECT statement:

SELECT * FROM HRM.EMPLOYEES;

-- first we grant select privileges to all database users
GRANT SELECT ON HRM.EMPLOYEES TO PUBLIC;

-- next, we create a public synonym for your table
CREATE PUBLIC SYNONYM EMPLOYEE_DATA FOR HRM.EMPLOYEES;

-- from now on, anyone can query your table using the synonym
SELECT * FROM EMPLOYEE_DATA;

SQL : VIEW


SQL: VIEWS


A view is, in essence, a virtual table. It does not physically exist. Rather, it is created by a query joining one or more tables.

Creating a VIEW

The syntax for creating a VIEW is:
CREATE VIEW view_name AS
SELECT columns
FROM table
WHERE predicates;

For Example:

CREATE VIEW sup_orders AS
SELECT suppliers.supplier_id, orders.quantity, orders.price
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id
and suppliers.supplier_name = 'IBM';
This would create a virtual table based on the result set of the select statement. You can now query the view as follows:
SELECT *
FROM sup_orders;

Updating a VIEW

You can update a VIEW without dropping it by using the following syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT columns
FROM table
WHERE predicates;

For Example:

CREATE or REPLACE VIEW sup_orders AS
SELECT suppliers.supplier_id, orders.quantity, orders.price
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id
and suppliers.supplier_name = 'Microsoft';

Dropping a VIEW

The syntax for dropping a VIEW is:
DROP VIEW view_name;

For Example:

DROP VIEW sup_orders;

Frequently Asked Questions


Question: Can you update the data in a view?
Answer: A view is created by joining one or more tables. When you update record(s) in a view, it updates the records in the underlying tables that make up the view.
So, yes, you can update the data in a view providing you have the proper privileges to the underlying tables.

Question: Does the view exist if the table is dropped from the database?
Answer: Yes, in Oracle, the view continues to exist even after one of the tables (that the view is based on) is dropped from the database. However, if you try to query the view after the table has been dropped, you will receive a message indicating that the view has errors.
If you recreate the table (that you had dropped), the view will again be fine.

Thursday, June 7, 2012

Recursive Grep


How do I grep (i.e. search for a string) recursively through subdirectories on UNIX?

   find . | xargs grep some_pattern
This searches through all files starting from the current directory down. Note that this includes non-text files.
   find . -name "*.txt" | xargs grep some_pattern
This will restrict your search to certain file names or file types.


Grep command: Recursively Search All Files For A String
cd /path/to/dir
grep -r "word" .

grep -r "string" .
Ignore case distinctions:
grep -ri "word" .
To display print only the filenames with GNU grep, enter:
grep -r -l "foo" .
You can also specify directory name:
grep -r -l "foo" /path/to/dir/*.c

find command: Recursively Search All Files For A String

find command is recommend because of speed and ability to deal with filenames that contain spaces.
cd /path/to/dir
find . -type f -exec grep -l "word" {} +
find . -type f -exec grep -l "seting" {} +
find . -type f -exec grep -l "foo" {} +
Older UNIX version should use xargs to speed up things:
find /path/to/dir -type f | xargs grep -l "foo"
It is good idea to pass -print0 option to find command that it can deal with filenames that contain spaces or other metacharacters:
find /path/to/dir -type f -print0 | xargs -0 grep -l "foo
I find myself needing this more and more often lately, so I figured this is as good a place as any to keep track of it. Here's how to recursively search for files containing a particular string or regular expression. This should work on most Linux or UNIX systems, depending on your configuration.
    find -type f -name "*" | xargs grep 'word, phrase, or regular expression'
Summary for the curious:
  • find returns the names of one or more files, but not their contents.
  • -type f tells find to only look at files, not directories.
  • -name "*" tells find to examine everything. You can replace this with "*.js" to only search JavaScript files, for example.
  • | "pipes" the output of the command on the left to the input of the command on the right.
  • xargs creates and executes a command based on the arguments you pass to it.
  • grep searches the contents of files and returns each line that is a match.
  • '...' is a regular expression (or just a word, if you prefer) that indicates what you are searching for.




Oracle Interview Questions