You are on page 1of 13

Directories

y y

mkdir dirname --- make a new directory cd dirname --- change directory. You basically 'go' to another directory, and you will see the files in that directory when you do 'ls'. You always start out in your 'home directory', and you can get back there by typing 'cd' without arguments. 'cd ..' will get you one level up from your current position.

Example:-

Use cd to change directory. Use pwd to see what directory you are in.
% cd english % pwd % /u/ma/jeremy/english % ls novel poems % cd novel % pwd % /u/ma/jeremy/english/novel % ls ch1 ch2 ch3 journal scrapbook % cd .. % pwd % /u/ma/jeremy/english % cd poems % cd % /u/ma/jeremy y y

pwd --- tells you where you currently are rmdir dirname ---removes directory

Example:-

Use this command to remove a directory. For example, to remove a directory called "essays", do this:
% rmdir essays

A directory must be empty before it can be removed. To empty a directory, use rm.
y

rm remove file rm -f --- forcely remove the file rm -i --- Requires confirmation.

Example:use rm to remove files from your directory.


% rm i foo remove foo? y

% rm i letter* remove letter1? y remove letter2? y remove letter3? n %

The first command removed a single file. The second command was intended to remove all files beginning with the string "letter."

Commands
y

ls --- lists your files ls -l --- lists your files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified. ls -a --- lists all files, including the ones whose filenames begin in a dot, which you do not always want to see. There are many more options, for example to list files by size, by date, recursively etc. more filename --- shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern.

Example:-

More is a command used to read text files. For example, we could do this:
% more poems

The effect of this to let you read the file "poems ". It probably will not fit in one screen, so you need to know how to "turn pages". Here are the basic commands:
y y y y y y

q --- quit more spacebar --- read next page return key --- read next line b --- go back one page Less filename --- shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern. umask
Used to specify default permissions while creating files.

File Creation
y

cat
(for concatenate) command is used to display the contents of a file. Used without arguments it takes input from standard input <Dtrl d> is used to terminate input.

cat [filename(s)] cat > [filename] Data can be appended to a file using >> Example :
y

This is one of the most flexible Unix commands. We can use to create, view and concatenate files. For our first example we create a three-item English-Spanish dictionary in a file called "dict."
% cat >dict red rojo green verde blue azul <control-D> %

<control-D> stands for "hold the control key down, then tap 'd'". The symbol > tells the computer that what is typed is to be put into the file dict. To view a file we use cat in a different way:
% cat dict red rojo green verde blue azul %

If we wish to add text to an existing file we do this:


% cat >>dict white blanco black negro <control-D> %

Now suppose that we have another file tmp that looks like this:
% cat tmp cat gato dog perro %

Then we can join dict and tmp like this:


% cat dict tmp >dict2

echo
The echo command echoes arguments on the command line. echo [arguments]

Example :1. echo Hello!! How are you > kpd1.txt Cat kpd1.txt hello!!! How are you!!!! 2. Echo Bye Bye

3. The echo command echoes its arguments. Here are some examples:
% echo this this % echo $EDITOR /usr/local/bin/emacs % echo $PRINTER b129lab1

4. Things like PRINTER are so-called environment variables. This one stores the name of the default printer --- the one that print jobs will go to unless you take some action to change things. The dollar sign before an environment variable is needed to get the value in the variable. Try the following to verify this:
% echo PRINTER PRINTER

Touch
It is also used to create a new empty file.

Example:- touch myfile.txt myfile1.txt myfile3.txt

File Creation and Display Operation Command


CAT:cat > test1.txt hell0 I am here to learn Unix Starting from 2:00 PM IST <ctrl-Z> cat test1.txt

