Sunday, August 12, 2012

Basic Unix Commands


Login Process:

Login: user2
Password: ********

$  --> Normal user prompt
#  --> System admin prompt

Commands:

$ logname           -->  to check present working user.
User2

$ clear                 --> to clear screen
We can also use cntrl+l to clear screen

$exit                    --> to quit from user session.

Help Facility:

Syn:
1. $ man [command name]     --> displays use and options of the command.

Ex:  $ man date
Displays date command usage and options.
$ man cal
$ man man

2. $ pwd                         --> to display the present working directory
      /home/user2

3. 
$ cal                          --> to display calendar (current month calendar)
        $ cal 2000              --> to display 2000 year calendar.
        $ cal 08 2010         --> august month of 2010 year calendar
        $ cal 78                  --> 78 year calendar.
        $ cal 1                    --> 1st year calendar.
        $ cal 10000            --> illegal year usage range upto 1-9999 only

If you pass only one parameter after cal it considers it as year. If you pass two parameters 1st parameter is month and 2nd parameter is year.

4.  $ date                      --> to display date (displays system or server’s date which it is working)  

Options: 

        $ date +%m         --> to display month
        12
       $ date +%h           --> to display name of the month
        December
       $ date +%d           --> to display day of month (1 to 31)
       22
      $ date +%y            --> to display last two digits of the year.
      11
      $ date +%Y            --> to display four digits of the year.
      2011
      $ date +%D           --> to display date in MM/DD/YY format.
      12/22/11
      $ date +%T           --> to display time in the format hh:mm:ss
     12:30:54 H, M and S  --> this options are used to display hour, minutes and seconds.

  5.  $ who               --> to display present working users.
          User2 tty1
          User3 tty3
          User4 tty0
Here tty is the UNIX naming for terminals.
LINUX naming for terminals is Pts1, Pts2 ……
Terminal number is used identify the clients.

  6.  $ finger               --> displays more information about the users like name of the user, phone number, idle time etc ..,

  7.  $ who am i          --> displays current working users details
       User2   tty1    2011-11-12    17:20   ip address

  8.  $ whoami          --> displays current working user without details
       User2

  9.  $ tty                  --> to display terminal type

 10. $ sleep [time in sec]       --> to take the shell into sleeping state.
       $ sleep 5      
      For 5 seconds the shell will be going to sleep state

11. Executing multiple commands
      $ cmd1; cmd2; cmd3; cmd4
   Ex:  $ ls; sleep 5 ; date
Here first list of the files will be displayed then it goes to sleeping state for 5 seconds and it displays date.

12. Wild card characters

       A number of characters are interpreted by the UNIX shell before any other action takes place. These characters are known as wildcard characters. Usually these characters are used in place of filenames or directory names.
*             An asterisk matches any number of characters in a filename, including none.
?             The question mark matches any single character.
[ ]            Brackets enclose a set of characters, any one of which may match a single character
                at that position.
 -             A hyphen used within [ ] denotes a range of characters.
~             A tilde at the beginning of a word expands to the name of your home directory. If   
               you append another user's login name to the character, it refers to that user's home
               Directory.

Here are some examples:
1.      cat c*           --> displays any file whose name begins with c including the file c, if it exists.
2.      ls *.c            -->  lists all files that have a .c extension.
3.      cp ../abc?.  --> copies every file in the parent directory that is four characters long and
                     begins with abc to the working directory. (The names will remain the same)
4.      ls abc[34567]  --> lists every file that begins with abc and has a 3456, or 7 at the end.
5.      ls abc[3-7]       --> does exactly the same thing as the previous example.
6.      ls ~                  --> lists your home directory.
7.      ls ~user1         --> lists the home directory of the user with the user id user1.
8.      $ ls file [1-9] [1-9]  --> in the filename 5th char should be 1-9 and 6th should be 1-9.
        Ex: file 21   file34   file 56

LINUX Wild card characters

Here are wildcards and regular expressions:
* — Matches all characters
? — Matches one character
\* — Matches the * character
\? — Matches the ? Character
\) — Matches the ) character


v Working with Directories

·         Displaying Directory Contents

 $ ls      -->   To display the present working directory contents.

Options:

