Shell Programming 1

download Shell Programming 1

of 34

Transcript of Shell Programming 1

  • 8/8/2019 Shell Programming 1

    1/34

    UNIX/LINUXSHELL PROGRAMMING - 1

    --by Nataraja Rao

    1http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    2/34

    ObjectivesUnix file systemCreating files in UNIXFile manipulation

    File type meanings & permission typesLinks and inode numbersFile permissions

    Masking file permissionsSome useful commands

    2

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    3/34

    Unix File SystemUnix treats everything it knows and understands, asa file .All utilities, applications, data in Unix is stored asfiles.Even a directory is treated as a file whichcontains several other files .

    3

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    4/34

    Unix File System (contd)The file system begins with a directory called root .The root directory is denoted as forward slash( / ).Branching from root there are several otherdirectories called bin , lib , usr , etc , tmp , and etc..These directories are called sub-directories, theirparent being the root ( / ) directory.

    Each of these sub-directories contain several filesand directories called sub-sub-directories.

    4

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    5/34

    5

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    6/34

    Creating Files$ touch file1

    This creates a file called file1.The size of the file would be zero bytes since touchdoesnt allow you to store anything in a file.When we want to create several empty files quickly.

    $ touch file1 file2 file3 file4

    If we want to store a few lines in a file ?$ cat > file1 then press enter and type data whateveryou wants to write in that file1.

    6

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    7/34

    Creating Files (contd)To save & close that file1 press enter and then pressCtrl d

    .In Unix the keys Ctrl d indicate the EOF or end of filecharacter.To see the contents of the created file1$ cat file1 cat command can concatenate the contents of two filesand store them in third file.

    $ cat file1 file2 > file3To append data into a file, we will replace > with>> .

    7

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    8/34

    File Manipulation$ cp file1 file2

    This will copy the contents of file1 into a file file2.If file2 does not exist, it will be created.

    $cp

    file1 file2 folderThis will copy all files mentioned are copied tofolder named directory. Provided that the

    directory folder exists.

    8

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    9/34

    File Manipulation (contd)In Unix you can copy files from or to different

    directories by specifying their name along with thepath.$ cp /tmp/folder1/file1 /tmp/folder2/file2rm removes the given file or folder.$ rm fv file -r recursive

    $ rm rfv folder -f forcefully-v verbose

    9

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    10/34

    File Manipulation (contd)mv move or rename from one file to another.

    $ mv file1 file2Above command will cut file1 and then paste it asfile2$ mv file1 file2 folder1Above command will cut file1 and file2 named

    files, then paste them into folder1 named folder.

    10

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    11/34

    Listing Files and Directoriesls to see the list of files, like windows dir command

    Windows dir command also works in Linux.$ ls$ dir

    11

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    12/34

    Listing Files and Directories (contd)12

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    13/34

    Listing Files and Directories (contd)13

    Any filename which begins with a . is treated as a

    hidden file.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    14/34

    Listing Files and Directories (contd)14

    $ ls p*

    The above command will list the files, which is startswith letter p.$ ls ?ileWhen the shell comes across a ?. It understandsthat the symbol signifies any single character.

    The * is interpreted by the shell as presence of anynumber of characters.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    15/34

    Listing Files and Directories (contd)15

    $ ls [aeiou]*

    This indicates that the first character of the filenameto be listed must be any one of the letters givenwithin the square brackets, and the remaining canbe anything.$ ls [!aeiou]*The ! Symbol will exclude specified characters

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    16/34

    Listing Files and Directories (contd)16

    $ ls [a-m][c-z][4-9]??

    This will list all5 character filenames in the currentdirectory whose first character is in the range a tom, the second character is in the range c to z , thethird character is in the range 4 to 9 , whereas thefourth and fifth are any valid characters.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    17/34

    Listing Files and Directories (contd)17

    $ ls l (long listing)

    Unix treats all entities files, directories, devices asfiles.To differentiate between all of them it uses filetypes.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    18/34

    Listing Files and Directories (contd)18

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    19/34

    File type meanings & Permission types19File Type Meaning

    - Ordinary file

    d Directory file

    c Character special file

    b Block special file

    l Symbolic links Semaphore

    p Named pipe

    m Shared memory file

    Permission type Permission meaning Permission weight

    r read 4

    w write 2

    x execute 1

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    20/34

    Links and Inode numbers20When a file has two links, it is not physically present

    at two places, but can be referred to by either ofthe names.Unix will identify files using Inode numbers, thesenumbers are unique.$ ln file1 file2The above command will create link for file1, withthe name of file2 , but the Inode numbers aresame for the above two files.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    21/34

    Links and Inode numbers (contd)21

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    22/34

    File Permissions22The existing file permissions can be changed by ownerof the file or by the superuser(root).The way to change these permissions is by using thechmod command.$ chmod 755 file1

    First digit is Owner permissions7 = read(4) + write(2) + execute(1)Second digit is Group permissions

    5 = read(4) + execute(1)Third digit is Other users permissions5 = read(4) + execute(1)

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    23/34

    File Permissions (contd)23The above way of changing file permissions is

    referred to as the absolute mode .There is another syntax for chmod that changespermissions, which constitutes the symbolic mode.

    Syntax:$ chmod [who] [+/-/=] [permissions] fileThe who here refers to whom the permissions are to

    be assigned. It may be the user or owner(u), thegroup(g) or others(o). If none is specified, all areassumed.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    24/34

    File Permissions (contd)24+ means add permissions

    - means remove permissions= means instructs chmod to add the specifiedpermission and take away all other, if present.

    Ex:$ chmod +w file1Add write permissions for all(owner, group, others)Ex:$ chmod go-x file2Remove executable permissions to group and others.

    Ex:$ chmod go+r,go-w file3Add read permissions to group and others, remove

    write permissions to group and others.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    25/34

    File Permissions (contd)25Ex:$ chmod go=r,u=rw file1

    This removes all existing permissions and replacesthem with read permission for group and others andread & write permission for owner of the file file1.

    Ex:$ chmod ugo=rwx file2This removes all existing permissions and replaces

    them with full permissions for owner, group and others.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    26/34

    Masking File Permissions26

    How come that the file permissions for file1 file havebeen set to 644 ?How come that the folder permissions for folder1 havebeen set to 755 ?

    What unix does is it uses the value stored in a variablecalled umask to decide the default permissions.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    27/34

    Masking File Permissions (contd)27Umask stands for user file creation mask, the term

    mask implying which permissions to mask or hide.The umask value tells unix which of the threepermissions are to be denied rather than granted.

    The current value of umask can be easilydetermined by just typing umask.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    28/34

    Masking File Permissions (contd)28Whenever a file is created Unix assumes that thepermissions for this file should be 666.But since our umask value is 022, Unix subtracts thisvalue from the default system wide permissions (666)resulting in a value 644.

    This value is then used as the permissions for the filethat you create.Similarly system-wide default permissions for adirectory are 777.

    This means that when we create a directory itspermissions would be 777 022, i.e. 755.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    29/34

    Masking File Permissions (contd)29If a directory doesnt have an execute permission

    we can never enter into it.We can change current umask value using umaskcommand

    $ umask 242

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    30/34

    Directory Related Commands30pwd stands for present working directory

    mkdir to create an empty directory.

    mkdir p , allows you to create multiplegenerations of directories, at one go.That means, it creates all the parent directories

    specified in the given path too.Ex:$ mkdir p study/hacktivism/books

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    31/34

    Useful commands31Unix knows each user not only by the login name, but

    also by two numbers called the user and group identitynumbers.id to show identity numbersuname to find the name of the Unix system.tty to find out the name of your terminal filewhoami to find some information about yourself.who to see who all are currently sharing the networkwith you.date to display the current date and time.

    http://studyhacktivism.blogspot.com

  • 8/8/2019 Shell Programming 1

    32/34

    Useful commands (contd)32

    http://studyhacktivism.blogspot.com

    h // d h k i i bl

  • 8/8/2019 Shell Programming 1

    33/34

    Reference books33

    Unix Shell Programming--by Yeshavant P. Kanetkar

    http://studyhacktivism.blogspot.com

    htt // t d h kti i bl g t

  • 8/8/2019 Shell Programming 1

    34/34

    Questions ?34

    http://studyhacktivism.blogspot.com