If you use ">" ouput re-direction symbol it every time override the file content. That means It create a new File. cat > test1.txt hell0 I am here to learn Unix Starting from 2:00 PM IST<ctrl-d> cat test1.txt Suppose I want to append some content to the above file then I will use following file re-direction symbol i.e. >> cat >> test1.txt I am testing here for the file re-direction operation<ctrl-d> cat test1.txt ---------------------------------------------------------------------------------------------------------------------Touch:touch test1.txt test2.txt test3.txt It will create three files with zero bytes. What a need? Suppose I wanna to execute script in the Unix If this file exist then I can use touch. Generally Touch command use to change the file creation If you use option -t u can change file creation date for the file touch -t201009291000 test.txt -----------------------------------------------------------------------------------------------------------------------Vi Editor:You can create file using Vi-editor. I will discuss more on this In tomorrow session. For now, Just use to create the file. vi hello.txt <enter> It will open on editor Then press i - to insert the data in the file then press esc You will be out the insert mode

the type :wq! for saving the file cat hello.txt--u will see the content on to your output stream.

File Permission,user,group and shell change Commands:chmod - chmod is a Unix command that lets you tell the system how much (or little) access it should permit to a file. - rwx r-xr-x kpd1 acctg wordmatic The first set three letters after the file type tell what you, the owner of the file, have permission to do. An r in the first position means you are permitted to read the file. A w in the second position means you may write the file. An x in the third position means you may execute the file. A hyphen in any position means that you don t have that particular permission. Example:chmod permission filename Letter Meaning u The user who owns the file (this means you. ) g The group the file belongs to. o The other users a all of the above (an abbreviation for ugo)

Or you can use number 4-Read 2- write 1- execute 7 - all Example:drwxrwxr-x 2 training training 4096 2010-09-29 09:20 friend chmod a-x friend drw-rw-r-- 2 training training 4096 2010-09-29 09:20 friend

find . -name "echo.txt" -print find: ./friend: Permission denied You can copy the command using the following symbol "=" chmod go=u friend ------------------------------------------------------------------------------------------------------------------------chgrp - To change the group Example:chgrp training friend chown - To change the owner Example:chown training friend -----------------------------------------------------------------------------------------------------------------------Chsh:- Change of Shell Example:- List the shells available chsh -l /bin/sh /bin/bash /bin/nologin To change the shell chsh -s /bin/sh ask for the password to change exit and I re-login shell will be changed.

Use and Group id s commands:To find a user's UID or GID in Unix, use the id command. id -u training 500 id -g training 500 id -G training

wc command
Character Count wc -c test.txt 30 test.txt Line Count wc -l test.txt 3 test.txt Word Count wc -w test.txt 3 test.txt

History Command:If you type History It will give List of all command on the screen for that user. You will find this file in the home directory with name as ".sh_history" or ".bash_history"

Weak And Strong quote


Suppose I am preserve the content without being replace I will use single quotes or I will use double quotes. Confused Example:echo '$HOSTNAME `pwd`' o/p $HOSTNAME `pwd` echo "$HOSTNAME `pwd`" o/p localhost.localdomain /home/training

Generally Info:echo *

It will all file name in the current and also subsequent directory in the current directory

Symbolic link
I wanna to store same content at different location(i.e. I mean in different directory) For these I have to create same file in different directory. To avoid this Unix/Linux as provide very useful command called "ln" Example: ln -s hello.txt ./friend/hello lrwxrwxrwx 1 training training 8 2010-09-29 09:07 hello-> /home/training/NT-odc/kpd1/hello.txt ------------------------------------------------------------------------------------------------------------

Cut Command
Column selection mode A column is one character position. In this mode cut acts as a generalized for files substr function. Classic Unix cat cannot count characters from the back of the line like Perl substr function, but rcut can ). This type of selection is specified with -c option. List entries can be open (from the beginning like in -5, or to the end like in 6-), or closed (like 6-9). cut -c 4,5,20 sample.txt # cuts sample file at columns 4, 5, and 20. cut -c 4-6,10-13,20- sample.txt # cuts sample file at columns 4 to 6 then 10 to 16 and again from 20 to end of the line. cut -c -4,5- sample.txt # cuts sample file at columns 1-4 and 5 to end of that line.Just same as cat of the sample file