$ ls –a            --> to display all files including hidden files like . (Dot) and  .. (Double dot) files
$ ls |pg           --> to display contents page wise (only UNIX).
$ ls |more       --> to display contents line by line in UNIX and LINUX.
$ ls –x            --> to display in width wise.
$ ls –x|more   --> to display contents width wise and line wise.
$ ls –f             --> to display only files.
$ ls –F            --> to display all files including exe files.
$ ls –R            --> to display including sub directories recursively like tree structure.
$ ls –r             --> to display in reverse order.
$ ls –d            --> to display only directories.
$ ls –t             --> to display based on date and time of creation of files (latest to old files)
$ ls –u            --> to display based upon last accessed time.
$ ls –s             --> to display files including number of blocks used by the file and directories.
$ ls –i              --> to display files including I-node number of files. I-node number provides
                            information about the files.
$ ls –l              --> to display long list of the files.

Ex:
$ pwd  /home/user2

$ ls –l
d    rwxrw_rw_    3    user2      group1       5436     feb22        14:00    xxx
_    rw_rw_r__     2   user2     group1       231     oct21       10:00    file1

In above example First character is the type of the file.
_              --> Regular File                                                                                  
d              --> Directory File
c               --> Character Special File
b               --> Block Special File
f                --> FIFO File
s                --> Socket File
l                 --> Symbolic File
·         Creating a Directory
          $ mkdir [dir name]        -->  To create a Directory
·         Changing to a Directory
          $ cd [dir name]              --> To change into the Directory
·         To Create Multiple Directories
          $ mkdir [dir1] [dir2] [dir3] ....
·         To move back to Parent Directory
          $ cd ..
·         To move two levels up from PWD
          $ cd ../..
·         To move three levels up from PWD
          $ cd ../../..
·         To change to Home Directory or User Login Directory
          $ cd
·         To change to Root Directory
          $ cd /



Ex:   To create following Hierarchy
                / (root directory)

                               Home (users working location)

                                                                  User2 (Home directory of user which he logged in)
                                   
                                                X                                             Y


                                    X1               X2                          Y1                Y2


                        X11               X12                        Y11            Y12
$ pwd
/home/user2
$ mkdir X
$ mkdir Y
   To check whether the Directories created or not use ls command.
$ ls
X    Y
Now Change into X directory to create X1 and X2 directories.
$ cd X
$ pwd
/home/user2/X
Creating multiple directories at once
$ mkdir X1 X2
$ ls
X1      X2
Here X is the parent directory of X1 and X2
$ pwd
/home/user2/X
$ cd X1
$ pwd
/home/user2/X/X1
$ mkdir X11 X12
$ ls
X11       X12
Here X1 is the parent directory of X11 and X12
Now move back to Home Directory
$ cd
$ pwd
/home/user2
$ cd Y
$ pwd
/home/user2/Y
$ mkdir Y1 Y2
$ ls
Y1      Y2
Here Y is the parent directory of Y1 and Y2
$ cd Y1
$ pwd
/home/user2/Y/Y1
$ mkdir Y11 Y12
$ ls
Y11    Y12
Here Y1 is the parent directory of Y11 and Y12
To display Tree Structure of a Directory
   $ tree [dir name]
Ex:
   $ tree X
      X
                        X1
                                         X11
                                         X12                          
                        X2
If you enter the Tree command without directory name it shows current working directory tree structure.
    S pwd
    /home/user2
    $ tree
       .
                    X
                                 X1
                                                X11
                                                X12
                                 X2
                    Y
                                 Y1
                                                 Y11
                                                 Y12
                                  Y2

To go to Root Directory
$ cd/
$ pwd
/

To see the Tree structure of Root Directory line by line
          $ tree | more
It will display the Tree Structure of Root directory line By line.

·         Renaming the Directory
          $ mv [old name] [new name]
          $ mv X Z

·         Removing Directory

      To Delete a Directory it should be Empty
          $ rmdir [dir name]
   Ex:  $ rmdir x12
      To Delete a Directory including Sub Directories. It is a forceful deletion.
          $ rm –R [dir name]
   Ex:  $ rm –R Y
     Y dir along with its sub-directories is deleted.

·         Working with Absolute path and Relative path  Absolute Path:
           It’s a path from Root to Target Directory (Destination)
   Ex:   $ pwd
         /home/user2/X/X1/X11
 Now we have to change into Y2 directory in a Y directory
    $ cd [absolute path] 
    $ cd /home/user2/Y/Y2
    $ pwd
    /home/user2/Y/Y2
   $ cd /home/user2/X/X1/X11
   $ pwd
   /home/user2/X/X1/X11
        Relative Path:
             It’s a path from Current Directory to Target Directory.
   Ex:  $ pwd
       /home/user2/X/X1/X11
    Now change into Y2 Directory in Y Directory.
  Syn:  $ cd [Relative path]
      $cd ../../../Y/Y2
      $ pwd
     /home/user2/Y/Y2
   Ex:  $ pwd
    /home/user2/X/X1/X11
