Familiarization with
basic commands
 On a large system serving hundreds of users, someone has to be in
  charge of the administration of the system. This person is known as the
  system administrator.
 The administrator is responsible for the management of the entire
  setup. She allocates user accounts, maintains file systems, takes
  backups, manages disk space, and performs several other important
  functions. She is the person to be contacted in case of a genuine
  problem.
 If you own a workstation or PC that runs some flavor of UNIX, then you
  are probably its administrator. You are then directly responsible for its
  startup, shutdown, and maintenance. If you lose a file, it’s your job to
  get it from a backup. If things don’t work properly, you have to try all
  possible means to set them right before you decide to call a
  maintenance person.
    If you are not the administrator, you can use a UNIX machine only after
    she has opened an account with a user ID and password for your use.
    These authentication parameters are maintained in two separate files
    on your system. You can’t simply sit down at any terminal and start
    working unless you first log on to the system using a valid user-id–
    password combination. The administrator uses a special user-id to log
    on to the system: it is called root. The root user has near-absolute
    powers. Some programs can only be run from this account—for
    instance, the program that creates the user account itself.
  Basic Structure of Command
 Every command line command is a little computer program, even
  commands as simple as clear.
 Some simple commands like clear don’t require any options or
  arguments. Options are usually preceded by a hyphen (-), and they
  tweak the command’s behavior. Arguments can be names of files,
  raw data, or other options that the command requires. A simple
  command that has an argument is echo. The echo command prints
  a phrase to the console.
 If you want to see the last command, press the Up arrow key. You
  can press Up and Down in order to scroll through the history of
  commands that you’ve entered. If you want to re-execute a past
  command, you can scroll to that command then press Enter. Try
  getting back to the echo "Hello World!" command and execute it
  again.
 Computers are organized in a hierarchy of folders, where a
  folder can contain many folders and files. People who use
  Unix often refer to folders as directories, and these terms
  are interchangeable. This directory hierarchy forms a tree,
  like the diagram below. You can use the command line to
  navigate      these    trees     on     your      computer.
Hierarchical File structure
 The home directory is another special directory that is
  represented by a tilde (~). Your home directory contains
  your personal files, like your photos, documents, and the
  contents of your desktop. When you first open up your shell,
  you usually start off in your home directory. Imagine tracing
  all of the directories from your root directory to the directory
  you’re currently in. This sequence of directories is called a
  path.
 Your shell starts in your home directory. Whatever directory
  your shell is in, is called the working directory. Enter the
  pwd command into your shell to print working directory.
 You can change your working directory using the cd
  command. If you use the cd command without any
  arguments then your working directory is changed to your
  home directory.
 You can identify a specific file or folder by its path.
 The root directory (/) contains all of the folders and
  files on your computer.
 Your home directory (~) is the directory where your
  terminal always starts.
 Use the cd command to change your working
  directory.
 The pwd command will print the working directory.
 The ls command will list files and folders in a
  directory.
               Logging In and Out
 Login:
    When you first connect to a Unix system, you see a prompt such as the login
    You should have your user ID (user identification) and password ready.
    Type your user ID at the login prompt, then press ENTER. Your user ID is
     case-sensitive, so be sure you type it exactly as your system administrator has
     instructed.
    Type your password at the password prompt, then press ENTER. Your
     password is also case-sensitive.
    If you provide the correct user ID and password, then you will be allowed to
     enter into the system.
 Logout:
    When you finish your session, you need to log out of the system.
    This is to ensure that nobody else accesses your files.
    To log out
         Press [ctrl-d]: Keep [Ctrl] pressed and then press d
         Just type the logout command at the command prompt, and the system
         will clean up everything and break the connection.
         Type exit
                  System Information with date
                            and who
   Date: To print the date and time information maintained by the UNIX system internal clock, one can use
    the single command named date.
   Who Command: UNIX can be used concurrently by multiple users, who command can be used to view the
    List of Current Users.
   In the second example :There are currently three users—romeo, henry, and steve—sharing the CPU. These
    names are actually the user-ids they used to log in.
   The output also includes your own user-id, (here csed/ romeo), which you entered at the login prompt to
    gain entry to the system. The date and time of login are shown in three columns.
    Basic commands to handle files
 Handling Files: UNIX maintains all data in containers called files.
  These files are assigned names, and a group of filenames are held
  together in another separate file known as a directory.
 Creating a File with echo: There are several ways to create a file.
  Here we use the echo command with a special symbol (the >):
 The echo command is meant to display a message on the terminal,
  but here the message (date) goes to the file foo instead. We’ll not
  concern ourselves with the role played by the shell here but simply
  note that the > is a convenient mechanism of redirecting command
  output.
