unit1 material

Upload: vamsinath-javangula

Post on 28-Feb-2018

258 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Unit1 Material

    1/96

    "Unit-I - Linux Utilities"

    Introduction to Linux

    Linux is a Unix-like computer operating systemassembled under the model of free and open

    source software development and distribution. The defining component of Linux is the Linux

    kernel, an operating system kernelfirst released !ctober "##" by Linus Torvalds.

    Linux was originally developed as a free operating system for Intel x$%-based personal

    computers. It has since been ported to more computer hardware platforms than any other

    operating system. It is a leading operating system on serversand otherbig ironsystems such as

    mainframe computersand supercomputersmore than #&' of today(s && fastest supercomputers

    run some variant of Linux, including the "& fastest. Linux also runs on embedded systems

    )devices where the operating system is typically built into the firmwareand highly tailored to the

    system* such as mobile phones, tablet computers, network routers, televisions and video game

    consoles+ the ndroidsystem in wide use on mobile devices is built on the Linux kernel.

    distribution oriented toward desktop use will typically include the indow /ystemand an

    accompanying desktop environmentsuch as 01!23or 453 6lasma. /ome such distributions

    may include a less resource intensive desktop such as L53or fcefor use on older or lesspowerful computers. distribution intended to run as a server may omit all graphical

    environments from the standard install and instead include other software such as the pache

    7TT6 /erver and an //7 server such as !pen//7.8ecause Linux is freely redistributable,

    anyone may create a distribution for any intended use. pplications commonly used with desktop

    Linux systems include the 2o9illa :irefoxweb browser, the Libre!fficeoffice application suite,

    and the 0I26image editor.

    /ince the main supporting user spacesystem tools and libraries originated in the 01U 6ro;ect,

    initiated in "#$< by =ichard /tallman, the :ree /oftware :oundation prefers the name

    GNU/Linux.

    7istory

    http://en.wikipedia.org/wiki/Unix-likehttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Free_and_open_source_softwarehttp://en.wikipedia.org/wiki/Free_and_open_source_softwarehttp://en.wikipedia.org/wiki/Free_and_open_source_softwarehttp://en.wikipedia.org/wiki/Linux_kernelhttp://en.wikipedia.org/wiki/Linux_kernelhttp://en.wikipedia.org/wiki/Operating_system_kernelhttp://en.wikipedia.org/wiki/Linus_Torvaldshttp://en.wikipedia.org/wiki/Linus_Torvaldshttp://en.wikipedia.org/wiki/Intel_x86http://en.wikipedia.org/wiki/Portinghttp://en.wikipedia.org/wiki/Server_(computing)http://en.wikipedia.org/wiki/Big_ironhttp://en.wikipedia.org/wiki/Big_ironhttp://en.wikipedia.org/wiki/Mainframe_computerhttp://en.wikipedia.org/wiki/Mainframe_computerhttp://en.wikipedia.org/wiki/Supercomputerhttp://en.wikipedia.org/wiki/TOP500http://en.wikipedia.org/wiki/Embedded_systemhttp://en.wikipedia.org/wiki/Firmwarehttp://en.wikipedia.org/wiki/Tablet_computerhttp://en.wikipedia.org/wiki/Router_(computing)http://en.wikipedia.org/wiki/Video_game_consolehttp://en.wikipedia.org/wiki/Video_game_consolehttp://en.wikipedia.org/wiki/Android_(operating_system)http://en.wikipedia.org/wiki/Android_(operating_system)http://en.wikipedia.org/wiki/X_Window_Systemhttp://en.wikipedia.org/wiki/Desktop_environmenthttp://en.wikipedia.org/wiki/GNOMEhttp://en.wikipedia.org/wiki/GNOMEhttp://en.wikipedia.org/wiki/KDE_Plasma_Workspaceshttp://en.wikipedia.org/wiki/LXDEhttp://en.wikipedia.org/wiki/Xfcehttp://en.wikipedia.org/wiki/Apache_HTTP_Serverhttp://en.wikipedia.org/wiki/Apache_HTTP_Serverhttp://en.wikipedia.org/wiki/SSH_serverhttp://en.wikipedia.org/wiki/OpenSSHhttp://en.wikipedia.org/wiki/OpenSSHhttp://en.wikipedia.org/wiki/Mozilla_Firefoxhttp://en.wikipedia.org/wiki/LibreOfficehttp://en.wikipedia.org/wiki/LibreOfficehttp://en.wikipedia.org/wiki/GIMPhttp://en.wikipedia.org/wiki/User_spacehttp://en.wikipedia.org/wiki/User_spacehttp://en.wikipedia.org/wiki/GNU_Projecthttp://en.wikipedia.org/wiki/Richard_Stallmanhttp://en.wikipedia.org/wiki/Free_Software_Foundationhttp://en.wikipedia.org/wiki/Free_Software_Foundationhttp://en.wikipedia.org/wiki/GNU/Linux_naming_controversyhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Free_and_open_source_softwarehttp://en.wikipedia.org/wiki/Free_and_open_source_softwarehttp://en.wikipedia.org/wiki/Linux_kernelhttp://en.wikipedia.org/wiki/Linux_kernelhttp://en.wikipedia.org/wiki/Operating_system_kernelhttp://en.wikipedia.org/wiki/Linus_Torvaldshttp://en.wikipedia.org/wiki/Intel_x86http://en.wikipedia.org/wiki/Portinghttp://en.wikipedia.org/wiki/Server_(computing)http://en.wikipedia.org/wiki/Big_ironhttp://en.wikipedia.org/wiki/Mainframe_computerhttp://en.wikipedia.org/wiki/Supercomputerhttp://en.wikipedia.org/wiki/TOP500http://en.wikipedia.org/wiki/Embedded_systemhttp://en.wikipedia.org/wiki/Firmwarehttp://en.wikipedia.org/wiki/Tablet_computerhttp://en.wikipedia.org/wiki/Router_(computing)http://en.wikipedia.org/wiki/Video_game_consolehttp://en.wikipedia.org/wiki/Video_game_consolehttp://en.wikipedia.org/wiki/Android_(operating_system)http://en.wikipedia.org/wiki/X_Window_Systemhttp://en.wikipedia.org/wiki/Desktop_environmenthttp://en.wikipedia.org/wiki/GNOMEhttp://en.wikipedia.org/wiki/KDE_Plasma_Workspaceshttp://en.wikipedia.org/wiki/LXDEhttp://en.wikipedia.org/wiki/Xfcehttp://en.wikipedia.org/wiki/Apache_HTTP_Serverhttp://en.wikipedia.org/wiki/Apache_HTTP_Serverhttp://en.wikipedia.org/wiki/SSH_serverhttp://en.wikipedia.org/wiki/OpenSSHhttp://en.wikipedia.org/wiki/Mozilla_Firefoxhttp://en.wikipedia.org/wiki/LibreOfficehttp://en.wikipedia.org/wiki/GIMPhttp://en.wikipedia.org/wiki/User_spacehttp://en.wikipedia.org/wiki/GNU_Projecthttp://en.wikipedia.org/wiki/Richard_Stallmanhttp://en.wikipedia.org/wiki/Free_Software_Foundationhttp://en.wikipedia.org/wiki/GNU/Linux_naming_controversyhttp://en.wikipedia.org/wiki/Unix-like
  • 7/25/2019 Unit1 Material

    2/96

    Unix

    The Unix operating system was conceived and implemented in "#%# at T>T(s 8ell

    Laboratories in the United /tates by 4en Thompson,5ennis =itchie,5ouglas 2cIlroy, and ?oe

    !ssanna. It was first released in "#@" and was initially entirely written in assembly language, a

    common practice at the time. Later, in a key pioneering approach in "#@

  • 7/25/2019 Unit1 Material

    3/96

    2ost Linux distributions support do9ens of programming languages. The original development

    tools used for building both Linux applications and operating system programs are found within

    the 01U toolchain, which includes the 01U Aompiler Aollection)0AA* and the 01U build

    system. mongst others, 0AA provides compilers for da, A, ACC, ?ava, and :ortran. :irst

    released in D&&

  • 7/25/2019 Unit1 Material

    4/96

    7. $oice:The large number of Linux distributions gives you a choice. 3ach distribution is

    developed and supported by a different organi9ation. Gou can pick the one you like best+ the

    core functionalities are the same+ most software runs on most distributions.

    8. !ast and easy installation:2ost Linux distributions come with user-friendly installation

    and setup programs. 6opular Linux distributions come with tools that make installation of

    additional software very user friendly as well.9. !ull use of $ard dis:Linux continues work well even when the hard disk is almost full.

    10. %ultitasing:Linux is designed to do many things at the same time+ e.g., a large printing

    ;ob in the background wonHt slow down your other work.

    11. Security:Linux is one of the most secure operating systems. JallsK and flexible file

    access permission systems prevent access by unwanted visitors or viruses. Linux users have

    to option to select and safely download software, free of charge, from online repositories

    containing thousands of high uality packages. 1o purchase transactions reuiring credit

    card numbers or other sensitive personal information are necessary.

    12. en Source:If you develop software that reuires knowledge or modification of the

    operating system code, LinuxHs source code is at your fingertips. 2ost Linux applicationsare !pen /ource as well.

    '$e difference between Linux and UNI( o#erating systems)

    U1I is copyrighted name only big companies are allowed to use the U1I copyright and

    name, so I82 I and /un /olaris and 76-U all are U1I operating systems. The !pen

    0roup holdsthe U1I trademark in trust for the industry, and manages the U1I trademark

    licensing program.

    2ost U1I systems are commercial in nature.

    Linux is a UNI( lone

    8ut if you consider 6ortable !perating /ystem Interface )6!/I* standards then Linux can be

    considered as U1I. To uote from !fficial Linux kernel =3523 file

    Linux is a Unix clone written from scratch by Linus Torvalds with assistance from a loosely-knit

    team of hackers across the 1et. It aims towards 6!/I compliance.

    7owever, M!pen 0roupM do not approve of the construction MUnix-likeM, and consider it misuse

    of their U1I trademark.

    Linux Is *ust a +ernel

    http://www.unix.org/http://www.unix.org/http://www.unix.org/http://www.unix.org/
  • 7/25/2019 Unit1 Material

    5/96

    Linux is ;ust a kernel. ll Linux distributions includes 0UI system C 01U utilities )such as cp,

    mv, ls,date, bash etc* C installation > management tools C 01U cBcCC Aompilers C 3ditors )vi*

    C and various applications )such as !pen!ffice, :irefox*. 7owever, most U1I operating

    systems are considered as a complete operating system as everything come from a single source

    or vendor.

    s I said earlier Linux is ;ust a kernel and Linux distribution makes it complete usable operating

    systems by adding various applications. 2ost U1I operating systems comes with -N

    programs such as editor, compilers etc. :or example 76-U or /olaris comes with -N

    programs.

    License and cost

    Linux is :ree )as in beer OfreedomP*. Gou can download it from the Internet or redistribute it

    under 01U licenses. Gou will see the best community support for Linux. 2ost U1I like

    operating systems are not free )but this is changing fast, for example !pen/olaris U1I*.

    7owever, some Linux distributions such as =edhat B 1ovell provides additional Linux support,

    consultancy, bug fixing, and training for additional fees.

    User-!riendly

    Linux is considered as most user friendly U1I like operating systems. It makes it easy to install

    sound card, flash players, and other desktop goodies. 7owever, pple !/ is most popular

    U1I operating system for desktop usage.

    Security !irewall Software

    Linux comes with open source netfilterBiptables based firewall tool to protect your server and

    desktop from the crackers and hackers. U1I operating systems comes with its own firewall

    product )for example /olaris U1I comes with ipfilter based firewall* or you need to purchase a

  • 7/25/2019 Unit1 Material

    6/96

    U1I comes with various file systems such as ;fs, gpfs )I*, ;fs, gpfs )76-U*, ;fs,

    gpfs )/olaris*.

    System Administration 'ools

    ". U1I comes with its own tools such as /2 on 76-U.

    D. /use Linux comes with Gast

  • 7/25/2019 Unit1 Material

    7/96

    few popular names

    ". 76-U

    D. I82 I

  • 7/25/2019 Unit1 Material

    8/96

    Gou can look at this from both sides of the fence. /ome say giving the public access to the code

    opens the operating system )and the software that runs on top of it* to malicious developers who

    will take advantage of any weakness they find. !thers say that having full access to the code

    helps bring about faster improvements and bug fixes to keep those malicious developers from

    being able to bring the system down. I have, on occasion, dipped into the code of one Linux

    application or another, and when all was said and done, was happy with the results. Aould I have

    done that with a closed-source indows applicationS 1o.

    03: Licensing freedom vs2 licensing restrictions

    long with access comes the difference between the licenses. IHm sure that every IT professional

    could go on and on about licensing of 6A software. 8ut letHs ;ust look at the key aspect of the

    licenses )without getting into legalese*. ith a Linux 06L-licensed operating system, you are

    free to modify that software and use and even republish or sell it )so long as you make the code

    available*. lso, with the 06L, you can download a single copy of a Linux distribution )or

    application* and install it on as many machines as you like. ith the 2icrosoft license, you cando none of the above. Gou are bound to the number of licenses you purchase, so if you purchase

    "& licenses, you can legally install that operating system )or application* on only "& machines.

    04: &nline #eer su##ort vs2 #aid $el#-des su##ort

    This is one issue where most companies turn their backs on Linux. 8ut itHs really not necessary.

    ith Linux, you have the support of a huge community via forums, online search, and plenty of

    dedicated eb sites. nd of course, if you feel the need, you can purchase support contracts from

    some of the bigger Linux companies )=ed 7at and 1ovell for instance*.

    7owever, when you use the peer support inherent in Linux, you do fall prey to time. Gou could

    have an issue with something, send out e-mail to a mailing list or post on a forum, and within "&

    minutes be flooded with suggestions. !r these suggestions could take hours of days to come in. It

    seems all up to chance sometimes. /till, generally speaking, most problems with Linux have

    been encountered and documented. /o chances are good youHll find your solution fairly uickly.

    !n the other side of the coin is support for indows. Ges, you can go the same route with

    2icrosoft and depend upon your peers for solutions. There are ;ust as many help

    sitesBlistsBforums for indows as there are for Linux. nd you can purchase support from

    2icrosoft itself. 2ost corporate higher-ups easily fall victim to the safety net that having a

    support contract brings. 8ut most higher-ups havenHt had to depend up on said support contract.

    !f the various people I know who have used either a Linux paid support contract or a 2icrosoft

    paid support contract, I canHt say one was more pleased than the other. This of course begs the

    uestion Jhy do so many say that 2icrosoft support is superior to Linux paid supportSK

    05: !ull vs2 #artial $ardware su##ort

  • 7/25/2019 Unit1 Material

    9/96

    !ne issue that is slowly becoming nonexistent is hardware support. Gears ago, if you wanted to

    install Linux on a machine you had to make sure you hand-picked each piece of hardware or

    your installation would not work "&& percent. I can remember, back in "##@-ish, trying to figure

    out why I couldnHt get Aaldera Linux or =ed 7at Linux to see my modem. fter much looking

    around, I found I was the proud owner of a inmodem. /o I had to go out and purchase a U/

    =obotics external modem because that was the one modem I knewwould work. This is not so

    much the case now. Gou can grab a 6A )or laptop* and most likely get one or more Linux

    distributions to install and work nearly "&& percent. 8ut there are still some exceptions. :or

    instance, hibernateBsuspend remains a problem with many laptops, although it has come a long

    way.

    ith indows, you know that most every piece of hardware will work with the operating

    system. !f course, there are times )and I have experienced this over and over* when you will

    wind up spending much of the day searching for the correct drivers for that piece of hardware

    you no longer have the install disk for. 8ut you can go out and buy that "&-cent 3thernet card and

    know itHll work on your machine )so long as you have, or can find, the drivers*. Gou also can restassured that when you purchase that insanely powerful graphics card, you will probably be able

    to take full advantage of its power.

    06: ommand line vs2 no command line

    1o matter how far the Linux operating system has come and how ama9ing the desktop

    environment becomes, the command line will always be an invaluable tool for administration

    purposes. 1othing will ever replace my favorite text-based editor, ssh, and any given command-

    line tool. I canHt imagine administering a Linux machine without the command line. 8ut for the

    end user not so much. Gou could use a Linux machine for years and never touch the command

    line. /ame with indows. Gou can still use the command line with indows, but not nearly to

    the extent as with Linux. nd 2icrosoft tends to obfuscate the command prompt from users.

    ithout going to =un and entering cmd )or command, or whichever it is these days*, the user

    wonHt even know the command-line tool exists. nd if a user does get the indows command

    line up and running, how useful is it reallyS

    07: entrali8ed vs2 noncentrali8ed a##lication installation

    The heading for this point might have thrown you for a loop. 8ut letHs think about this for a

    second. ith Linux you have )with nearly every distribution* a centrali9ed location where you

    can search for, add, or remove software. IHm talking about package management systems, such as/ynaptic. ith /ynaptic, you can open up one tool, search for an application )or group of

    applications*, and install that application without having to do any eb searching )or

    purchasing*.

    indows has nothing like this. ith indows, you must know where to find the software you

    want to install, download the software )or put the A5 into your machine*, and run setup.exe or

  • 7/25/2019 Unit1 Material

    10/96

    install.exe with a simple double-click. :or many years, it was thought that installing applications

    on indows was far easier than on Linux. nd for many years, that thought was right on target.

    1ot so much now. Installation under Linux is simple, painless, and centrali9ed.

    09: !lexibility vs2 rigidity

    I always compare Linux )especially the desktop* and indows to a room where the floor and

    ceiling are either movable or not. ith Linux, you have a room where the floor and ceiling can

    be raised or lowered, at will, as high or low as you want to make them. ith indows, that floor

    and ceiling are immovable. Gou canHt go further than 2icrosoft has deemed it necessary to go.

    Take, for instance, the desktop. Unless you are willing to pay for and install a third-party

    application that can alter the desktop appearance, with indows you are stuck with what

    2icrosoft has declared is the ideal desktop for you. ith Linux, you can pretty much make your

    desktop look and feel exactly how you wantBneed. Gou can have as much or as little on your

    desktop as you want. :rom simple flat :luxbox to a full-blown

  • 7/25/2019 Unit1 Material

    11/96

    drive to access the contents of a A5 is completely foreign to new users. There is a reason this is

    the way it is. 8ecause Linux has always been a multiuser platform, it was thought that forcing a

    user to mount a media to use it would keep the userHs files from being overwritten by another

    user. Think about it !n a multiuser system, if everyone had instant access to a disk that had been

    inserted, what would stop them from deleting or overwriting a file you had ;ust added to the

    mediaS Things have now evolved to the point where Linux subsystems are set up so that you can

    use a removable device in the same way you use them in indows. 8ut itHs not the norm. nd

    besides, who doesnHt want to manually edit the/etc/fstabfleS

    01

  • 7/25/2019 Unit1 Material

    12/96

    - /how all.

    -b !mits line numbers for blank space in the output.

    -e character will be printed at the end of each line prior to a new line.

    -3 5isplays a )dollar sign* at the end of each line.

    -n Line numbers for all the output lines.

    -s If the output has multiple empty lines it replaces it with one empty line.

    -T 5isplays the tab characters in the output.

    -v1on-printing characters )with the exception of tabs, new-lines and form-

    feeds* are printed visibly.

    .(A%PL.:

    ". To Areate a new file

    cat V file".txt

    This command creates a new file file".txt. fter typing into the file press controlCd )Wd*

    simultaneously to end the file.

    D. To ppend data into the file

    cat VV file".txt

    To append data into the same file use append operator VV to write into the file, else the

    file will be overwritten )i.e., all of its contents will be erased*.

  • 7/25/2019 Unit1 Material

    13/96

    such situation you can print those files in a new file or display the file using less

    command.

    cat file".txt fileD.txt X less

    . To concatenate several files and to transfer the output to another file.

    cat file".txt fileD.txt V file

  • 7/25/2019 Unit1 Material

    14/96

    D. To delete a directory tree

    rm -ir tmp

    This rm command recursively removes the contents of all subdirectories of the tmpdirectory, prompting you regarding the removal of each file, and then removes the tmp

    directory itself.

    :

    cd command is used to change the directory.

    S?N'A(:

    The /yntax is

    cd Odirectory X Y X .B X ..B X - P

    &P'I&NS:

    -L Use the physical directory structure.

    -6 :orces symbolic links.

    .(A%PL.:

    ". cd linux-command

    This command will take you to the sub-directory)linux-command* from its parent

    directory.

    D. cd 22

    This will change to the parent-directory from the current working directoryBsub-directory.

  • 7/25/2019 Unit1 Material

    15/96

    : cp command copy files from one location to another. If the destination is an existing file, then

    the file is overwritten+ if the destination is an existing directory, the file is copied into the

    directory )the directory is not overwritten*.

    S?N'A(:

    The /yntax is

    cp O!6TI!1/P... /!U=A3 53/T

    cp O!6TI!1/P... /!U=A3... 5I=3AT!=G

    cp O!6TI!1/P... --target-directoryZ5I=3AT!=G /!U=A3...

    &P'I&NS:

    -a same as -dp=.

    --backupOZA!1T=!LP make a backup of each existing destination file

    -b like --backup but does not accept an argument.

    -fif an existing destination file cannot be opened, remove it and try

    again.

    -p same as --preserveZmode,ownership,timestamps.

    --

    preserveOZTT=[LI/TP

    preserve the specified attributes )default

    mode,ownership,timestamps* and security contexts, if possible

    additional attributes links, all.

    --no-

    preserveZTT=[LI/Tdon(t preserve the specified attribute.

    --parents append source path to 5I=3AT!=G..(A%PL.:

    ". Aopy two files

    cp file" fileD

  • 7/25/2019 Unit1 Material

    16/96

    The above cp command copies the content of file".php to fileD.php.

    D. To backup the copied file

    cp -b file".php fileD.php

    8ackup of file".php will be created with (Y( symbol as fileD.phpY.

    :

    ls command lists the files and directories under current working directory.

    S?N'A(:

    The /yntax is

    ls O!6TI!1/P... O:IL3P

    &P'I&NS:

    -lLists all the files, directories and their mode, 1umber of links, owner of the

    file, file si9e, 2odified date and time and filename.

    -t Lists in order of last modification time.

    -a Lists all entries including hidden files.

    -d Lists directory files instead of contents.

    -p 6uts slash at the end of each directories.-u List in order of last access time.

    -i 5isplay inode information.

    -ltr List files order by date.

    -l/r List files order by file si9e.

    .(A%PL.:

  • 7/25/2019 Unit1 Material

    17/96

    ". 5isplay root directory contents

    ls B

    lists the contents of root directory.

    D. 5isplay hidden files and directories

    ls -a

    lists all entries including hidden files and directories.

  • 7/25/2019 Unit1 Material

    18/96

    &P'I&NS:

    -fLink files without uestioning the user, even if the mode of target forbids

    writing. This is the default if the standard input is not a terminal.

    -n 5oes not overwrite existing files.

    -s Used to create soft links.

    .(A%PL.:

    ". ln -s file".txt fileD.txt

    Areates a symbolic link to (file".txt( with the name of (fileD.txt(. 7ere inode for (file".txt(

    and (fileD.txt( will be different.

    D. ln -s nimi nimi"

    Areates a symbolic link to (nimi( with the name of (nimi"(.

    c$own &%%AN>:

    chown command is used to change the owner B user of the file or directory. This is an admin

    command, root user only can change the owner of a file or directory.

    S?N'A(:

    The /yntax is

    chown OoptionsP newowner filenameBdirectoryname

    &P'I&NS:

    -=

    Ahange the permission on files that are in the subdirectories of the directory

    that you are currently in.

    -c Ahange the permission for each file.

    -f6revents chown from displaying error messages when it is unable to change

    the ownership of a file.

    .(A%PL.:

  • 7/25/2019 Unit1 Material

    19/96

    ". chown hiox test.txt

    The owner of the (test.txt( file is root, Ahange to new user hiox.

    D. chown -= hiox test

    The owner of the (test( directory is root, ith -= option the files and subdirectories user

    also gets changed.

    :

    chmod command allows you to alter B Ahange access rights to files and directories.

    !ile Permission is given for users@grou# and ot$ers as@

    ead rite .xecute

    User

    Brou#

    &t$ers

    Permission 000

    Symbolic %ode ___ ___ __

    S?N'A(:

    The /yntax is

    chmod OoptionsP O2!53P :ile1ame

    !ile Permission

    F :ile 6ermission

  • 7/25/2019 Unit1 Material

    20/96

    & none

    " execute only

    D write only

    < write and execute

    Q read only

    read and execute

    % read and write

    @ set all permissions

    &P'I&NS:

    -c 5isplays names of only those files whose permissions are being changed

    -f /uppress most error messages-= Ahange files and directories recursively

    -v !utput version information and exit.

    .(A%PL.:

    ". To view your files with what permission they are

    ls -alt

    This command is used to view your files with what permission they are.

    D. To make a file readable and writable by the group and others.

    chmod &%% file".txt

  • 7/25/2019 Unit1 Material

    21/96

    &P'I&NS:

    -m /et the access mode for the new directories.

    -p Areate intervening parent directories if they don(t exist.

    -v 6rint help message for each directory created.

    .(A%PL.:

    ". Areate directory

    mkdir test

    The above command is used to create the directory (test(.

    D. Areate directory and set permissions

    mkdir -m %%% test

    The above command is used to create the directory (test( and set the read and write

    permission.

    rmdir &%%AN>:

    rmdir command is used to deleteBremove a directory and its subdirectories.

    S?N'A(:

    The /yntax is

    rmdir Ooptions..P 5irectory

    &P'I&NS:

    -pllow users to remove the directory dirname and its parent directories which

    become empty.

    .(A%PL.:

  • 7/25/2019 Unit1 Material

    22/96

    ". To deleteBremove a directory

    rmdir tmp

    rmdir command will removeBdelete the directory tmp if the directory is empty.

    D. To delete a directory tree

    rm -ir tmp

    This command recursively removes the contents of all subdirectories of the tmp directory,

    prompting you regarding the removal of each file, and then removes the tmp directory

    itself.

    mv &%%AN>:

    mv command which is short for move. It is used to moveBrename file from one directory to

    another. mv command is different from cp command as it completely removes the file from the

    source and moves to the directory specified, where cp command ;ust copies the content from one

    file to another.

    S?N'A(: The /yntax is

    mv O-fP O-iP oldname newname

    &P'I&NS:

    -fThis will not prompt before overwriting )euivalent to --replyZyes*. mv -f will

    move the file)s* without prompting even if it is writing over an existing target.

    -i 6rompts before overwriting another file..(A%PL.:

    ". To =ename B 2ove a file

    mv file".txt fileD.txt

  • 7/25/2019 Unit1 Material

    23/96

    This command renames file".txt as fileD.txt

    D. To move a directory

    mv hscripts tmp

    In the above line mv command moves all the files, directories and sub-directories from

    hscripts folderBdirectory to tmp directory if the tmp directory already exists. If there is no

    tmp directory it rename(s the hscripts directory as tmp directory.

    :

    diff command is used to find differences between two files.

    S?N'A(:

    The /yntax is

    diff Ooptions..P from-file to-file

    &P'I&NS:

    -a Treat all files as text and compare them line-by-line.

    -b Ignore changes in amount of white space.

    -c Use the context output format.

    -e 2ake output that is a valid ed script.

    -7Use heuristics to speed handling of large files that have numerous scattered

    small changes.

    -i Ignore changes in case+ consider upper- and lower-case letters euivalent.

    -n6rints in =A/-format, like -f except that each command specifies the number

    of lines affected.

  • 7/25/2019 Unit1 Material

    24/96

    -!utput =A/-format diffs+ like -f except that each command specifies the

    number of lines affected.

    -r hen comparing directories, recursively compare any subdirectories found.

    -s =eport when two files are the same.

    -w Ignore white space when comparing lines.-y Use the side by side output format.

    .(A%PL.:

    Lets create two files file".txt and fileD.txt and let it have the following data.

    5ata in file".txt 5ata in fileD.txt

    7I! T3/T

    hscripts.comwith friend ship

    hiox india

    7I! T3/T

    7/A=I6T/.com

    with friend ship

    ". Aompare files ignoring white space

    diff -w file".txt fileD.txt

    This command will compare the file file".txt with fileD.txt ignoring whiteBblank space

    and it will produce the following output.

    DcD

    \ hscripts.com

    ---

    V 7/A=I6T/.com

    Qd:

    more command is used to display text in the terminal screen. It allows only backward

    movement.

    S?N'A(:

    The /yntax is

    more OoptionsP filename

    &P'I&NS:

    -c Alear screen before displaying.

    -e 3xit immediately after writing the last line of the last file in the argument list.

    -n /pecify how many lines are printed in the screen for a given file.

    Cn /tarts up the file from the given number.

    .(A%PL.:

    ". more -c index.php

    Alears the screen before printing the file .

    D. more -< index.php

    6rints first three lines of the given file. 6ress.nterto display the file line by line.

    $ead &%%AN>:

    head command is used to display the first ten lines of a file, and also specifies how many lines

    http://www.computerhope.com/unix/ucrontab.htmhttp://www.computerhope.com/unix/ucrontab.htmhttp://www.computerhope.com/unix/ucrontab.htm
  • 7/25/2019 Unit1 Material

    36/96

    to display.

    S?N'A(:

    The /yntax is

    head OoptionsP filename

    &P'I&NS:

    -n To specify how many lines you want to display.

    -n numberThe number option-argument must be a decimal integer whose sign affects

    the location in the file, measured in lines.

    -c numberThe number option-argument must be a decimal integer whose sign affects

    the location in the file, measured in bytes.

    .(A%PL.:

    ". head index.php

    This command prints the first "& lines of (index.php(.

    D. head - index.php

    The head command displays the first lines of (index.php(.

    :

    tail command is used to display the last or bottom part of the file. 8y default it displays last

    "& lines of a file.

    S?N'A(:

    The /yntax is

    tail OoptionsP filename

  • 7/25/2019 Unit1 Material

    37/96

    &P'I&NS:

    -l To specify the units of lines.

    -b To specify the units of blocks.

    -n To specify how many lines you want to display.

    -c numberThe number option-argument must be a decimal integer whose sign affects the

    location in the file, measured in bytes.

    -n numberThe number option-argument must be a decimal integer whose sign affects the

    location in the file, measured in lines.

    .(A%PL.:

    ". tail index.php

    It displays the last "& lines of (index.php(.

    D. tail -D index.php

    It displays the last D lines of (index.php(.

    :

    cut command is used to cut out selected fields of each line of a file. The cut command uses

    delimiters to determine where to split fields.

    S?N'A(:

    The /yntax is

    cut OoptionsP

  • 7/25/2019 Unit1 Material

    38/96

    &P'I&NS:

    -c /pecifies character positions.

    -b /pecifies byte positions.

    -d flags /pecifies the delimiters and fields.

    .(A%PL.:

    ". cut -c"-< text.txt

    &ut#ut:

    Thi

    Aut the first three letters from the above line.

    D. cut -d, -f",D text.txt

    &ut#ut:

    This is, an example program

    The above command is used to split the fields using delimiter and cut the first two fields.

    #aste &%%AN>:

    paste command is used to paste the content from one file to another file. It is also used to set

    column format for each line.

    S?N'A(:

    The /yntax is

    paste OoptionsP

    &P'I&NS:

    -s 6aste one file at a time instead of in parallel.

    -d =euse characters from LI/T instead of T8s .

  • 7/25/2019 Unit1 Material

    39/96

    .(A%PL.:

    ". paste test.txtVtest".txt

    6aste the content from (test.txt( file to (test".txt( file.

    D. ls X paste - - - -

    List all files and directories in four columns for each line.

    sort &%%AN>:

    sort command is used to sort the lines in a text file.

    S?N'A(:

    The /yntax is

    sort OoptionsP filename

    &P'I&NS:

    -r /orts in reverse order.

    -u If line is duplicated display only once.-o filename /ends sorted output to a file.

    .(A%PL.:

    ". sort test.txt

    /orts the (test.txt(file and prints result in the screen.

    D. sort -r test.txt

    /orts the (test.txt( file in reverse order and prints result in the screen.

    bout uni

    =eport or filter out repeated lines in a file.

  • 7/25/2019 Unit1 Material

    40/96

    /yntax

    uni# [-c | -" | -u ] [ -f fiel"s ] [ -s car ] [-n] [$m] [input!file [ output!file ] ]

    -c 6recede each output line with a count of the number of times the line occurred in

    the input.

    -d /uppress the writing of lines that are not repeated in the input.

    -u /uppress the writing of lines that are repeated in the input.

    -f fields Ignore the first fields fields on each input line when doing comparisons, where

    fields is a positive decimal integer. field is the maximal string matched by the

    basic regular expression

    OOblankPPROWOblankPPR

    If fields specifies more fields than appear on an input line, a null string will be

    used for comparison.

    -s char Ignore the first chars characters when doing comparisons, where chars is a

    positive decimal integer. If specified in con;unction with the -f option, the first

    chars characters after the first fields fields will be ignored. If chars specifies more

    characters than remain on an input line, a null string will be used for comparison.-n 3uivalent to -f fields with fields set to n.

    Cm 3uivalent to -s chars with chars set to m.

    input[file path name of the input file. If input[file is not specified, or if the input[file is -,

    the

    standard input will be used.

    output[file path name of the output file. If output[file is not specified, the standard output

    will be used. The results are unspecified if the file named by output[file is the

    file named by input[file.

    3xamples

    uniG myfile12txt H myfile32txt- =emoves duplicate lines in the first file".txt and outputs the

    results to the second file.

  • 7/25/2019 Unit1 Material

    41/96

    bout tr

    Translate characters.

    /yntax

    tr [-c] [-"] [-s] [string%] [string&]

    -c Aomplement the set of characters specified by string".

    -d 5elete all occurrences of input characters that are specified by string".

    -s =eplace instances of repeated characters with a single character.

    string" :irst string or character to be changed.

    stringD /econd string or character to change the string".

    3xamples

    ec$o "1345679 ;359" tr 1345679; com#uter$- this example takes an echo response of

    ("D

  • 7/25/2019 Unit1 Material

    42/96

    -a

    /lowly ad;ust the time by sss.fff seconds )fff represents fractions of a second*.

    This ad;ustment can be positive or negative.!nly system adminB super user

    can ad;ust the time.

    -s date

    -string

    /ets the time and date to the value specfied in the datestring. The datestr may

    contain the month names, time9ones, (am(, (pm(, etc.

    -u 5isplay )or set* the date in 0reenwich 2ean Time )02T-universal time*.

    !ormat:

    'a bbreviated weekday)Tue*.

    ' :ull weekday)Tuesday*.

    'b bbreviated month name)?an*.

    '8 :ull month name)?anuary*.

    'c Aountry-specific date and time format..

    '5 5ate in the format 'mB'dB'y.

    '; ?ulian day of year )&&"-

  • 7/25/2019 Unit1 Material

    43/96

    The above command will remove space and print as

    5ate is &@BD

  • 7/25/2019 Unit1 Material

    44/96

    .(A%PL.:

    ". who -u7

    &ut#ut:

    123 LI13 TI23 I5L3 6I5 A!2231T

    hiox ttyp< ?ul "& ""&$ . Q@$

    This sample output was produced at "" a.m. The M.M indiacates activity within the last

    minute.

    D. who am i

    who am i command prints the user name.

    ec$o &%%AN>:

    echo command prints the given input string to standard output.

    S?N'A(:

    The /yntax isecho Ooptions..P OstringP

    &P'I&NS:

    -n do not output the trailing newline

    -e enable interpretation of the backslash-escaped characters listed below

    -3 disable interpretation of those seuences in /T=I10s

    ithout -3, the following seuences are recogni9ed and interpolated

    ]111the character whose /AII code is 111

    )octal*

    ]a alert )83L*

    ]] backslash

  • 7/25/2019 Unit1 Material

    45/96

    ]b backspace

    ]c suppress trailing newline

    ]f form feed

    ]n new line

    ]r carriage return

    ]t hori9ontal tab

    ]v vertical tab

    .(A%PL.:

    ". echo command

    echo Mhscripts 7iox IndiaM

    The above command will print as hscripts 7iox India

    D. To use backspace

    echo -e Mhscripts ]b7iox ]bIndiaM

    The above command will remove space and print as hscripts7ioxIndia

    :

    passwd command is used to change your password.

    S?N'A(:

    The /yntax is

    passwd OoptionsP

  • 7/25/2019 Unit1 Material

    46/96

    &P'I&NS:

    -a /how password attributes for all entries.

    -l Locks password entry for name.

    -d 5eletes password for name. The login name will not be prompted for

    password.

    -f:orce the user to change password at the next login by expiring the password

    for name.

    .(A%PL.:

    ". passwd

    3ntering ;ust passwd would allow you to change the password. fter entering passwd youwill receive the following three prompts

    Aurrent 6assword

    1ew 6assword

    Aonfirm 1ew 6assword

    3ach of these prompts must be entered correctly for the password to be successfully

    changed.

    #wd &%%AN>:

    pwd - 6rint orking 5irectory. pwd command prints the full filename of the current working

    directory.

    S?N'A(:

    The /yntax is

    pwd OoptionsP

    &P'I&NS:

    -6 The pathname printed will not contain symbolic links.

    -L The pathname printed may contain symbolic links.

  • 7/25/2019 Unit1 Material

    47/96

    .(A%PL.:

    ". 5isplays the current working directory.

    pwd

    If you are working in home directory then, pwd command displays the current working

    directory as Bhome.

    cal &%%AN>:

    cal command is used to display the calendar.

    S?N'A(:

    The /yntax is

    cal OoptionsP OmonthP OyearP

    &P'I&NS:

    -" 5isplays single month as output.

    -< 5isplays prevBcurrentBnext month output.

    -s 5isplays sunday as the first day of the week.

    -m 5isplays 2onday as the first day of the week.

    -; 5isplays ?ulian dates )days one-based, numbered from ?anuary "*.

    -y 5isplays a calendar for the current year.

    .(A%PL.:

    ". cal

    &ut#ut:

    /eptember D&&$

    /u 2o Tu e Th :r /a

    " D < Q %

    @ $ # "& "" "D "

  • 7/25/2019 Unit1 Material

    48/96

    "Q " "% "@ "$ "# D&

    D" DD D< DQ D D% D@

    D$ D#

  • 7/25/2019 Unit1 Material

    49/96

    -h hostname X

    terminal

    Used by in.telnetd to pass information about the remote host and terminal type.

    -r hostname Used by in.rlogind to pass information about

    the remote host.

    3xamples

    login com#uter$o#e2com- ould attempt to login to the computerhope domain.

    uname command

    6rint name of current system.

    /yntax

    uname O-aP O-iP O-mP O-nP O-pP O-rP O-sP O-vP O-P O-/ systemnameP

    -a 6rint basic information currently available from the system.

    -i 6rint the name of the hardware implementation )platform*.

    -m 6rint the machine hardware name )class*. Use of this option is discouraged+ use

    uname -p instead.-n 6rint the nodename )the nodename is the name by which the system is known to

    a communications network*.

    -p 6rint the current host(s I/ or processor type.

    -r 6rint the operating system release level.

    -s 6rint the name of the operating system. This is the default.

    -v 6rint the operating system version.

    - 6rint expanded system information, one information

    element per line, as expected by /A! Unix. The

    displayed information includes

    system name, node, release, version, machine, and number of A6Us.

    8usType, /erial, and Users )set to MunknownM in /olaris*

  • 7/25/2019 Unit1 Material

    50/96

    !32F and !riginF )set to & and ", respectively*

    -/

    systemname

    The nodename may be changed by specifying a system name argument. The

    system name argument is restricted to /G/[12L1 characters. /G/[12L1 is

    an implementation specific value defined in \sysButsname.hV. !nly the super-

    user is allowed

    this capability.

    3xamples

    uname -arv

    List the basic system information, !/ release, and !/ version as shown below.

    /un!/ hope .@ 0eneric["&%Q"-&$ sunQm sparc /U1,/6=Astation-"&

    uname -#

    5isplay the Linux platform.

    S.>:

    hat is sedS

    non-interactive stream editor

    Interprets sed instructions and performs actions

    Use sed to

    utomatically perform edits on file)s*

    /implify doing the same edits on multiple files

    rite conversion programs

  • 7/25/2019 Unit1 Material

    51/96

    sed command syntax

    sed !peration

  • 7/25/2019 Unit1 Material

    52/96

    7ow 5oes sed orkS

    sed reads line of input

    line of input is copied into a temporary buffer called pattern space

    editing commands are applied

    subseuent commands are applied to line in the pattern space, not the

    original input line

    once finished, line is sent to output

    )unless ^n option was used*

    line is removed from pattern space

    sed reads next line of input, until end of file

    1ote input file is unchanged

    sed instruction format

    address determines which lines in the input file are to be processed by the command)s*

    if no address is specified, then the command is applied to each input line

  • 7/25/2019 Unit1 Material

    53/96

    address types

    /ingle-Line address

    /et-of-Lines address

    =ange address

    1ested address

    /ingle-Line ddress

    /pecifies only one line in the input file

    special dollar sign )* denotes last line of input file

    3xamples

    show only line M#O in#ut-file

    6rint lines between 830I1 and 315, inclusive

    ,.BIN

    Line 1 of in#ut

    Line 3 of in#ut

  • 7/25/2019 Unit1 Material

    55/96

    Line4 of in#ut

    .N>

    Line 5 of in#ut

    Line 6 of in#ut

    1ested ddress

    1ested address contained within another address

    3xample

    print blank lines between line D& and

  • 7/25/2019 Unit1 Material

    56/96

    Line 1umber

    line number command )Z* writes the current line number before each matchedBoutput line

    3xamples

    sed -e JM'wo-t$irds-timeMFJ tuition2data

    sed -e JMQ

  • 7/25/2019 Unit1 Material

    57/96

    /yntax

    addressV iK

    text

    ppend Aommand a

    adds one or more lines directly to the output after the address

    /imilar to the insert command )i*, append cannot be used with a range address.

    ppended JtextK does not appear in sedHs pattern space.

    /yntax

    addressV aK

    text

    Ahange Aommand c

    replaces an entire matched line with new text

    accepts four address types

    single-line, set-of-line, range, and nested addresses.

    /yntax

    address1@address3VV cK

    text

    5elete Aommand d

    deletes the entire pattern space

  • 7/25/2019 Unit1 Material

    58/96

    commands following the delete command are ignored since the deleted text is no

    longer in the pattern space

    /yntax

    address1@address3VV d

    /ubstitute Aommand )s*

    /yntax

    addr1V@addr3V sMsearc$Mre#laceMflagsV

    replaces text selected by search string with replacement string

    search string can be regular expression

    flags

    global )g*, i.e. replace all occurrences

    specific substitution count )integer*, default "

    =egular 3xpressions use with sed

  • 7/25/2019 Unit1 Material

    59/96

    /ubstitution 8ack =eferences

    3xample =eplacement /tring >

    cat datafile

  • 7/25/2019 Unit1 Material

    60/96

    $arles %ain 42< 2; 4 45

    S$aron Bray 624 2;9 6 34

    Patricia =emenway 52< 29 5 19

    ', Savage 525 25 6 3 actions

    #attern Raction

    if pattern is missing, action is applied to all lines

  • 7/25/2019 Unit1 Material

    67/96

    if action is missing, the matched line is printed

    must have either pattern or action

    3xample

    aw JMforMJ testfile

    prints all lines containing string JforK in testfile

    8asic Terminology input file

    field is a unit of data in a line

    3ach field is separated from the other fields by the field separator

    default field separator is whitespace

    record is the collection of fields in a line

    data file is made up of records

    3xample Input :ile

  • 7/25/2019 Unit1 Material

    68/96

    8uffers

    awk supports two types of buffers

    record and field

    field buffer

    one for each fields in the current record.

    names ", D,

    record buffer

    & holds the entire record

    /ome /ystem Eariables

    :/ :ield separator )defaultZwhitespace*

    =/ =ecord separator )defaultZ]n*

    1: 1umber of fields in current record

    1= 1umber of the current record

    !:/ !utput field separator )defaultZspace*

    !=/ !utput record separator )defaultZ]n*

    :IL3123 Aurrent filename

  • 7/25/2019 Unit1 Material

    69/96

    3xample =ecords and :ields

    cat em#s

    'om *ones 5535 6M13M77 654465

    %ary Adams 6457 11M5M74 3976

    Sally $ang 1765 9M33M65 76

  • 7/25/2019 Unit1 Material

    70/96

    3 %ary Adams 3976

    4 Sally $ang 76

  • 7/25/2019 Unit1 Material

    71/96

    comment lines start with F

    awk /cripts

    830I1 pre-processing

    performs processing that must be completed before the file processing starts )i.e.,

    before awk starts reading records from the input file*

    useful for initiali9ation tasks such as to initiali9e variables and to create report

    headings

    8!5G 6rocessing

    contains main processing logic to be applied to input records

    like a loop that processes input data one record at a time

    if a file contains "&& records, the body will be executed "&& times, one for

    each record

    315 post-processing

    contains logic to be executed after all input data have been processed

  • 7/25/2019 Unit1 Material

    72/96

  • 7/25/2019 Unit1 Material

    73/96

    match

    entire input record

    regular expression enclosed by `BHs

    explicit pattern-matching expressions

    Y )match*, _Y )not match*

    expression operators

    arithmetic

    relational

    logical

    3xample match input record

    cat em#loyees3

    'om *ones:5535:6M13M77:654465

    %ary Adams:6457:11M5M74:3976

    Sally $ang:1765:9M33M65:76

  • 7/25/2019 Unit1 Material

    74/96

    cat datafile

    nort$west N $arles %ain 42< 2; 4 45

    western . S$aron Bray 624 2;9 6 34

    sout$west S Lewis >alsass 329 2 3 1

    sout$ern S& Suan $in 621 2;6 5 16

    sout$east S. Patricia =emenway 52< 29 5 19

    eastern .A ', Savage 525 25 6 3