To create an Y21 directory in Y2 directory without moving from present working directory X11.
    $ mkdir /home/user2/Y/Y2/Y21          --> it’s by using Absolute Path.
    $ mkdir ../../../Y/Y2/Y21                     --> it’s by using Relative Path.
    $ pwd
    /home/ user2/X/X1/X11
To check the contents of Y2 directory in present working directory.
    $ pwd
    /home/ user2/X/X1/X11
    $ ls /home/user2/Y/Y2                    --> by using Absolute Path
    $ ls ../../../Y/Y2                               --> by using Relative Path.
To create complete Hierarchy as above at a time using relative path
   $ pwd
   /home/user2
   $ mkdir X   Y   X/X1  X/X2  X/X1/X11   X/X1/X12   Y/Y1    Y/Y2   Y/Y1/Y11  Y/Y1/Y12  
 To remove complete Hierarchy at a time using relative path
    $ pwd
   /home/user2
 $ rmdir   X/X1/X11   X/X1/X12   X/X1  X/X2  X   Y/Y1/Y11  Y/Y1/Y12   Y/Y1    Y/Y2   Y
It is a reverse path of mkdir to remove complete hierarchy.
·         Moving Directories
To move the Directory including sub Directories from Source to Destination.
  Syn:  $ mv [source path] [destination path]
Ex:
   $ pwd
  /home/user2
To move X1 directory including sub Directories into Y1 directory.
  $ mv /home/user2/X/X1   /home/user2/Y/Y1       --> Absolute Path
  $ mv X/X1  Y/Y1                                                  --> Relative Path
·         Copying Directory contents from one location to another location including sub directories
Syn:   $ cp [option] [source path] [destination path]
Options:
-i: 
    Interactive. Prompt for confirmation whenever the copy would overwrite an existing file. A in answer confirms that the copy should proceed. Any other answer prevents cp from overwriting the file.
-p:
     Preserve. Duplicate not only the contents of the original file or directory, but also the modification time and permission modes.

-R: 
     Recursive. If any of the source files are directories, copy the directory along with its files (including any subdirectories and their files); the destination must be a directory.

cp refuses to copy a file onto itself.
Ex:    $ pwd
  /home/user2
To copy X1 directory into Y1.
 $ cp –R /home/user2/X/X1    /home/user2/Y/Y1
                                (Or)
 $ cp –R X/X1   Y/Y1
Note:
     The difference between copy and move command is
     After copying a file from source to destination the file is available at both source and destinations.
     After moving a file from source to destination the file is available only at destination.
Working with Files

·         Creating a File
Cat command is used to create a file
 Syn:      $ cat > [file name]
Ex:   $ cat >file1
   bmnxbcmnxb
   cjdbcnbdncbdmns

Enter the data you want and press (control key + d) to save & quit from file
The symbol ‘>’ is used to enter the data into file using cat command.
    With cat command we can only create a single data file. To create multiple files is not possible through cat command it is possible with touch command.
·         Creating multiple files
 Syn:   $ touch [file1] [file2] [file3] [file4] …..
    Touch command is used to create multiple empty files. The data should be entered later using vi editor.
·         Displaying file contents
Cat command is also used to display the data in the file.
Syn:   $ cat < [file name] or    $ cat [file name]
Here the symbol ‘<’ is optional.
Note:    With cat command we can create file and display data in the file but we cannot modify the data of a file.
Ex:   $ cat file1
  bmnxbcmnxb
  cjdbcnbdncbdmns

·         Displaying contents of multiple files
With cat command multiple files data can be displayed.
     Syn:   $ cat [file1] [file2] [file3] ….
The data of the files will be displayed sequentially one after the other.
·         Copying files
To copy the data of one file to another file cp command is used.
Syn:  $ cp [source file] [destination file]
   If the destination file already exist then data of source file overrides the destination file data. If does not exist it creates a new file.
Ex: $ cp file1  file2
If the destination file exist, to get confirmation from user to override the data or not –i option is used
Syn:  $ cp –i [source file] [destination file]  --> to get confirmation to override.
If the file exist only it will ask for confirmation otherwise it create new file.
Ex:    $ cp –i file1 file2
Overwrite y/n? –
    The file is deleted only if option ‘y’ is entered, other than ‘y’ any char is entered the will not be deleted.