Calendar (cal) command
 UNAME COMMAND – this command is used to find the
  name of the kernel machine with the date, year information
  about the process
          Displaying a File with cat
 The cat command displays the contents of files, so let’s use it to view
  the file that we just created:
 Observe that we used both the echo and cat commands with an
  additional word (date and foo). They are known as arguments. UNIX
  commands are often used with arguments, and the variety of these
  arguments makes these commands behave in numerous ways.
              File Management
 All data in Unix is organized into files. All files are
  organized into directories.
 These directories are organized into a tree-like structure
  called the file system.
 When you work with Unix, one way or another, you
  spend most of your time working with files.
 You need to understand how to create and remove files,
 copy and rename them, create links to them, etc.
        Creating a hidden file
-   Create a file
-   save it with . at the beginning
-   One can also change a file from ordinary to hidden by
    making use of cp command
-   To list the hidden files one can use ls -a
          Creating a link file
 Touch old1
 Ln old1 new1
 Ln –s new1_soft old1
          File System Types
In Unix, there are three basic types of files −
    Ordinary Files − An ordinary file is a file on the
     system that contains data, text, or program
     instructions.
    Directories − Directories store both special and
     ordinary files. For users familiar with Windows or
     Mac OS, Unix directories are equivalent to folders.
    Special Files − Some special files provide access to
     hardware such as hard drives, CD-ROM drives,
     modems, and Ethernet adapters. Other special
     files are similar to aliases or shortcuts and enable
     you to access a single file using different names.
          Some more important
              commands
 Touch Command to create new files
        Touch test123
 SHELL command : this is used to find the shell which is
  being used
 Executing a file – one can run a file by using the sh
  command
sh represents the shell, and it interprets any argument as
a file to take its input from, it runs the date command.
                Listing Files
 ls command is used to list the files and directories
  stored in the current directory. Here is the sample
  output of the above command:
                              Listing files
 The command ls supports the -l option which would help you to get
  more information about the listed files
                              Listing files
The information about all the listed columns −
 First Column − Represents the file type and the permission given on
  the file. Below is the description of all types of files.
 Second Column − Represents the number of hard links to the file or
  directory.
 Third Column − Represents the owner of the file. This is the Unix user
  who created this file.
 Fourth Column − Represents the group of the owner. Every Unix user
  will have an associated group.
 Fifth Column − Represents the file size in bytes.
 Sixth Column − Represents the date and the time when this file was
  created or modified for the last time.
 Seventh Column − Represents the file or the directory name.
 In the ls -l listing example, every file line begins with a d, -, or l. These
  characters indicate the type of the file that's listed.
                      Hidden Files
 An invisible file is one, the first character of which is the
  dot or the period character (.).
 Unix programs (including the shell) use most of these
  files to store configuration information.
 Some common examples of the hidden files include the
  files −
   .profile − The Bourne shell ( sh) initialization script
   .kshrc − The Korn shell ( ksh) initialization script
   .cshrc − The C shell ( csh) initialization script
   .rhosts − The remote shell configuration file
Hidden Files
 To list the invisible files, specify the -a option to ls −
 Single dot (.) − This represents the current directory.
 Double dot (..) − This represents the parent directory
                            Metacharacters
 Metacharacters have a special meaning in Unix. For example, * and ? are
  metacharacters. We use * to match 0 or more characters, a question mark
  (?) matches with a single character.
 For Example −
    $ls ch*.doc
 Displays all the files, the names of which start with ch and end
  with .doc −
 Here, * works as meta character which matches with any character. If you
  want to display all the files ending with just .doc, then you can use the
  following command −
 $ls *.doc
               Creating Files
You can use the vi editor to create ordinary files on any
Unix system.
• You simply need to give the following command $ vi
filename
• The above command will open a file with the given
filename.
• Now, press the key i to come into the edit mode. Once
you are in the edit mode, you can start writing your
content in the file as in the following program.
                 Creating files
Once you are done with the program, follow these steps
−
   Press the key esc to come out of the edit mode.
   Press two keys Shift + ZZ together to come out of the file
   completely.
• You will now have a file created with filename in the
current directory.
   Editing Files
 You can edit an existing file using the vi editor $ vi filename
 Once the file is opened, you can come in the edit mode by
  pressing the key i and then you can proceed by editing the file.
 If you want to move here and there inside a file, then first you
  need to come out of the edit mode by pressing the key Esc.
 After this, you can use the following keys to move inside a file −
     l key to move to the right side.
     h key to move to the left side.
     k key to move upside in the file.
     j key to move downside in the file.
 Using keys, you can position your cursor wherever you want to
  edit.
 Once you are positioned, then you can use the i key to come in
  the edit mode. Once you are done with the editing in your file,
  press Esc and finally two keys Shift + ZZ together to come out of
  the file completely.