Field selection mode In this mode cut selects not characters but fields delimited by specific one character delimiter specified by option -d. The list of fields is specified with -f option ( -f [list] ) cut -d ":" -f1,7 /etc/passwd # cuts fields 1 and 7 from /etc/passwd

cut -d ":" -f 1,6- /etc/passwd # cuts fields 1, 6 to the end from /etc/passwd cut -d ":" -f -4,6- /etc/passwd # cuts fields 1 to 4, 6 to the end from /etc/passwd The default delimiter is TAB. If space is used as a delimiter, be sure to put it in quotes (-d " "). -------------------------------------------------------------------------------------------------------------------------

Head command.
head filename by default will display the first 10 lines of a file. Example :head test.txt If you want first 50 lines you can use head -50 filename Example ;head -50 test.txt or for 37 lines head -37 filename and so forth. head -37 test.txt -------------------------------------------------------------------------------------------------------------------------

Mv command.
mv command is used to move a file from one directory to another directory or to rename a file.

Some examples: mv oldfile newfile will rename oldfile to newfile. mv * /usr/appl/ will move all the files in current directory to /usr/bajwa directory. -------------------------------------------------------------------------------------------------------------------------

Tail command.
tail filename by default will display the last 10 lines of a file. Example :tail test.txt If you want first 50 lines you can use tail -50 filename Example ;-

tail-50 test.txt -------------------------------------------------------------------------------------------------------------------------

pipe "|"
This use to re-direct output of one stream as Input to another stream Example :First 10 file in the directory ls -ltr | head Last 10 file in the directory ls -ltr | tail

cat test1.txt Wed Sep 29 15:04:41 IST 2010 cat test1.txt | cut -c1-10 Wed Sep 29 cat test1.txt | cut -f1-4 Wed Sep 29 15:04:41 IST 2010 cat test1.txt | cut -f1-4 -d" " Wed Sep 29 15:04:41 -------------------------------------------------------------------------------------------------------------------------

Grep Command
To search for the particular string in the file or in the output of the file. $ cat test.txt HELLO hello Hi HIHELLO hihello hi hello HI HELLO Just search for the string in the file but it is case sensitive. $ cat test.txt | grep "hello"

hello hihello hi hello W - Search for the Word $ cat test.txt | grep -w "hello" hello hi hello i- use to ignore the case $ cat test.txt | grep -wi "hello" HELLO hello hi hello HI HELLO n - to print the number $ cat test.txt | grep -wni "hello" 1:HELLO 2:hello 6:hi hello 7:HI HELLO -------------------------------------------------------------------------------------------------------------------------

Generally Info
To Know the host name u can do in two ways echo $HOSTNAME or uname -n localhost.localdomain

Generally Info
Configuration files for shells
Shells read configuration files on multiple circumstances which differ depending on the shell. This table shows the configuration files for popular shells:

ksh csh tcsh /etc/.login login login /etc/csh.cshrc yes yes /etc/csh.login login login ~/.tcshrc yes ~/.cshrc yes yes[4] ~/.login login login ~/.logout login login /etc/profile login login ~/.profile login login
~/.bash_profile ~/.bash_login ~/.bash_logout ~/.bashrc /etc/zshenv /etc/zprofile /etc/zshrc /etc/zlogin /etc/zlogout ~/.zshenv ~/.zprofile ~/.zshrc ~/.zlogin ~/.zlogout

sh

bash

zsh

login login[5] login[5] login[5] login int.+n/login yes login int. login login yes login int. login login

Explanation:
y y y y y

blank means a file is not read by a shell at all. "yes" means a file is always read by a shell upon startup. "login" means a file is read if the shell is a login shell. "n/login" means a file is read if the shell is not a login shell. "int." means a file is read if the shell is interactive.

"i.login" means a file is read if the shell is an interactive login shell.

You might also like