·         Rename a File
   To rename a file mv command is used.
Syn:   $ mv [old name] [new name]
   If the new name already existed the old name will be renamed to new name and new name data will be overridden by old name data.
Ex:  $ mv file1 filex
Here also to get confirmation we can use –i option.
·         Removing a file
    To delete a file rm command is used.
Syn:  $ rm [file name]
Ex: $ rm file1
·         Comparision of files
    To check the differences between the data of two files cmp command is used. But it displays only the first difference.
Syn: $ cmp file1 file2
   To display all the differences between the files diff command is used.
Syn: $ diff file1 file2
Note:  Comparison between the files of different users is possible only when the present working user has the access permission on the other user.
·         Removing multiple files
  To remove multiple files also rm command is used.
Syn: $ rm file1 file2 file3 file4 …..
    Here all the files which are entered will be deleted. If we want confirmation from user to delete the files –i option is used
Syn: $ rm –i file1 file2 file3 file4 ….
Ex: 
    $ rm –i file1 file2 file3 file4
Remove file1 y/n? y
Remove file1 y/n? n
Remove file1 y/n?
Remove file1 y/n? x
    Here only file1 is deleted. Other than option y if you type any character the file will not be deleted.
·         Knowing file types
   To know the type of the file, file command is used.
Syn: $ file [file name]
It displays the file type like exe, ascii, ZIP file etc.,
Ex: $ file file1
         ASCII text
      $ file file.zip
          ZIP archive
·         Search for a file
   To search for a file there are two types of commands. They are locate and find.
Locate command
     By using locate command we can search for the file in the entire system (OS).
Syn: $ locate [file name]     -->  in whole filing system
Ex:  $ locate file1
  /home/user1/x/file1
  /home/user2/file1
  /home/file1
 /file1
Find command
     By using find command we can search only in present working directory. To search in other locations by using –name option we have to specify the path it has to search in.
Syn: $ find [file name]  --> only search in the present working directory
        $ find –name [file path]  --> to search for a file in required location
Ex:   $ find –name /file1       --> to search for file1 in root directory.
   $ find –name /home/file3   --> to search for file3 in home directory
·         wc
   To count number of lines, words, chars in a file wc command is used. By using this command multiple files data can also be counted.
Syn:  $ wc [file name]
 Ex:   $ wc file1
         3     20      50       file1    --> here 3 lines 20 words and 50 characters
$ wc file1 file2 file3 file4
  3     20    50      file1
  2     10    20      file2
  5     40    30      file3
  6     50    100    file4
Options:
-l        -->     To display only lines
-w      -->     To display only words
-c       -->     To display only chars
-lw     -->     To display lines and words
-lc      -->     To display lines and chars
-wc    -->     To display words and chars
·         od
od – octal dump. It shows the binary format of file.
Options:
   -b       -->    to display ascii values of characters in a file
   -bc     -->    to display ascii values of characters along with characters.
Ex:     $ od -b  file1
00060       163         164        158        193
$ od –bc file1
00060       163          164       158        193
   a              z               d           c            f
·         Compressing File
     Compacts a file so that it is smaller. When compressing a file it will be replaced with a file with the extension .gz, while keeping all the same ownership modes.
    gzip is command to compress a file. gzip is any of several software applications used for file compression and decompression. The term usually refers to the GNU Project's implementation, "gzip" standing for GNU zip. It is based on the DEFLATE algorithm, which is a combination of Lempel-Ziv (LZ77) and Huffman coding.
Syn:   $ gzip [filename]
After compressing file into zip file then file name will be changed to filename.zip and the original file will not be available.
Ex:   $ gzip file1
       Result is file1.gz and the original file file1 will not be available.
·         Uncompressing File
    To uncompress the zip file gunzip command is used.
Syn:  $ gunzip [filename.gz]
Ex: $ gunzip file1.gz
Result is file1
·         Working with archival file
Archival file is a pack which contains hierarchy file system.
Ø  Creating archival file
“tar” is command used to create archival file.
Syn:   $ tar –cvf   [user defined name.tar] [dir name]
Here
          c   --> create new archival file
          v  --> VERBOSE is security protocol
          f   --> specified files
Ex:
    $ tar –cvf    myarchive.tar  .     --> . refer to all files in current working directory
    $ tar -cvf    myarchive1.tar x   --> all files in x directory
    $ tar –cvf    myarchive2.tar file1 file2 file3 file4  --> files to be archived