Display Content of a File
 You can use the cat command to see the content of a file.
 For example, to see the content of the above created file.
    $ cat filename
    This is unix file....I created it for the first time..... I'm going to save this
    content in this file.
    $
• You can display the line numbers by using the -b option along with
the cat command as follows
    $ cat -b filename
    1      This is unix file....I created it for the first time.....
    2      I'm going to save this content in this file.
    $
                 Counting Words in a File
 You can use the wc command to get a count of the total number of lines, words, and characters
  contained in a file. For example, to see the information about the file created above
     $ wc filename
     2 19 103 filename
     $
 Here is the detail of all the four columns:
      First Column − Represents the total number of lines in the file.
      Second Column − Represents the total number of words in the file.
      Third Column − Represents the total number of bytes in the file. This is the actual size of
       the file.
      Fourth Column − Represents the file name.
 You can give multiple files and get information about those files at a time. Following is simple
  syntax
    $ wc filename1 filename2 filename3
To find the number of lines alone
wc –l filename
Wc –w filename
Sorting the contents of a file
                              Copying Files
 To make a copy of a file use the cp command. The basic syntax of
  the command is:
 Following is the example to create a copy of the existing file
  filename.
 You will now find one more file copyfile in your current directory. This
  file will exactly be the same as the original file filename.
                            Renaming Files
 To change the name of a file, use the mv command. Following is the
  basic syntax:
 The following program will rename the existing file filename to
  newfile.
 The mv command will move the existing file completely into the new
  file. In this case, you will find only newfile in your current directory.
                             Deleting Files
 To delete an existing file, use the rm command. The basic syntax:
 Caution − A file may contain useful information. It is always
  recommended to be careful while using this Delete command.
 It is better to use the -i option along with rm command.
 Following is the example which shows how to completely remove the
  existing file filename.
 You can remove multiple files at a time
               Standard Unix Streams
 We see command output and error messages on the terminal
  (display), and we sometimes provide command input through the
  terminal (keyboard).
 The shell associates three files with the terminal—two for the display
  and one for the keyboard.
 These special files are actually streams of characters that many
  commands see as input and output. A stream is simply a sequence of
  bytes.
 Under normal circumstances, every Unix program has three streams
  (files) opened for it when it starts up −
     stdin − This is referred to as the standard input and the associated file
      descriptor is 0. This is also represented as STDIN. The Unix program reads
      the default input from STDIN.
     stdout − This is referred to as the standard output and the associated file
      descriptor is 1. This is also represented as STDOUT. The Unix program writes
      the default output at STDOUT .
     stderr − This is referred to as the standard error and the associated file
      descriptor is 2. This is also represented as STDERR. The Unix program writes
      all the error messages at STDERR.
Unix / Linux -
Directories
Management
                   Directories
 A directory is a file, the solo job of which is to store the
  file names and the related information.
 All the files, whether ordinary, special, or directory, are
  contained in directories.
 Unix uses a hierarchical structure for organizing files
 and directories.
 This structure is often referred to as a directory tree.
 The tree has a single root node, the slash character (/),
  and all other directories are contained below it.
             Home Directory
 The directory in which you find yourself when you
  first login is called your home directory.
 You do much of your work in your home directory
  and subdirectories that you create to organize your
  files.
 You can go in your home directory anytime using the
  following command
 Here ~ indicates the home directory.
           Home Directory
 Suppose you have to go in any other user's home
  directory, use the following command
 To go in your last directory, you can use the
  following command
                   Absolute/Relative
                     Pathnames
 Directories are arranged in a hierarchy with root (/) at the top.
 The position of any file within the hierarchy is described by its
  pathname.
 Elements of a pathname are separated by a /.
 A pathname is absolute, if it is described in relation to root, thus
  absolute pathnames always begin with a /.
 Following are some examples of absolute filenames.
                       Absolute/Relative
                         Pathnames
 A pathname can also be relative to your current working directory.
  Relative pathnames never begin with /.
 Relative to user amrood's home directory, some pathnames might
  look like this.
 To determine where you are within the filesystem hierarchy at any
  time, enter the command pwd to print the current working directory
            Listing Directories
 To list the files in a directory, you can use the following
  syntax.
 Following is the example to list all the files contained in
  /usr/local directory
      Creating Directories
 Directories are created by the following
  command.
 Here, directory is the absolute or relative
  pathname of the directory you want to create.
  For example, the command
       Creating Directories
 Creates the directory mydir in the current
  directory. Here is another example.
  $mkdir /tmp/test-dir
  $
 This command creates the directory test-dir
  in the /tmp directory.
 The mkdir command produces no output if
  it successfully creates the requested
  directory.
       Creating Directories
 If you give more than one directory on the
  command line, mkdir creates each of the directories.
 For example,
 Creates the directories docs and pub under the
  current directory.
            Creating Parent Directories
 Sometimes when you want to create a directory, its parent
  directory or directories might not exist. In this case, mkdir issues
  an error message.
 In such cases, you can specify the -p option to the mkdir
  command.
 The above command creates all the required parent
  directories.
           Removing Directories
 Directories can be deleted using the rmdir command as follows.
 To remove a directory, make sure it is empty which means there
  should not be any file or sub-directory inside this directory.
 You can remove multiple directories at a time as follows −
 This command removes the directories dirname1, dirname2, and
  dirname3, if they are empty.
 The rmdir command produces no output if it is successful.
                   Changing Directories
 You can use the cd command to do more than just change to a
  home directory. You can use it to change to any directory by
  specifying a valid absolute or relative path. The syntax is as given
  below.
 Here, dirname is the name of the directory that you want to change
  to. For example, the command
 Changes to the directory /usr/local/bin. • From this directory, you
  can cd to the directory /usr/home/amrood using the following
  relative path
                      Renaming Directories
 The mv (move) command can also be used to rename a
  directory.
 The syntax is as follows.
   You can rename a directory mydir to yourdir as follows
