Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts

Wednesday, 11 August 2010

Creating new commands and passing arguments in UNIX...

When a particular sequence of commands is repeated time after ,it might be better if we create a shorter command of our own. These command creation mentioned may not be suitable for public use but would be helpful for private use.

Consider suppose that we intend to count the number of users quite frequently:
$ who|wc -l

We can make a small command of ours named 'count' that does the same work.
So we do
$echo 'who|wc -l'>count (A file named 'count' is created).

Now we run the shell program with its input coming from the file 'count'.
$ sh<count
this is same as
$ sh count

Now to type sh all the time wouldn't be such a great thing. So we make the file 'count' executable by
$ chmod +x count
Now the result of doing 'who|wc -l' can be produced by doing 'count'.
$ count
Install count ,place it in the /usr/bin directory. Set the path appropriately and start using the command
created.

Now again when we begin to create executable commands of our own, we will have to make it executable . So it would be better if we create a shorter command for chmod +x filename.
The problem here would be that the filename that is passed to 'chmod' may change every time.
Therefore we do
$ echo 'chmod +x $1'>cx (cx would be the file created).

Here $1-indicates that only 1 argument can be passed to the command at a time
$2 would make it 2 and $9 would make it 9.
So to enable any number of arguments we do
$ echo 'chmod +x $*'>cx

Now we need to make cx executable.
This can be done either by doing
$ chmod +x cx or else
$ sh cx cx because sh cx is equivalent to doing chmod +x $*.
Now test it by creating a test command 'test' ,install it and run the command. 'Permission denied' will be the message. Now do
$ cx test
$ test
The command 'test' will run and the result will be produced.

Now suppose we have a file 'class' consisting of some information about the students of our class and consider a case where we need information about certain students and we need it quite frequently. This can be done with the help of the 'grep' command.
$ echo 'grep $* /root/new/class'> find
install the command.
$ cx find /* makes the file executable
$ find

Now a possible would arise when the following case is given as the argument:
$ find sunil kumar
This is because the words sunil and kumar will be seen as two different arguments.
Hence do
$ echo 'grep “$*” /root/new/class'> find
This would solve the problem.

Tuesday, 3 August 2010

Files in the Unix file system and their permissions...

A file is a sequence of characters. A few commands and knowledge that may help in dealing with files and directories in UNIX are the following.
The 'ls' commands and its variations :
The 'du' command and its variations
The command used to change the permissions – 'chmod'.
A look into these commands
ls command gives a listing of all the files that are present in the current directory.
The 'ls -l' is used to provide a detailed listing of the files that are present.
For eg. If you type the command 'ls -l' the results be will produced as
-rw-r--r-- 1 root root 856 2010-08-03 20:15 delete.c

-rw-r--r-- 1 root root 868 2010-08-03 08:19 line.c

where line.c and delete.c are the two files present in the directory.
The other variations of the command include ls-u,ls-t etc
The 'du' (disk usage)provides information about the usage of the disk.
The command 'du -a' is used to provide disk informations about even the files of subdirectories.
The 'du-a|grep line.c'
provides the details about the file 'line.c'.
'chmod' is the command that is used to change permissions. The permissions can be changed by the superuser.
Consider the following permissions:
-rwxrwxrwx - indicates that the owner,the group and the other users of the system can all read(r),write(w)and execute(x) the file of which the permission has be shown.
Inorder to change the permission , the 'chmod' command is used.
For eg. 'chmod 666 file'
indicates that the file can be read and written by all the three categories of users mentioned above.
The octal values have their meanings as
4-indicates that the file can be read
2-file can be written
1-file can be executed
therefore 'chmod 444 file' would set the permissions of the file to read only for all the categories of users.
6 6 6 indicates(4+2 4+2 4+2) for the three users.)
Now do 'ls -l' and you will see the permissions as
-r--r--r--.
'+' is used to turn a permission on and '-' to turn it off.
The command 'chmod +w file' enables write permissions for the file
and 'chmod -x file' turns off the execute permissions for the file.
The permissions of the file can be viewed using the 'ls -l' command stated before. The permissions of the directory cab also be set and viewed using the 'ls -ld' command.
The 'ln','cp','mv' commands are used in UNIX for slightly different uses.
The 'ln command creates a link to the file. Therefore the same file can be accessed using the two file names. The syntax is as follows:
'ln filename filename1'
You would obtain another link to the file named 'filename' by the name of 'filename1':
The change made to original file would reflect in the other as well. You could see that with help of the 'ls -i' (command that indicates the 'i-number' of the file)that the two files that are linked are having the same i-number.
'cp' command is used to make the copy and the 'mv' command is used to move or rename files.
The command 'mv filename1 filenname2...... destination directory' is used to move several files together to the destination directory.

Friday, 30 July 2010

A few basic commands in Linux

The common  file system commands that would make a beginner get started with linux are the following:

ls:command that lists all the files in the current directory
Variations in the use of the ls command would be found as the ls -l,ls -t...
cp source dest:command that is used to copy file 'source' to file 'dest'
mv source dest:moves the file 'source' to file'dest'
rm files:command used to remove a particular file
These commands when invoked would be looked in the directory /usr/bin
To check the presence of the 'cp' command use:
which cp
You would get back /usr/bin
There is a case where the use of a paricular command (say cp)would display in a message as follows
bash:cp command not found
Make use of the 'which' command to see if the cp command is present in the directory
which cp
There might be no response
Therefore the coreutil package needs to be reinstalled
apt-get install coreutils --reinstall coreutils
This would fix the problem.
The other basic commands would include the commands for printing the text:
cat filename:
pr filename:

Directories in linux contain files and subdirectories.Each user will be having his own home directory to whih he logs in at the beginning of a session.The user may then move on work in other directories which becomes his current directory.

The basic commands that are used in regard with the directories include the following

mkdir dirname:creates a new directory
cd dirname:change to the particular directory(provided that you are in the correct place)
To check whether you are in the correct place use
pwd
You would get back the 'current directory'
cd ..: is used to go the previous directory.
cd: moves to the home directory.

The shell-Basic facilities offered

Shell -the command interpreter.
There are many basic facilities offered by the shell.They include

1)Using shorthands for files(use of the asterisk '*')
For example the command
pr temp*
would print the contents of all files beginning with 'temp'
Similarly the echo command
echo *
will list all the files in the current directory

2)Input-Output redirection
Consider the case where we want to list the files of the directory but not in the terminal
So we redirect it to a file using the '>' operator
ls > filename
If the file 'filename ' is not already created it will be so,or else it will be overwritten.
Counting the number of lines in a particular directory is done as
ls >filename
wc -l<tfilename



3)Inorder to avoid the creation of a temporary file,PIPES are used for the input-output redirection
The vertical bar '|' is the pipe.
Instead of using
ls>filename
wc -l<filename
we use
ls|wc -l
for the redirection to be possible.

4)Tailoring the environment
As a user of UNIX,we could customize it in the way comfortable to us.
The 'stty' command helps us do that
for eg:
stty erase '^h'
enables the user to use ctrl-h for erasing a particluar character.
By storing it in the file .profile (present in the home directory),we need not type it each time we login
One of the most important tailoring comes during the setting up of paths
Again this could be made changed in the '.profile' file.
The current path would enable searches in the directories /usr/bin and /bin.
If we did want to augment another directory to the search path we do it as follows:
PATH=export $PATH:/usr/games
where usr/games would be the newly added search directory.
The augmented path would be obtained using the 'echo' command
echo $PATH
:/usr/bin:/bin:/usr/games

All these changes could be saved into the file named '.profile'