Ø  Extracting archive file
   To extract the archive file tar command with option –xvf  is used.
Syn:  $ tar –xvf  [archived file name]
Ex:   $ tar –xvf  myarchive.tar
Here x stand for extract archive file
·         Creating archive file and compressing
     We can create archive a file and compress it. For doing both actions we use tar command with –czvf option.
Syn:   $ tar –czvf [userdfined name.tar.gz] [directory name]
Ex:  $ tar –czvf myarchive.tar.gz .  --> it will archive and compress the present working directory file system and name as myarchive.tar.gz
        $ tar _czvf  myarchive1.tar.gz x   --> x directory file system will be archived and compressed.
·         Uncompressing and Extracting archive file
By using tar command with –xzvf option we can uncompress and extract the archive file.
Syn:  $ tar –xzvf  [archive file name.tar.gz]
Ex:   $ tar –xzvf  myarchive1.tar.gz
Here archive file is uncompressed first and then Extracted. The result is original file system.
Link Files
There are two types of link files
1.      Hard Link
2.      soft Link
1. Hard Link
      It is a link file to another file. When the original file is deleted in Hard link, even then we can see the data in link files.
Whenever link file is created on original file then link counters increases.
                        file1                                                            file1
filex                                                           filex
                                                                  filey
                  link count=2                                                 link count=3
To create hard link to file1 by using a link file filex ln command is used
$ ln file1 filex
$ ls –l
--------2---------file1
--------2---------filex
Here 2 is the link counter number
$ ln file1 filey
$ ls –l
----------3--------file1
----------3--------filex
----------3--------filey
Here 3 is the link counter number
$ rm file1
    Even original file is deleted data will not be deleted. data will be remained in link files. Whenever a link file or original file is deleted just link counter decreases. When link counter reaches to 1 the data will be deleted.
    If any changes made in original file data that affects the link file data and vice versa. since all link files refer to the same data.
2. Soft Links
        Soft link is also a link to the original file. but when the original file is deleted link file will not work.
To create a soft link on a file use “-s” option with ln command
$ ln –s file1 filex


$ ls –l
-l-----------1------------file1
-l-----------1------------filex  --> file1
Here l indicates symbolic link file

$ ln –s file1 filey
------------1------------file1
-l ---------1------------filex  --> file1
-l ---------1------------filey  --> file1
In soft link there will be no increment of link counter number.
when the file1 is deleted other link files on file1 will not work.

FTP (File Transfer Protocol)
     File Transfer Protocol (FTP) is a network protocol used to copy a file from one computer to another over the Internet or LAN. FTP follows a client-server architecture which utilizes separate control and data connections between the ftp client and server.
To connect to FTP server enter ftp command in user mode. Now $ prompt changes to ftp prompt there open the server connection by using ip address and password.
   Syn: $ ftp
   ftp > open 192.14.35.76
   Password: ********
To close the server connections close command is used.
    ftp> close
      237 goodbye
To come out from ftp to $ prompt bye command is used.
    ftp> bye
    $-
Commands used in ftp
ftp> verbose                    --> Verbose mode gets off.
ftp> pwd                          --> To display server’s current working directory
ftp> lcd                            --> To display client’s current working directory.
ftp> ls                              --> To display server’s directory contents.
ftp>dir                             --> To display server’s directory contents.
ftp> mkdir [dir name]     --> To create a directory on server.
ftp> cd [dir name]           --> To change a directory on server.
ftp> rmdir [dir name]      --> To delete a directory from server.
ftp> delete [file name]    --> To delete file on server.
ftp> mdelte file1 file2..   --> To delete multiple files on server.
ftp> binary                      --> To set the transfer mode as binary
ftp> put [filename]          --> To upload the file
ftp> mput file1 file2..     --> To upload multiple files
ftp> mput *.cpp              --> To upload all the files with extension .cpp
ftp> get [file name]        --> To download the file
ftp> mget *.cpp              --> To download the multiple files with extension .cpp.
ftp> disconnect               --> To disconnect from server.
ftp> quit                         -->  To quit from ftp prompt
ftp> bye                         --> disconnecting from server and quit from ftp prompt.
ncftp   --> Anonymous ftp
$ ncftp 192.65.78.90     --> To connect to the ftp site without UID and PWD.


No comments:

Post a Comment

Thank you :
- kareem