The directories . (dot) and .. (dot dot)
    The filename . (dot) represents the current working
     directory; and the filename .. (dot dot) represents
     the directory one level above the current working
     directory (parent directory).
    If we enter the command to show a listing of the
    current working directories/files and use the -a
    option to list all the files and the -l option to
    provide the long listing, we will receive the
    following result.
 Copying a File with cp: cp command to copy the file foo that we
  just created:
   cp needs two arguments and operates silently. If you run cat foo.sh now, cat will
   also display the string date.
 Displaying List of Filenames with ls: having two identical files, we
  can produce a list of their names with the ls command:
   the ls command to display the attributes of files and directories.
 Renaming a File with mv: The mv command renames a file, and
  the following sequence renames foo.sh to foo.shell.
 Removing a File with rm: The rm command deletes files, and this one
  removes the file foo.shell:
 Handling Directories: Every file has an association with a directory, and we
  often describe this association (somewhat loosely) by saying that a file
  resides in a directory. A user too is associated with a directory, and this is
  conveniently expressed by saying that a user is placed in a directory called
  the current directory. UNIX considers a directory as a file, and some
  commands work with both ordinary files and directories. The file foo can be
  considered to reside in the current directory. This directory was not created
  by us but by the system administrator when opening an account for user
  romeo. But we can also create a directory, copy a file to it, navigate to that
  directory, and remove it.
 Creating a Directory with mkdir: The mkdir command creates
  a directory. The following command creates one named
  scripts:
 one file and one directory, both in the current directory. ls will
  now display both filenames, but if you follow it with -F (with
  a space between ls and -F).
 For example, to check the calendar, you need
  to type the cal command as follows:
                Change Password
 All Unix systems require passwords to help ensure that
  your files and data remain your own and that the system
  itself is secure from hackers and crackers.
 Following are the steps to change your password −
    Step 1 − To start, type passwd at the command prompt.
    Step 2 − Enter your old password, the one you're currently using.
    Step 3 − Type in your new password. Always keep your password
     complex enough so that nobody can guess it. But make sure, you
     remember it.
    Step 4 − You must verify the password by typing it again.
Change Password
               Who are you?
 While you're logged into the system, you might be
  willing to know : Who am I?
 The easiest way to find out "who you are" is to enter
  the whoami command
    $ whoami
   amrood
    $
 Try it on your system. This command lists the account
  name associated with the current login. You can try
  who am i command as well to get information about
  yourself.
            Who is Logged in?
 If you wants to know who is logged in to the computer at the same
  time.
 There are three commands available to get you this information,
  based on how much you wish to know about the other users: users,
  who, and w.
                        System Shutdown
 The most consistent way to shut down a Unix system properly via the
  command line is to use one of the following commands −
Assignment 1 Navigating the
Command Line Exercises
 Set your working directory to the root directory.
 Set your working directory to your home directory
  using three different commands.
 Find a folder on your computer using your file and
  folder browser, and then set your working directory to
  that folder using the terminal.
 List all of the files and folders in the directory you
  navigated to in #3.