Understanding The Unix Architecture

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 96

Unix Architecture

No matter what profession you are in, the process of learning something new requires that you gain some basic skills to perform even the most common tasks. Learning a new operating system is no different. Becoming familiar with the basic tasks in the UNIX or Linux environment and accessing the help feature of UNIX will enable you to acquire the fundamental skills you need to use this system effectively.

Understanding the Unix Architecture:


UNIX is an operating system developed at the Bell Laboratories and is the registered trademark of UNIX Systems Laboratories, a division of AT&T. The operating system is the software that controls the computer hardware and enables interaction between a system and its user. The operating system also forms the foundation for applications software. Because UNIX was designed for software development, the needs of the basic user were ignored in favor of speed and accuracy. However, UNIX has evolved with an ever-increasing focus on the enduser's needs.

Features of Unix:The popularity of the UNIX operating system is increasing because of its various features and benefits. The table lists the features of UNIX and their description.

Features
Open Source

Description
While the source code for UNIX requires a source license, the source code for many variants of UNIX is freely available for users so that they can customize the operating system to add new features and functionalities. UNIX includes many tools and utilities that can be simple commands or programs that you can use to simplify your work. UNIX also provides utilities for all tasks such as electronic mail and compilation. More than one user can work simultaneously on a UNIX system and each user can perform multiple tasks at a time. One of the initial objectives of UNIX was to run the same operating system on different computers and to enable data communication between them. Multiple users can share the information when working in a networked environment. UNIX runs on almost every type of computer, from micro computers to super computers. One of the main reasons for the popularity of UNIX is its portability. Unlike most other operating systems, UNIX is almost entirely written in C, a high-level programming language. Therefore, installing UNIX to a new hardware can be done in a relatively short amount of time.

Tools and Utilities

Multiuser and Multitasking Networking

Portable

UNIX and Linux Systems:


Linux is a UNIX-like operating system that works on the same principle as UNIX. Linux also has the same features and benefits as UNIX. However, there are some differences between the two. Most of the standard UNIX operating systems such as, Solaris, HP-UX, AIX are architecture dependent, but Linux was developed to run on a wider range of platforms than UNIX. Most of the UNIX operating systems are commercial, whereas Linux operating systems are open source. While there are differences in installation, system management, and package management, all Linux systems share the same set of GNU utilities that are free software. These utilities are similar to the equivalent UNIX utilities. Because the Linux operating system and the tools that run on it are open source, Linux is gaining popularity. The UNIX architecture is an operating system architecture that represents a set of protocols to control the hardware resources of the computer and provide an environment on which programs can run. The UNIX or Linux architecture is the layered structure of the different parts of the operating system. It contains four layerskernel, shell, applications and commands, and users.

The kernel is the central layer of the UNIX architecture. It is the layer that interacts directly with the hardware of a computer through device drivers.

which make the kernel the core of UNIX systems.

(binary programs such as shells) The shell is a program that is used to interact with an operating system via the kernel. The shell is the layer of programming that understands and executes the commands a user enters. It acts as a user interface with an operating system. When the user first logs in, a default shell environment opens for the user.

Shells Available in Unix and Linux:Shell The Bourne Shell (sh)

The C Shell (csh)

The KornShell (ksh)

The Bourne Again shell (bash)

Description This shell was the standard shell used in all UNIX environments to interpret the commands entered by users. For all recent versions, KornShell is the default user shell in UNIX systems. The sh command is used to start the Bourne shell. This shell is a UNIX shell whose syntax is modeled after the C programming language. It has many feature improvements over the Bourne shell, such as aliases and command history. You can use the csh command to start this shell. This is a UNIX shell that is compatible with the Bourne shell and includes many features, such as command history, of the C shell. To start the KornShell, you can use the ksh command. This is the default shell on most Linux systems and on Mac OS X. bash the acronym for Bourne-again shell is a pun on the name of the Bourne shell (sh). The bash

The TC shell (tcsh)

command is used to start the Bourne Again shell. The bash shell is also available on the Windows and Mac Operating Systems. This is a UNIX shell based on and compatible with the C shell (csh). It is essentially an extension of the C shell with added features. You can use the tcsh command to start this shell.

Interaction between a User and a system:->


Once you log in to a system using your default shell, your every interaction with the system is carried out in a sequence of steps. The shell prompts you to enter a command You enter a line of command. The shell processes your command to determine the actions to be taken and carries out the required actions. After the execution of the command, the shell prompts you to enter the next command. This cycle continues every time you enter a command. There are different shells available in UNIX such as the Bourne shell, the C shell, the KornShell, the Bourne Again shell, and the TC shell. Each shell is different from the others because of its unique features. You can switch to another shell from the shell you are currently working on by using the commands such as bash and ksh.

These commands will start the new shell within the same interface replacing the current shell.

Applications and commands are the tools, utilities, and commands that run in the UNIX system to
perform the required tasks. These utilities can also be used to install a wide variety of add-on programs such as

compilers, word processors, either free of charge or by paying a cost to software vendors.

Users are individuals or groups of people who use the UNIX system to perform tasks. This layer also consists of
programs or applications run by users. These programs or applications execute as special users with limited access to the system. For example, the ftp program is used to transfer files. When the user runs the ftp command, UNIX provides a username ftp and the ftp program will run as a special user.

*Multiuser System:
UNIX is a multiuser system with a system console and local user terminals. Multiple individuals can use one UNIX system. Each of these individuals is known as a "user" of the system. Multiple users can log in to one UNIX computer at the same time, through remote connections over the Internet or a network, or through terminals connected to the UNIX computer. Every user is subject to file permissions. Since multiple users are logged into the same system at the same time from different terminals, each user is assigned a unique user ID. This means users cannot navigate through directories where they do not have permissions. Users cannot read, edit, or delete files unless they have the required permissions. Note: There may be many other users in UNIX who are not peoplethey could be programs running, such as web servers that execute as a special user with limited access to the system.

*Groups:
Groups are used to logically assemble different users together to manage them. Groups allow you to provide access for the files or resources to the users belonging to a particular group. A group may have multiple users and a user can be a part of multiple groups based on the requirements. For instance, two members from the management team of your company require access to everyones data but their directory needs to be protected from everyone. To do this, these two members can belong to all existing groups and can have a special group called management in which they are the only members.

Login to the System:


Command Line Interface (CLI):The Command Line Interface (CLI) is a text-based interface between an operating system and its user. Users enter their inputs through commands in the CLI and get the required output.

The CLI has a prompt where the user has to type commands to execute programs and applications in UNIX. Unlike a graphics interface where the user can click icons and buttons and use the mouse to execute commands, a CLI requires the user to use only the keyboard or a similar device to enter commands at the prompt.

*Graphical User Interface:The Graphical User Interface (GUI) is an interface that uses graphical objects as a means of interaction between an operating system and its user. It has elements such as menus, icons, and buttons that a user can click in order to execute commands and applications in the operating system.

login prompt:
The login prompt is where you enter your user name and password to access the UNIX operating system.

The login program validates the user name and password. Once validated, the user acquires access to the computer based on their access rights. If you enter an incorrect user name or password at the login prompt, you will not be able to access the computer.

When you type the password at the login prompt, it does not get displayed because of security reasons.

*passwd command:You can use the passwd command to change the login password. Users who are currently logged in can change only their login password, and not that of the other users. When you type the passwd command at the command prompt, you are asked to enter your current password and then the new password you want to set. The new password is effective from the next time you log in to the system.

*passwd Command Errors:If you type the incorrect current password, you will get an error message "passwd: Authentication token manipulation error". Then you will have to type the passwd command again to change the password. If the new password that you enter does not match with the password you retyped, you will get an error message "Sorry, passwords do not match". You will have to provide the new password again.

exit command:
The exit command is used to log out from the system. If you switch off your system, you will be disconnected from the system resulting in improper termination of the programs that you were running. It may cause file damage or data loss. After the exit command is executed, you will get the login prompt to log in to the system again.

Use basic commands:


Command syntax:The Command syntax is the format in which a command is typed at the command prompt. The command syntax consists of three parts: command, option, and argument. The command, options, and arguments have spaces between them. These spaces serve to separate one part of the command from the others and are called delimiters. The options and the arguments are not always required with every command.

*Command Option and Arguments:


Arguments to a command are used to specify on what a command is to be operated. The cat file1 command is used to display the content of the file1 file. In this command, file1 is the argument to the cat command. Command options allow you to specify the operation of a command. A single command can be executed with different options. You can specify options to a command by using a minus sign (-) followed by the option that can be a letter or a word. The ls command is used to list the names of the files in a directory. The ls -l command displays a long version of the list with details of each file. Here, -l is used as an option to the ls command to display the list with details of each file.

*Some examples of commands that do not require the general format of the command syntax include:
Command with option and argument - head -5 /etc/passwd Command with only argument - cat /etc/passwd Command with only option ls l Command with no option and no argument date

*UNIX or Linux commands are case sensitive and will not be executed if the wrong casing is used. For example, commands such as ls, pwd, or cat need to be specified in lowercase and will not work if they are typed in uppercase. Most UNIX or Linux commands are written in lowercase.

*Command History:
The shell that you use to execute commands maintains a history of the previously executed commands. The history editing techniques allow modifications to a previously executed command. Therefore, you can use the history editing techniques to recall, edit, and reuse commands and prevent repetitive typing. This simplifies the process of correcting spelling errors and removes the need to repeat complicated commands and arguments. Note: History editing is shell dependent and differs in different shells.

*Ctrl+C command:You can stop the execution of a command by pressing Ctrl+C. When you press Ctrl+C, the system will terminate the running command and return to the command prompt. Before the command prompt, the character ^C is displayed on the terminal; this verifies that the system has received the Ctrl+C command. Ctrl+C can be used to cancel the execution of time-consuming commands.

*date command:
The date command displays the current date and time set on your system. You can control the format of the output of the date command by using the sequence of characters such as %H, %M, %y, and %m. The syntax for displaying the formatted output is date +[FORMAT]. The FORMAT is the combination of characters used to display different fields of the date. You can use - (hyphen) or : (colon) followed by different fields of the date for a clear output.

The characters used to display the different fields that are also called interpreted sequences of the date command are listed. characters Description %d Displays the current day of month (01 to 31). %D Displays the date in the format mm/dd/yy, where mm is month, dd is day, and yy is year. %H Displays the current hour in the 24-hour format (00 to 23). %l Displays the current hour (1 to 12). This option does not display A.M. or P.M. after the hour. %m Displays the current month of the current year (01 to 12). %M Displays the current minute (00 to 59). %r Displays the time in the 12-hour format that is hh:mm:ss [A.M. or P.M.]. %R Displays the time in the 24-hour format that is hh:mm. This option does not display the value of seconds. %S Displays seconds (00 to 60). %T Displays the time in the 24-hour format that is hh:mm:ss. This option displays the value of seconds also. %y Displays the last two digits of the current year. %Y Displays the current year in the four digits (yyyy).

*cal command:
The cal command displays the calendar for any month or year. If you do not specify the month or year with the cal command, it will print the calendar for the current month. You can display the calendar for a specific month of a year by specifying the month and the year after the cal command. Specifying a single number after the cal command will be considered the year and the calendar for the complete year will be displayed. The year must be specified as four digits; otherwise, the cal command will not display the desired results. For instance, the command cal 08 will not display the calendar for the year 2008.

*uptime command:
The uptime command is used to determine the duration since the system is running. The output of the uptime command gives information about the current time, how long the system has been running, and how many users are currently logged on.

**Load Average:
The last part of the uptime command shows the system load averages for the last 1, 5, and 15 minutes. Load average is the average number of processes waiting to run on the system for the last 1 minute, 5 minutes, and 15 minutes. Ideally, the number should be less than one. This information can be used to know whether the system is busy or not. The load average information is specific to the operating system and the hardware. A system with a load average value of 10 may run faster from another system with a load average value of 3.

*id command:
The id command is used to view the user ID and group ID. The output of the id command displays the user ID of the user who has run the command, username, the group ID of the groups that the user belongs to, and the group names. The user system.

ID is a unique identification number that differentiates one user from the other within a Linux or UNIX

The group ID uniquely identifies the groups that the user is a part of. User IDs and group IDs are assigned by the system administrator.

* who command:
The who command is used to determine the details of users currently logged in to the system. The output of the who command includes the user name, the time since the user is connected, and the name of the system from which the user is connected.

*whoami command:
The whoami command is used to display the user name with which you are currently logged in to the system along with the user ID. Sometimes, you may need to log in to a system and switch between different users and you may not be sure with which user you are currently logged in. In such instances, you can use the whoami command to know your current user name.

*hostname command:
The hostname command is used to display the host name of the system you are currently logged in to. When you log in to different systems using the same terminal, you can use the hostname command to identify the system on which you are presently running the commands.

*w command
The w command is primarily used to display the details of users who are currently logged in to the system and their transactions.

The output of the w command is divided into two areas.


The first line shows the status of the system. Below the first line, the output shows a table with the first column showing all users logged in to the system and the last column showing the current activities of the user. The remaining columns of the table show different attributes associated with the users.

* wc command:
The wc command is used to print the number of lines, words, and characters in a file. The syntax of the wc command is wc [option] [filename]. Various options can be used with the wc command to customize the output as desired. The -l option is used to print the number of lines, the -w option to print the number of words, and the -c option to print the number of characters.

*echo command:
The echo command is used to display a line of text on the terminal. You can specify the text you want to display after the echo command. You can also use the echo command to display the value stored in a variable by specifying the variable name after the echo command.

*sleep command:
The sleep command is used to pause the activities for a specified time. The sleep time command makes the prompt unavailable for the number of seconds specified by the value of the variable time.

The sleep command can be used to schedule a command to run at a specified time. For example, the sleep 600; date command will execute the date command after 600 seconds.

* shell environment
The shell environment is the primary environment used to work in Linux. It contributes to the look and feel of your shell. The shell environment includes the way your keystrokes are interpreted by the operating system. The shell environment is the collection of variables that have specific values to control the behavior of the shell.

You can use the env command to view your shell environment.

Shell variables are a set of parameters that contain information for the system to use and control the way a
shell works. Shell variables are local to the shell in which they are defined. They play an important role in configuring a user's environment. This information is necessary to set up a workable interface for each user who accesses the system. The user can modify the shell environment to suit personal needs and preferences by changing the value of the shell variables. You can display the value stored in a shell variable by using the $ (dollar) sign before the variable name.

Shells also control the behavior of the applications installed on your system.

TMOUT Variable Behavior:


The KornShell provides additional 60 seconds over the value of the TMOUT variable before exiting the shell, for example, if the TMOUT variable is set to x. Now at any instance, if you do not perform any action for x seconds, the KornShell will display a message that the shell will be terminated after 60 seconds and the command prompt will be unavailable. After displaying the message, the shell will wait for any input for 60 seconds before terminating. You can press Enter to get the prompt back and run the commands.

*export command:

After changing the value of a shell variable, you need to export the variable so that its new value can be used by the shell. The export command is used to export the variable. For example, if you want to change the path of your login directory, you need to run the HOME=/usr/login command, and then the export HOME command so that the user is in the /usr/login directory on the next login.

You can also combine the command to change the value of the variable and to export the new value by using the syntax export [variable name]=[value].

Get Help:*man command:


The man command provides reference information about the requested command or allows users to search for commands related to a particular keyword. man is the short form of manual. The syntax of the man command is man [command name]. The man command enables you to display manual pages, which are screens full of information from the system documentation. The information in man pages is generally very long and cannot be displayed on one screen. You can press the Spacebar to display the next screen of information or you can press Enter to display the next line at the bottom of the screen.

**Command Used in man Pages:


Sometimes it is difficult to find the relevant information in the man pages because you get extensive information about a command in the man page. You can search for the specific information about a command. Press / (forward slash), type the word you want to search, and then press Enter; all occurrences of that word will be highlighted in the man page. If you find the relevant information in the man page and you have not scrolled down all pages, you can quit the man pages by pressing q. Also, while reading man pages, you can press h anytime to open the help menu of man pages.

* apropos command:
The apropos command displays a list of all topics in the man pages that are related to the subject of a query.

It is useful when searching for commands, where exact names are not known. The syntax for the apropos command is apropos keywords. You can also use the man -k command to get help by using keywords. The

man -k command is

equivalent to the apropos command.

*whatis command :
The whatis command is used to display a single line of summary about a command in UNIX. It is the line from the name section of the man page. The whatis command searches a set of database files on your system containing a short description of commands and displays on the command line. The syntax for the whatis command is whatis

[command name].

Working with Files and Directories:

Imagine that you have spent the last half hour searching through piles of papers for an article you wrote a week ago. If only you had all your papers neatly organized in a folder within a filing cabinet, you would not be in this mess. Your computer is not much different than your desk. All data on your computer is organized as files and the knowledge of basic file and directory management will provide you with a firm foundation for the UNIX or Linux operating system.

*file system:A file system is a component of the operating system that manages the access to files and directories. A file system also manages file naming conventions and logical placements of files and directories for storage and retrieval. The UNIX file system enables you to view, organize, secure, and interact with files and directories located on storage devices. A storage device can be formatted to contain one or more file systems.

A file is the most basic component of the UNIX and Linux operating system. In UNIX, almost everything is considered a file including documents, commands, devices, and directories.

**Hidden Files and Directories:


Hidden files and directories are files that store settings, configurations, and user preferences. These files are by default hidden from display and have a period in front of the file name. For example, .report is a file that contains annual reports of the members in the R&D team. Most of the applications that you install, create hidden files and directories to store application-specific settings, configurations, and user preferences for applications. These files and directories are not used for work purposes by normal users, and so they are hidden from normal display. However, you can use the

ls -a command

to display hidden files and directories, if required.

UNIX file system


The UNIX file system is a hierarchical structure in which directories contain subdirectories and files. At the top of the file system hierarchy is the root directory, symbolized by a "/" (forward slash). Next in the hierarchy are system directories such as /bin, /etc, /sbin, /usr, and /lib, followed by directories within the system directories. A user will not usually have access to system directories. The "/" (forward slash) character separates directory levels. The directory and file names combined with the "/" (forward slash) separators constitute a path name.

**Home Directory:
The home directory is the default directory for each user logged into a UNIX or a Linux system. The home directory location is also referred to as the home path. The home directory contains user documents and other personal data. All users have access permissions to place their files in their home directories. In the home directory, users can install software that they cannot install on the system directories because of security restrictions. After logging in to your home directory, you can navigate to other directories within your home directory or outside your home directory, depending on your access rights.

** current working directory:


The directory in which you are working is called your current working directory. For work purposes, you will have to navigate to various directories. You can use the pwd command to check your current working directory

** parent directory:
The parent directory is the directory one level above your current working directory in the file system hierarchy. The file system is hierarchical and therefore, every directory other than the root directory has a parent directory. You can use the .. (dot-dot) notation to refer to your parent directory. The dot-dot references can be used multiple times to refer to the directories above in the file system. For example, you can use ../.. to refer to two levels higher than your current working directory..

**UNIX and Windows File System:


The file system structure of UNIX is different from that of Windows. In UNIX, the starting point or the root of the file system hierarchy is / (slash). The root in UNIX is parent of all directories including files and directories on different disks or different drive on the same disk. In windows, the root of the file system is different for different drives or partition (on the same disk or on multiple disks). Usually, root is C:\ for the drive on which the operating system is installed. The partitions of the disk other than the C:\ drive have different roots such as D:\, and E:\. Another difference is that in Windows you use a \ (backslash) to separate directories in the hierarchy, whereas in UNIX, you use / (forward slash) for the same.

The UNIX operating system supports three types of file systems.

*Logical volumes:
Logical volumes is a method of structuring multiple file systems across multiple physical disks. Today, instead of physical file systems, the concept of logical volumes is used by many UNIX and Linux computers. Current versions of Linux have built-in capabilities to build logical volumes. Multiple disks grouped together act as one logical volume group that an administrator can divide into multiple logical volumes. Each of these logical volumes acts as an individual file system.

* Logical Volume Management:


Logical Volume Management is the concept of creating and managing individual file systems on logical volumes within a single large logical volume group. Logical volume management gives you the flexibility of increasing the file system capacity, whenever required, by adding another physical disk to the logical volume group. Once the new disk becomes a part of the logical volume group, the administrator can allocate new disk space to any individual logical volume to increase its capacity.

Benefits of Logical Volume Management


The benefits of logical volume management include higher data security, fault tolerance, improved availability, and increased capacity. Logical volume management allows: Allocating disk space efficiently and flexibly than in traditional physical file systems. Moving logical volumes between different physical devices, when required. Creating very large logical volumes by using multiple physical devices. Replacing drives without interrupting services.

* path:
A path specifies a location in the file system. It begins with the root, which is the directory at the top of the directory tree, and ends with the directory or file you want to access. You can refer to a particular file by providing a path to the specific directory that contains the file. If, for example, the directory john contains a subdirectory junk, which contains the file myjunk, then you would use the following path name to refer to the file: /john/junk/myjunk. Notice that the forward slash character ( / ) is used to separate items in the path. The slash that precedes john represents the root directory, from which the path to myjunk begins.

There are two ways of referring to a file name


by using absolute paths and by using relative paths. An absolute path is the exact location of a file in the file system. An absolute path contains the complete path from the root directory to the file, including all subdirectories in it. The absolute path starts with a forward slash. A relative path is a truncated version of the path that provides the location of a file relative to the current working directory. A relative path is the path relative to the current working directory, and therefore the full absolute path need not be included. These paths however comprise [.] and [..] indications for the current and parent directory.

The main difference between absolute and relative paths is that the absolute paths act as the independent and unique
descriptors of the location of the file and you can use them to access the file from anywhere in the file system. Relative paths depend on your current working directory.

* Special characters
Special characters are characters that are used with basic or primary commands in UNIX or Linux to modify the behavior of a command. Special characters are also called metacharacters. They are not part of a command; but are features available in the shell and their operation varies in different shells. Special characters can be used with most commands in UNIX and Linux.

Special characters ~ (tilde)

$ (dollar)

; (semicolon)

whitespace

Description Used as a shortcut to the home directory. You can refer to your home from anywhere in the file system using the ~ sign. For example, the cd ~command will take you to the home directory from your current working directory. Used to access the value stored in a variable. The variable name is preceded by the $ (dollar) sign. The echo the value is $val command, will display the value of variable val as a part of text. Used as a command separator. You can use more than one command on a single line using ; between the commands. For example, the command1; command2; command3 statement will run three commands in sequence. Used to determine where the command including arguments starts and ends. Whitespace characters are spaces, tabs or new line characters. For the mv /user/home/log /log1 command, the shell starts reading from the start of the command line to the first whitespace and identifies it as the command to be executed. In this example, the command is mv. Then

\ (backslash)

' (single quote) " (double quote)

the set of characters till the next whitespace is identified as the first argument. In this example, the first argument is /user/home/log. In this way, the shell reads the entire command till the end of the command line. Used to disable the meaning of the any other special character immediately following the \ character. If you want to print the statement "The value is $50", you need to use the echo The value is \$50 command. The use of the \ character will disable the special meaning of the $ sign and print the dollar sign as a part of the text. Used to disable the special meaning of all the characters enclosed within the single quotes. Used to disable the special meaning of the other special characters enclosed within the double quotes. The difference between double quotes and single quote is that it does not disable the meaning of three characters $, ` (backtick), and \ (backslash) when enclosed within double quotes.

* Wildcards
Wildcards are expressions or special characters that use patterns to locate multiple files at the same time. You can use wildcards to list or search for files that match a pattern or when you only know a part of a file that you want to search for. This helps you in expanding or narrowing your search, as required.

** Search for a file:


When you are not sure of the file name, you can guess a file name, or list a group of file names by substituting any character or characters in a file name with wildcards. When you use
a wildcard to search for a file, the shell that you are using replaces the wildcard with the matching file names.

wildcards * ? [ ]

Description Matches zero or any number of characters in a file name. Matches any one character in a file name. Matches one of the characters included inside the [ ] symbols. You can also specify a range within [] wildcard. For example, [15] matches any one character from 1 to 5 in the search. You can use the ! character if you dont want to include some characters in your search. For example, if you want to list the files that do not start with letter "a", you

can use the command ls [!a]*. You can also use this character to exclude a range of letters from your search. The command ls [!a-m]* will list all files that do not start with any letter between a and m.

**Complex Wildcards:
A complex wildcard is a combination of individual wildcards. For example, enter ls [a-z]?[1-9] to list the files with file name of three charactersthe first character being a letter, the last character a number, and the middle character that can be a letter, a number, or any special character.

* cd command:
You can use the cd command to navigate between directories in the UNIX or Linux file system. When you log in to a UNIX or a Linux computer, you will be in your home directory. To use files or resources in any other directory, you will need to use the cd command to navigate to the required directory. You can also use the required options with the cd command to navigate between files and directories.

Option cd / cd.. cd ../../ cd ~ cd cd /<path to a directory>

Description Moves to the root directory. The "/" symbolizes the root directory. Moves to the parent directory. Moves to the parent directory of the parent directory. Moves to the home directory. The "~" represents the home directory. Moves to the home directory. Moves to the directory specified by the path.

* ls command:
You can use the ls command to list and view all files and directories in a directory. By default, the ls command displays the files in your current working directory. There are many options that you can use with the ls command to get specific information such as size, the name of the user who owns the file, the group to which the user belongs, date and time when the file was created, and access permissions in separate columns. You can also use a combination of multiple options to display files and the related information, as required.

The most commonly used ls command options are listed in the table. Options Description -l Lists the specified files in a directory in a

-a

-t -r -h

-s -R

long columnar format with information about the file name, ownership, group of the owner, access permissions, and date and time of last modification. The list is sorted in alphabetical order. The -l option controls the format, and not the files listed. Lists the specified files in a directory including the hidden files and directories. The list is sorted in alphabetical order. The option determines the action of the ls command. The arguments to the ls command determine which file is to be displayed. For instance, in the command ls -a [o]*, wildcards are used as arguments to display all files starting with the letter "o". The output of this command will not list any hidden file or directory in the current working directory, even though the -a option is used. You will obtain the same result without using the -a option. Lists the specified files in a directory sorted according to time stamps. The default listing in this case shows the last modified file first. Lists the specified files in a directory in the reverse order of the normal output. Displays the size of the specified files in human readable format. This option is used in combination of the -l option. For a file of size 10 megabytes, the ls -l command will display the size as 10485760, and ls -lh command will display the size as 10M. The -h option for the ls command is not available on many UNIX systems. Displays the size of the file in blocks. The size is displayed as the first column and the size of each block is 1 kilobyte. Lists the files in the directory and subdirectories recursively.

Note: The options -t, -r, -h, -s, and -R should be used in combination with the -l option to display the output in a more readable format. For example, use the command ls -lh to display a long list of the files and to display the file size in human readable format.

**The ls Command for Files And Directories:


You can also give the name of a file or a directory to the ls command as an argument. When the name of a file is given as an argument, the ls command will display the name of the file with others details depending on the option provided with the ls command. For example, ls -l fileA command will display the filename with information about the ownership, group of the owner, access permissions, and date and time of last modification of the file. In this case, the contents of the file are not displayed. When the name of a directory is given as an argument, the ls command will display the contents of the directory. For example, the ls dirA command will display the files and directories within the dirA directory.

Display File Contents:


* cat command:
The cat command is used to display the contents of a file. The syntax of the cat command is cat [options] filename. If you run the cat command without specifying any option, the contents displayed will be scrolled down to the end of the file, if the file is longer than the screen length. You can use different options with the cat command to view the contents of any file type and also to display the contents with some formatting to increase readability.

The options of the cat command are listed below in a table.


If You Need To Number each line in the displayed output Merge sequence of blank lines into one Display all characters in an executable file including non printable characters Display tab characters in the file as ^I Use This Option -n -s -v -t

*more command:
You can use the more command to display the contents of a long file one page at a time. The syntax of the more command is more [options] filename. You can view the subsequent pages in the file by pressing the Spacebar. While you are reading the file, you can press Q to quit and return to the command prompt.

** less command:
You can also use the less command for displaying files one page at a time. The less command is similar to the more command but it is not present on most of the UNIX systems.

Options used with the more command are listed in the table.
If You Need To Start reading the file from a specific line number Display the helpful prompts at the bottom of each page. Merge sequence of blank lines into one Clear the screen before displaying the contents of a file Use This Option +n (n is the line number) -d -s -c

Commands Used with more commands:When you are reading a file using the more command, you can use specific commands to display different parts of the file. Commands used within the more program are listed. If You Need To Use This Command Display next screen Spacebar (Press the Spacebar.) Display the next n lines n+Enter (Press a number and then Enter) Display list of commands available within the more h program Scroll down half page. d Quit the more program and return to the command q prompt. Skip forward by n lines ns Skip forward by n screens nf Display the current line number = Display the current file name and line number :f Move back one screen b Move forward one line e or press Enter

* head command:
The head command is used to view the first few lines of a file. The syntax of the head command is head -n filename where n is the number of the first n lines of a file. If you do not specify the number of lines, the head command will display the first ten lines of the file.

* tail command:->
The tail command is used to view the last part of a file. The syntax of the tail command is tail -n filename where n is the number of the last n lines of a file. If you do not specify the number of lines, the tail command will display the last ten lines of the file. -f is a very useful option of the tail command. It shows the appended data if the file is updated by any other program. For example, the server updates a file data_reports after every 30 seconds by appending the data into it that was collected for the last 30 seconds. If you are viewing the file using the tail -f data_reports command, it checks for any update in the text and displays the new lines appended to the data_reports file.

Creating Files and directories:


* touch command:
The touch command can be used to create an empty file with file size zero bytes so that you can write in these files later. The syntax of the touch command is touch [filename]. You can also use the touch command to update the timestamp of the existing file. The timestamp shows the last time the file was accessed or updated.

* mkdir command:
The mkdir command is used to create a directory. The syntax of the mkdir command is mkdir [option] [name of the directory]. You need to specify the path with the name of the directory to create the directory. If you do not specify the path, the directory will be created in your current working directory. If you create a directory with the name of a file that exists, an error message will be displayed.

When you use the mkdir command without using any option, the directory created will have the default permissions of read and execute.

Some of the most commonly used options of the mkdir command are listed in the table. options -m -p
Description Specify permissions for the created directory. Create a directory with any number of subdirectories in it.

Manage Files and Directories:


* cp command:
The cp command is used to copy files and directories from one location to another. The original file you

copy is neither altered nor deleted in the copy operation.


The syntax for the cp command is cp [option] SOURCE DESTINATION. SOURCE is the file or directory (with or without the path) you want to copy and DESTINATION is the file or directory (with or without the path) of the copy of the original file.

The command cp file1 file1.bk will copy the file1 file to the same directory with the filename file1.bk. If the file1.bk file exists in the directory, the copy command will overwrite the existing one.

You can use options with the cp command to copy files and directories interactively and recursively. The options of the cp command are listed If You Need To Copy files interactively where you are prompted to confirm that you want to overwrite the existing file Force the existing files to be overwritten Copy directories recursively. Copy files with the same attributes of the original file such as mode, ownership, and timestamps. . Use This Option -i -f -r -p

**Copy multiple files in a directories:


To copy multiple files in a directory, you need to specify the directory name for the destination instead of specifying the new file name. For example, the command cp file1 file2 file3 newdir will copy the three files file1, file2, and file3 to the newdir directory.

* mv command:
The mv command is used to move files and directories from one location to another. The syntax of the mv command is mv [option] SOURCE DESTINATION. SOURCE is the file or directory (with or without the path) you want to move and DESTINATION is the file or directory (with or without the path) with which you want to move the original file.

The options you can use with the mv command along with their description are listed in the table below. If You Need To Move files interactively Force the existing files to be overwritten Use This Option -i -f

**Rename a File:
There is no separate command for renaming a file in UNIX or Linux. You can use the mv command to rename a file in the current directory. For instance, the command mv notes reports will rename the notes file as reports in the same directory.

* rm command:
The rm command is used to remove files and directories permanently from a system. The syntax of the rm command is rm [option] filename. You should use the rm command carefully because once the file is deleted, it cannot be retrieved. ***Note: You can also use wildcards with the rm command. But, you need to be careful when using wildcards with the rm command options. This may lead to the deletion of files that you do not want to delete. For example, the rm -rf* command will remove all the files and directories within your directory without prompting. ***Options of the rm Command: You have different tools on the Windows and Macintosh operating systems to recover files that are deleted accidentally. But in UNIX and Linux, you can retrieve the file only if you have taken the backup on some other device. You can use the -i option to remove files interactively. Options that can be used with the rm command are listed. If You Need To Use This Option Remove files interactively -r Force the files to be removed ignoring all warnings -f Remove directories including all the files and -r subdirectories recursively

* rmdir command
The rmdir command is used to remove empty directories. If the directory you are trying to remove is

not empty, the rmdir command will display an error message. The syntax of the rmdir command is rmdir [option] directory.
*You can use the -p option to remove all directories in the pathname.

For example, the rmdir -p x/y/z command will first remove the /z directory, then the /y directory, and finally the x directory. If any of these directories is not empty, the rmdir command will terminate.

You can also delete multiple empty directories with a single command line. The rmdir dir1 dir2
dir3 command will remove the dir1, dir2, and dir3 directories.

Searches Files & Directories :


* find command
With the find command, you can search for files and directories that meet specific criteria.

You can search for files in a specific location or the entire system.
The syntax of the find command is find [search locations] [options] [action].

that match the search criteria. The find command recursively searches for the files matching the search conditions in the directory structure, including any subdirectories and their contents, beginning with the starting directory you specify.

***To specify your current working directory as the starting directory, insert a "." (dot) after the find command.

*** If you need to search all directories under the root directory, insert a "/" (forward slash) after the find command .

If you do not specify the action to be performed, the find command will display the searched files.

**Unspecified Search Destination:


If you do not specify the starting directory, the find command will start the search for the current working directory. You can use various options with the find command to make your search specific. Options Description -name -depth n -type x

-size n -mtime n -ctime n -atime n

-user x -group x

***Enhanced Searching using size,mtime,ctime, and atime option:


With options, such as -size, -mtime, -ctime, and -atime, instead of using an "n" to specify a value, you can also use "-n" to specify all values less than n and "+n" to specify all values more than n to search for files.

Managing File Permission:


Protecting information on a system is critical for maintaining its integrity. Consider a scenario where all your organizational policy documents, business development plans, and other important documents are stored in a central repository. You need to ensure that such confidential documents and resources are secure from unauthorized access. Permissions and ownership enable you to restrict or assign access to certain users for specific files and directories.

Permission is the property that controls the accessibility of a file or directory by users of a system.
Permissions may be set at different levels and for different users and groups. The basic file or directory permissions in UNIX or Linux are read, write, and execute. You can use the ls -l command to view the permissions assigned to a file. The read, write, and execute permissions are denoted by the letters r, w, and x respectively. The - (hyphen) sign in place of r, w, or x denotes that there is no permission assigned to the file or directory. The permissions (r, w, x, or -) vary for different classes of users of the file. The classes of users are user (u), group (g) and other (o).

**User Classes::
Permissions for a file can be managed by using the three user classes. The table lists all the user classes and their description. user class u o g a Description User - owner of the file. Others - users who are not the owners of the file or members of the owners group. Group - users who are the members of the owner's group. All - all three user classes (u, g, o).

**Permission String: The output of the ls -l command shows the permission string for a file or directory. The permission string contains ten characters.
The first character indicates the type of file: d for directory and - for file. The next nine characters indicate the permissions on the file or directory for three user classes. Characters at second, third, and fourth position denote permissions for the owner or user of the file or directory. Characters at fifth, sixth, and seventh position denote the permissions for group and the characters at eight, ninth, and tenth position denote the permissions for others

**File Permissions:
A set of read, write, and execute permissions is defined for each class of user. The permissions in
a permission set are written in a sequence: read, write, and execute. The permissions yield different results when applied to files and directories. .

Permissions read(r) write(w) execute(x) -(dash)

Access Levels

Combination of Permissions on a File:


The different combinations of read, write, and execute permissions of a file are listed in the following table. Permissions Description --No access allowed. r-Read-only access. r-w Read and execute access. rwRead and write access. rwx All accesses allowed. UNIX allows you to set some combinations of permissions for a file and these permissions may not always be useful. For example, --x is a permission set in which a file cannot be read and therefore cannot be executed. -wis a permission set in which a file cannot be read and therefore cannot be modified.

**Directories Permissions:
Read, write, and execute permissions are also applied to a directory. However, these permissions have a different meaning from those that are applied to a file. Directory Permission Access Level read(r)

write(w)

execute(x)

-(dash)

Combination of Permissions on a Directory:


The useful combinations of permissions for a directory are very limited. With read only permission for a directory, you can just list the files and subdirectories contained in it. Because you dont have execute permission, you cannot navigate into the directory to view its contents in detail. Similarly, if a directory has just the execute permission and not the read permission, you can examine the files in detail, but you cannot view a list of the files and the subdirectories. Therefore, to work with directories, you should have at least read and execute permissions. The different valid combinations of read, write, and execute permissions of a directory for a class of user and their description are listed in the table. Permission Description --No access allowed. r-x Read and execute access. rwx All access allowed. Permission mode is a way to represent the permissions of files and directories. There are two modes of representing permissionsthe octal mode and the symbolic mode. The octal mode is also called the absolute mode. In the symbolic mode, permissions are represented in the rwx format. In the octal mode, permissions are represented by numbers from 0 through 7. The system interprets the permissions only in the octal mode. If you assign the permissions in the symbolic mode, the system will convert it into the octal mode.

Representing Permission in the Octal Mode:


While representing the permissions in the octal mode, each type of permission is represented by a number. Permission Number assigned to it r 4 w 2 x 1 -(No permission) 0 In octal mode, you use an octal number from 0 to 7 to represent the set of permissions for each class of user. The octal number represented for each set of permission is the sum of numbers representing each read, write, and execute permissions.

* chmod command:
You can use the chmod command to modify file permissions. The chmod command can be used in both the symbolic mode and the octal mode. Only the owner of a file (or root user) can change its permissions. The syntax of the chmod command is chmod [OPTION] MODE [,MODE] FILE. The OPTION can be the user class with an operator or any of the chmod command options.

You can use the -R option with the chmod command to change the permissions on a directory recursively.

The symbolic mode is commonly used to modify file permissions. The format of the chmod command in the symbolic mode is

You can also set the permissions for all the three user classes on a single line. For example, chmod o+wx,u-x,g=r-x report.

There are three types of operators used by the chmod command in the symbolic mode. Operator Description +

The format of the chmod command in the octal mode is You need to use the octal representation of the set of permissions for each group in the chmod command.

The first digit is for the owner of the file, the second digit is for the group, and the third digit is for others.

**The chmod Command for Group of Files:

You can use wildcards with the chmod command to change the permissions of more than one file with a single command. For example, the command chmod go-rw * will remove read and write permissions for group and others for all the files in the current working directory. The command will not affect the permissions of hidden files. Similarly, the command chmod 700 * will set the read, write, and execute permission for the owner and no permissions for group and others for all the files in current working directory. Modifying File Permission:

Deleting Files & Directories:

File ownership provides rights to a user to change the permissions on a file. A


user who creates a file or a directory is its owner. The chown command is used to change the ownership of a file.

When you copy any file or directory, you become its owner provided you have read permissions on that file and read and write permissions on the directory.

*chown command:
The chown command is used to change the ownership of a file or directory. The syntax of the chown command is

chown [login name of the new owner] [filename]. You can use the -R option with the chown command to change the ownership of all
the files and subdirectories of a directory recursively. For security reasons, only the root user can change the ownership of a file.

For every file that is created, UNIX assigns an owner and a group to that file. A user can be the member of more than one group, but when the user logs in to the system, UNIX assigns a default group to the user. The group ownership of the file created by the user is assigned to the default group of that user. The group ownership of a file can be changed by using the chgrp command.

* the chgrp command.:


The chgrp command is used to change the group to which a file belongs. You need to be the owner of a file or root user to change the group of the file. The user must also be a member of both groups. You cannot change the group ownership of a file to a group of which you are not a member. The syntax of the chgrp command is

chgrp [name of the new group] [filename].


You can use the -R option with the chgrp command to change the group of all the files and subdirectories of a directory recursively.

Modifying Default Permission:


Default permissions are specific permissions on files and directories that different users automatically
obtain when they log in to a computer running UNIX or Linux. You can specify default permissions to decide which user can access your files. UNIX will automatically apply these default permissions to files and directories as you create them.

* umask command:
You can use the umask command to view default permissions for different user classes. You can view the permissions in either the symbolic or the octal mode. To check the current umask values in the octal mode, enter umask at the command prompt. UNIX will display the umask values in the octal mode, such as 0022. The first zero identifies the value as an octal number and the last three digits indicate the permissions. The umask value acts as a mask on the permissions. If the output of the umask command is 0022, when we exclude the first digit, 022 remains as the permission value.

Now, you need to subtract this value (022) from full permissions, that is, 777 and the result will be the default permissions for directories. In this case, it is 777022=755. For files, you further need to subtract 1 from each odd number. In this case, the default permission for files is 644.

The default value for umask varies from one system to other. On a Linux system, the default umask value could be 0002.

* default permissions in the symbolic mode:


You can use the umask command with the -S option to check default permissions in the symbolic mode. Calculating default permissions in the symbolic mode is different from the calculation done in the octal mode. If the output of umask -S command is u=rwx,g=rx,o=rx, then these are the default permissions for directories.

You need not subtract it from the full permissions. For files, you need to convert these permissions in the octal mode and then subtract 1 from each odd value.

The umask command is used to change default permissions. You can specify a permission with the umask command, and the umask will subtract the specified permission from the full permission 777 and set the result as the default permission. When you execute the umask 022 command, all new directories are given a default permission of 755 (that is, 777 022) or "rwxr-xr-x" (that is, symbolic

All files created are given a default permission of 644 (that is, 777022 and then
subtracting 1 from each odd number in 755) or "rw-r--r--" (that is, symbolic).

**Default Permission for New Files:


By default, most systems set umask to 022 (octal) or u=rwx,g=rx,o=rx (symbolic). This means that files will be created without the write permission except for the owner. Directories for all the user classes will be assigned the "x" permission , but ordinary files will not be assigned with "x" permissions. Therefore, ordinary files will be created with "rw-r--r--"and directories with "rwxr-xr-x". UNIX or Linux applies a different approach for specifying the "x" permission for files and directories. For directories, the system assumes that the "x" permission is important and assigns it by default. However, for files, the system assumes that all files may not be executable and does not provide the "x" permission for any file created.

Editing Files:
Editing and saving text documents are some of the simple tasks you might have to perform on a regular basis. You may be required to draft a letter or create a soft copy of some document. UNIX provides you with word-processing utilities that enable you to accomplish tasks such as documenting information and editing data. Becoming familiar with the vi editor will help you handle your office tasks efficiently. The is a text editor application that enables you to create, view, or modify the contents of files. In UNIX or Linux, you can use commands to change the text of files without opening them.

vi editor

However, vi editor does not support the formatting options that word processors provide.

* vi command:
The vi command is used to open a file in the vi editor. You can start the vi editor in three basic ways depending on the tasks that you need to perform. Command To Start vi Editor vi Description

A mode is an environment in which the user interacts with the vi editor.

There are two modes in the vi editor: the command mode and the input mode. While working on a file in the vi editor, it is possible to switch between the two modes. It is important to know which mode you are in because a keystroke can have different interpretations in the two modes. For example, in the command mode, if you press dd, the line on which the cursor is placed will be deleted, whereas in the input mode, dd will be written in the file as text at the point where the cursor is placed.

In the , whatever you type on the keyboard is considered an instruction by the vi editor. In this mode, you manage files by performing tasks such as saving a file, navigating through the file, deleting text, finding text, searching text, and saving changes made to the file. Most of the commands, such as the commands to navigate and delete text, used in the command mode do not get displayed when you type them.

command mode

You need not press Enter after you type the commands to navigate and delete text, unlike most of the commands in UNIX or Linux where Enter has to be pressed for the command to be executed.

In , you can edit a file by inserting and appending text. By default, the vi editor opens a file in the command mode.

the input mode

To switch to the input mode, you can press i or a. To change from the input mode to the command mode, you need to press Esc.
When you are not sure which mode you are in, press Esc and you will switch to the command mode. If you are already in the command mode and you press Esc, the vi editor will continue to remain in the command mode.

The vi editor is a line-oriented text editor, which allows you to edit one line at a time.

*Commands to Navigate:
The vi editor enables you to navigate through a file to reach the desired location. You are always
in the command mode when you are navigating through a file. Various commands are used to move the cursor between words, lines, paragraphs, or pages. Use this command To Do This

**Commands for Screen Navigation:


If the file you are editing is spread across multiple pages, the vi editor cannot show all the pages on one screen. Some of the vi commands to move across the screen are given in the table. Use This Command To Do This Ctrl+F Move forward one full screen Ctrl+D Move forward one half screen Ctrl+B Move backward one full screen

Ctrl+U G (uppercase) [n]G

Move backward one half screen Move the cursor to the last line of the file Move to a specific line number, n. For example, 20G will move to the 20th line of the file.

Various commands allow movement across words, lines, and paragraphs.


Use This Command w(lowercase) W(uppercase) b(lowercase) B(uppercase) } { L(uppercase) H(uppercase) To Do This

When you are in the command mode, you can use the i (lowercase), I (uppercase), a (lowercase), A (uppercase), o (lowercase), and O (uppercase) commands to switch to the input mode to insert text in a file. If you press any other key, it will be interpreted as a command by the vi editor.
Use This Command i(lowercase) I(uppercase) a(lowercase) A(uppercase) o(lowercase) O(uppercase) To Do This

After you have inserted text and modified files, you will need to save the changes made. Also, you need to explicitly quit the vi editor. Various commands are used to save and exit the vi editor.
Use This Command To Do This

:w! :w filename :wq :wq! ZZ(uppercase) :q! :e!

Modify Files:
*commands to delete text:
When you are in the command mode, you can use various commands to delete text in the vi editor. To delete single characters, you can use the x command. To delete specific lines or larger portions of text, you can use the d command in combination with different navigation commands. For example, if you need to delete a word, you will type the dw

command.

Various commands are used to delete text


Use This Command x(lowercase) X(uppercase) dw d$ or D dd To Do This

**Number Prefix:
Prefixing any command with numbers will delete a specified range before or after cursor. For example, 5dw will delete five words after and to the right of the cursor. 5dd will delete five lines below and after the cursor.

Commands to Copy Text:


You often need to copy text from one part of a file to another.

You can use the y (yank) and p (put) commands to copy large portions of text and paste it at any other location in the file. There are two ways of pasting text in a file: the p (lowercase) command inserts the copied text after the cursor and

the P (uppercase) command inserts the copied text before the cursor.

Like other commands, y can be also used with different navigation commands and numeric prefixes. Various commands are used to copy or yank the text.
Use This Command yw y$ y} To Do This

yy or Y

Command to Manage Text:

You can also change the casing of letters by using a single command. You need to be in the command mode to use these commands. **Undoing Changes:
While trying to undo the action, you don't necessarily have to be at the same location where the error is present. Also, after you save the file, you cannot undo any changes.

Various commands are used to manage text.


Use This Command u U(uppercase) .(dot) ~(tilde) To Do This Undo the last action

Modify Files using Advanced Techniques:


There are times when you may want to search for multiple occurrences of a specific word in a file without browsing through the entire file. In UNIX, the / (forward slash) character is the search command. You need to be in the command mode to execute the search command. When you enter the forward slash, the cursor will appear at the last line of the screen in the input mode. You then have to type the text you want to search for and press Enter; the cursor will move to the first character of the searched text in its first occurrence.

By default, the vi editor starts the search from the cursor location and proceeds in the forward direction (from left to right).

. To find the next occurrence of the word, the search can be repeated.

Various commands are used to search text.


Use This Command /[text] ?[text] n(lowercase) N(uppercase) / + Enter ? + Enter To Do This Search in forward direction Search in backward direction Repeat the search in the same direction Repeat the Search in the opposite direction Repeat the Search in the fwd direction Repeat the Search in the backward direction

* c command:
The c command, also called the change command, is used to replace text in a document. Just as the delete command needs an object to be defined, the c command also requires the navigation commands. You can also use numeric prefixes with the change command. The general syntax for the change command is

[number]c[navigation command].
When you use the change command, the vi editor deletes the text specified by the navigation command so that you remain in the input mode.

Use This Command cw c$ c0 cc r(lowercase) R(uppercase)

To Do This Change text from the cursor to the end of a word Change text from the cursor to the end of the line Change text from the cursor to the beginning of the line Change the current line

In some UNIX variants, when you use the change command, the vi editor marks the end of the text specified to be changed with the $ sign. The $ sign appears only if the change command is affecting the current line.

**Command to Modify Text while Searching:


You can combine the / (forward slash) and ? (question mark) commands, with the c and d commands, to modify text while searching for a pattern.

For example, the d?root command will delete the text from the current cursor location to the next occurrence of the word root in the backward direction including the word root. And the c/root command will delete the text from the current cursor location to the next occurrence of the word root excluding the word root and you will remain in the input mode to write the text required.

*Commands to join Lines:


If you want to join two consecutive lines, you can use the J (uppercase) command. You can place the cursor anywhere on the first line, and press J to join the first line to the next one. You can also use numeric prefixes to join more than two consecutive lines. For example, 4J will join four consecutive lines starting from the line where the cursor is placed.

Edit text using ex cmd:


The ex commands are used to edit text by referring to lines in a file. You can use the ex commands within the vi editor to modify text. The ex commands operate on lines; that is why ex is also called a line editor and vi is the visual front end to ex. The ex commands you run appear at the bottom of the screen. These commands are taken as instructions by the vi editor to execute an operation just like a command is executed at the shell prompt. When you are in the command mode, press : (colon) followed by an ex command and then press Enter to edit text using the ex commands

. After the command is executed, you return to the command mode in the vi editor. For most of the ex commands, the vi editor displays a message at the bottom of the screen confirming the execution of the command.

**The ex Commands to Refer Lines:


For executing the ex commands, you need to specify the line you need to operate on. If you do not mention the line number, the command will be executed on the line on which the cursor is placed. You can also specify the range of the lines to be edited by the ex commands. The table lists the commands and their description for referring lines. Command Description
:set number Displays all line numbers to the left of the

:set nonumber := :.= :/pattern/= :.,$#

screen. The displayed line numbers are not saved in the file. Removes the displayed line numbers. Prints total number of lines at the bottom of the screen. Prints the line number of the line on which the cursor is placed. The . (dot) operator is used to refer to the current line. Prints the line number of the first occurrence of the pattern. The vi editor starts looking for the pattern from the first line of the file. Temporarily displays the line numbers from the current line to the last line in the file. The $ operator is used to denote the last line in the file. The # operator is used to temporarily display the lines with the line numbers on the left.

Text can be easily edited with the vi commands, but the ex commands are useful when you want to edit numerous lines. You can use the co, m, and d commands for copying, moving, and deleting text respectively. You need to use a colon before entering any ex command. Various ex commands are used to edit text. Command Description
:.d :%d : .,20d :20.30co50 :.co$ :16,22m40

** t command:
Instead of the co command, you can also use the t command to copy text. **Edit Text Using Relative Addressing of Lines:
In place of specifying the lines with absolute line numbers, you can also refer to the lines relative to the current line number. You can use the characters + and to specify the range of lines with respect to the current line. You need to specify a number after the + or sign, and this number will be added or subtracted from the current line. The table lists some of the commands and their description. Command Description :.,.+10d Deletes the current line and 10 lines after the current line. :2,4m.-2 Moves lines 2 to 4 after two lines above the current line. :.,.+20# Displays line numbers from the current line to 20 lines from the current line.

*Text Editing using Search Patterns:


You can also edit files by referring to lines using search patterns. The search patterns used should be enclosed within the forward slash (/). Various commands are used to edit a file using search patterns. Command Description

*The ex cmds to Search & Replace Text:


The search and replace functions can be easily executed with the ex commands.You need to use the s (substitute) command to

. Both operations, search and replace, are carried out using a single command. The new_pattern in the current line. command will replace an instance of old_pattern with

**Interactive Replace: You can use the c option with the search and replace command to confirm
each replacement being done. When you run the :s/old_pattern/new_pattern/gc command, the entire line containing the old_pattern will be displayed and the pattern searched will be marked by a series of caret symbols (^^^^).To make the replacement, you need to press Y or press Enter if you do not want to make the replacement. The vi editor will prompt you to make the replacement till the last matching pattern in the file.

* g (global) command:
You can also replace all occurrences of the old pattern with the new pattern in the specified range using the g (global) command. For example, to replace the old_pattern with new_pattern in the entire file, you can use the :s/old_pattern/new_pattern/g command. Also, to replace all instances in a range of line number 1 to line number 10, you can use the : 1,10s:/old_pattern/new_pattern/g command.

*Cmds to Search using Metacharacters:.


You can use metacharacters to locate files on your system. You can also use these characters with the patterns in the search command. However, there are some commonly used characters in search patterns in the vi editor. Character Usage .(dot) ^(caret)

$(dollar)

*Cmds to copy External Files:


Sometimes, you may want to append the text from a file on a system to a file currently opened in the vi editor. You need to use the r command to copy text from the external file. If you do not specify the location to append the text in the current file, the text will be copied on the line just after the current cursor location. Various commands are used to copy files in the vi editor. Cmds Description

*Cmds to Run Shell Cmds:


While working in the vi editor, you can run UNIX commands without exiting the editor. The !

(exclamation mark) command is used to run shell commands within the vi editor. The UNIX commands can be accessed by using the :![command] command.
The result of the execution of this command is displayed at the bottom of the screen. There should be no space between the exclamation mark (!) and the command you want to execute.

For example, the command

:!ls

will list the contents of your current working directory. You can also combine the :r command to append the result of the shell command into the file that is currently opened in the vi editor. For example, the :r !date command will add the result of the date command in the file after the line on which the cursor is placed.

**Switching back to vi editor:


After running shell commands using !, you need to press

Enter to return to the vi editor.

**Run Multiple Shell Cmds in the vi Editor:


If you want to run multiple commands one after the other, you can create a shell within the vi editor using the :sh

command. When you enter the :sh command, a prompt appears at the bottom of the screen where you
can run the shell commands. You need to press Ctrl+D to exit the shell and return to the vi editor. By using

the :sh command, you cannot append the result of the shell commands to the file currently opened in the vi editor.

Managing Process:
Consider a scenario where the application you are working on is not responding. Since you are working in a multiuser environment, you cannot reboot the system to restart the application. You can resolve this issue by identifying and managing the process that is running the application.

Display Process:A process is the instance of a program started by a system when you execute a command. Any program, command, or application that you run on the system will have a process associated with it.

. The process terminates when the task gets completed. At any given time, multiple processes run on a computer depending upon the tasks, programs, and applications running on the computer. Each process is associated with various attributes that provide details about the process.

**Process ID: Every process running on the computer will have a process ID (PID), which is the
unique ID assigned to the process by the operating system. Process IDs are assigned in a sequential order. When a process stops, the ID that is being used by this process can be assigned to a new process by the system. Usually the range of PIDs is 1 to 32768 and the range can vary depending on the hardware architecture of the system. ** are processes initiated by a user to execute a command. Users can control only their processes by terminating or suspending them.

User processes

** system processes : In addition to the processes invoked by user commands, there are
also a few system processes such as init and inetd, running on the computer that help the operating system function. Only a root user can control the system processes. Additionally there is the kernel

process, which even a root user cannot control.


** A is a process created by another process. It is created when you execute a non built-in shell command and terminates after the execution of the command. The process that creates the child process is called the parent process. Therefore, every process, along with a PID, also has a parent process ID (PPID). The first user process that started when the computer boots is called init. The PID of init is always 1. The init process acts as the parent process for all the other user processes.

child process

A process can be in four different states.


Process State Running process Sleeping process Zombie process Description

Suspend process

* ps command:
You can use the ps command to view the processes running on a UNIX or Linux computer. The information about all the processes running on a UNIX or Linux computer is maintained by the kernel. The ps command displays information about PIDs, the terminal ID (TTY), the amount of CPU time in minutes and seconds that the process has used, and the name of the command that started the process.

The ps command along with its options when used displays other attributes such as memory usage, CPU usage, and many others.

** attributes of a process:
The attributes of a process help you examine and manage the process. These attributes appear as the column headers when you run the ps command. The table lists some attributes and their description associated with the processes running on your system.

Attributes
uid pid ppid sz stime tty time etime cmd pcpu

Description
Represents the user ID (UID) Represents the process ID (PID) Represents the parent process ID (PPID) Represents the size of the process in virtual memory (SZ) Represents the starting time of the process (STIME). It is given in hours, minutes, and seconds. If the STIME is more than 24 hours, the UNIX system converts it into months and days. Represents the controlling terminal of the process (TT) Represents the execution time of the process (TIME) Represents the total elapsed time of the process (ELAPSED) Represents the command that started the process (CMD) Represents the ratio of CPU time used by the process in proportion to the CPU time available at the time of execution (%CPU)

vsz args

Represents the total size of the process in virtual memory, in kilobytes (VSZ) Represents the name of the command associated with the process along with all the arguments (COMMAND) user Represents the name of the user who has run the command (USER) rss Represents the amount of physical memory allocated to a process. The size is represented in kilobytes (RSS) The value of these attributes may vary depending on the architecture of the system.

Options of the ps cmd:


There are various process attributes such as CPU usage, memory usage, and controlling terminals that you can view by using the different options of the ps command. The output of the different ps command options may vary depending on the UNIX platform that you are using. Some of the most commonly used options of the ps command are listed in the table.

Options
-a

Description

Selects all processes for the current user except those not associated with a terminal. -e Selects all processes for all users, including those not associated with a terminal. -f Displays the user ID, PID, PPID, CPU usage, process start time, controlling terminal, elapsed CPU usage, and the associated command of the currently running processes. -l Displays a long list of the common process attributes such as status, user ID, PID, PPID, flags, CPU usage, priority, current memory usage of the process, and the associated command of the currently running processes. -u Selects the processes run by the processes associated with the specified user. -o format Displays the processes in the user-defined format. You need to specify the format in the form of a commaseparated list. For example, the command ps -o pid,user,comm will display the processes with the column header as PID, USER, and COMMAND. Note: The output of the different ps options may vary depending on the UNIX platform that you are using.

**Display Process: * kill command:


You can use the kill command, along with the PID of the process to be terminated, to terminate a process. If any process is slowing down your computer, you can terminate it by using the kill command and thereby improve the performance of your computer. The kill command sends a signal to the specified process and terminates it.

**Recommended Method:
Terminating a process by using the kill command is not the recommended method of stopping a process. The recommended method is to close the application that started the process. Terminating a process by using the kill command can lead to data loss.

**Control of Process: You can terminate only those processes that you have started. As a user, you
do not have control over the processes started by the other users

A signal is a message sent to a process to control it.

Each signal has an associated default action that is performed on the processes you specify.

If the process does not handle the specified signal, the default action is to kill the process.

Signals are useful in terminating processes, and you can use the kill command with signals to terminate processes, based on your needs.

**Termination of a process: Ungraceful termination of a process is when the process does not
handle the signal sent to it properly and is killed due to the default action of the signal. A process is terminated gracefully if the process handles the signal properly and is terminated. Common Signals: Signal -1 -2 Description Called SIGHUP, this signal hangs up the execution of the process that you specify. For server processes, this signal causes the process to reread its configuration files. Called SIGINT, this signal interrupts the execution of the process that you specify. However, if you do not have the prompt available to execute the kill command, you can use the Ctrl+C keys on the keyboard instead of the kill -2 command. Called SIGQUIT, this signal terminates the execution of

-3

-9

-15

the process that you specify in a graceful manner. Called SIGKILL, this signal terminates the execution of the process that you specify in an ungraceful manner. This signal cannot be ignored by a process and causes it to terminate. Called SIGTERM, this signal gracefully terminates the instances of the software associated with the process that you specify. This is the default signal sent by the kill command.

Managing Jobs:
When working with multiple applications, you may need to manage tasks such as viewing the status of applications, switching between applications, and starting new applications. These job management skills help you perform multiple tasks using applications.

Contol Jobs:
A

job is a process that runs when a command is executed from the command line.

A job refers to a process that is a child of the current shell. You can run multiple processes from a shell; the shell assigns each job a unique job ID and uses this number to access and control jobs.

**Job Execution in a shell: When you enter a command on the command line, the shell
starts a process, which executes that command. If the resulting process is time consuming or user interactive, you have to wait for that process to complete the execution of the command to get the prompt back. This is the default way in which the shell executes processes and in this method, you can run only one process at a time; that is, only one job can be executed at a time.

**Process vs Job:
You know that the shell starts a process to execute a command. For many commands, several processes are created in order to complete the execution of the command. The collection of these processes to carry out the execution of a single command belongs to the same process group. Some shells such as C shell and KornShell have the ability to manage the process or group of processes created to execute a command by referring them as an identifiable unit

called job. The concept of job in these shells allows you to move the processes to background and foreground. You can also suspend and resume the process execution. Jobs are active as long as the shell that started the job is active.

Terminating the shell results in the termination of the jobs started by the shell. If you log out when there are active jobs, the shell will warn you once.

**A foreground job is a process initiated by the current shell that is user interactive; users
can communicate with the job using the screen and the keyboard.

By default, a job will launch in the foreground. There can be only one foreground job at any time, and

the shell prompt will be busy until the execution of the foreground job is complete.

** A background job is a process that is initiated by the shell but does not support
any user interaction.

If you need to run multiple jobs, or a time-consuming job, while you continue working at the command prompt, you can start them in the background by adding the & (ampersand) symbol at the end of the command. If you have started a job in the foreground, you can send it to the background at any time and get the prompt back.

**Background Job Behavior:


When a job is started in the background, the shell prints the job number and the process id on the command line and the prompt is available for the next command to execute. You can start any number of jobs in the background. After completion of a job in the background, the shell removes that job and prints a message at the command prompt only after you execute another command.

. If you want to run jobs in the background, you need to redirect the output of such jobs to a file.

** A suspended job is a process that is temporarily stopped from execution. When you
suspend a job, it is neither in the foreground nor in the background, but it still exists with its job number and other attributes such as the status and the command running the job. The status of the job is changed to Stopped. To suspend a foreground job, you can press Ctrl+Z to send a STOP signal to the job. The shell will display a message that the job has been stopped and gives you the command prompt to proceed with other tasks. You can restart the suspended job by bringing it to the foreground or sending it to the background.

**Execution of the jobs cmd: You cant execute the jobs command when a foreground job is running; therefore, the output of the jobs command displays either the jobs running in the background or the suspended jobs.

* jobs command:
The jobs command displays the status of all running jobs that are started from the current shell. The syntax of the jobs command is jobs [-lp][%job ID] where l and p are the options that are used to display the different attributes of the jobs, and the job ID is the job number assigned to the job by the shell. The jobs command without any option will display the job number, current job, state, and the command that started the job.

***Attributes of a Job:
Jobs have several attributes that provide information about a job. This information is helpful in managing the jobs. The table lists the attributes of a job and their description.

Attributes
job-number current

Description
The ID number assigned to the job by the shell. A character, represented by the + sign or sign. The character + is assigned to the current job. The current job is the job that jobs commands will act upon by default if no job identifier is provided. This is typically the job that was most recently managed in some way. The current attribute displays a + for the current job and a for the job that will become current if the current job stops. This is typically the job that was managed just previously to the current job. The characters + and are assigned to one job at a time. Therefore, for other jobs, the current attribute will be blank. The state of the job: running, done, or stopped. The command associated with the job.

state command

**Options of the jobs command:


The jobs command has options that enable you to view different attributes of the jobs. Some of the common options and their description are given in the table. If You Need To Use This Option Display the job number, current job, process group -l ID, state, and the command that started the job Display only the process ID of the jobs -p

You can use the fg command to bring a job to the foreground from the background and the bg command to bring a suspended job to the background.

The syntax for the fg command is fg

%[job identifier] and the syntax for the bg command is bg %[job identifier].
The job identifier can either be a job number or the character that defines the current attribute of the job, which is the + and sign. The + sign determines the current job and the sign determines the previous job. If you do not

include the job identifier argument, the command will act on the current job.

**Schedule Jobs:
Scheduled jobs are commands that are executed at a specific time. You do not have to run scheduled jobs
manually; they will run at the specified time even if you are not at your system. Scheduling jobs is useful for repetitive execution of commands. You can schedule jobs using the at command or the crontab command.

Jobs that are user interactive are not scheduled using the at or the crontab command. The output of the scheduled job is not displayed on standard output (such as CLI).

* at tool:
The at tool is used to execute commands at a specified time. The at tool reads commands from the standard input (for example, keyboard); these commands are to be executed at a later time. The at tool can also read commands from a file. For example, you can specify a list of commands in a file and schedule to read the file using the at command. The commands scheduled using the at tool will be executed only once.

* at command:
The at command is used to specify a job to be executed at a later time by the at tool. The syntax of the at command is at [time]. You can specify the time in the 12-hour or 24-hour time format. If the time is already passed, the job will be executed the next day at the same time. You can also specify the date on which you want the job to run in the MMDDYY, or MM/DD/YY, or MM.DD.YY format. The syntax of the at command to read commands from a file is at -f [filename] [time]. You can also use the < operator to read the commands from a file. For example, the command at 01:35 AM < atjobs will read and execute the commands listed in the atjobs file at 01:35 A.M.

** In UNIX, you may not get a message to confirm the jobs scheduled using the at command

**The at Prompt: When you run the at command, you will get the at prompt (at>) where you can
enter the series of commands to be executed at the time you specify with the at command. After entering all the commands you want to schedule, press Ctrl+D to return to the command prompt. A message is displayed to confirm that the at command has scheduled a job at the specified time..

**Different Ways to represent Time in the at cmd:


The table lists some of the time formats which you can use with the at command. Time Description 8:30PM JUL 20 The job will run at 08:30 P.M. on July 20 in the current year. If the date is passed, the job will run the next year at the specified time. 2030 JUL 20 2010 or 2030 07/20/2010 The job will run at 08:30 P.M. on July 20 in the year 2010. NOW + 6 MINUTE The job will run after 6 minutes from now. You can also use MIDNIGHT (12:00 A.M. or 00:00) and NOON (12:00 PM) in place of NOW. 4PM + 3 DAYS The job will run at 4 P.M. after three days from the current date. You can also use MINUTES, or HOURS,

1AM TOMORROW

or WEEKS in place of DAYS. The job will run the next day at 1 A.M. You can suffix the time with TODAY also.

Note: Time specified with the at command can also be mentioned in lowercase. You can use the at -l command to view the list of jobs scheduled using the at command. The output of the at -l command shows you the job ID of the job and the time at which it is scheduled to execute. You can also use the atq command to list the jobs scheduled by the at command.

* the atrm command:


You can use the atrm command to remove the jobs scheduled by the at command. The syntax for removing jobs is atrm jobid, where jobid is the job ID of the job that you want to remove.

Some systems may not support the atrm command. You can then use the

at -r jobid command to remove the jobs.

** crontab file
The crontab file is a configuration file that stores scheduled jobs and information about when the jobs need to be executed automatically. You can schedule a job to run more than once by making an entry in the crontab file. The file has a specific format to store information about scheduled jobs, such as the minute, hour, days of month, month, day of week, and the command.

The crontab file contains a table with six columns. Column Description Minute Hour Day of month Month Day of week Command

**Columns Values of the Crontab File:


For the first five columns, you can specify the asterisk (*) symbol, which means every. For example, if there is an asterisk value with the hour column, the command will be executed every hour. You can have a list of numbers that are separated by commas. For example, the entry 1, 4, and 10 for the year column will run the command on months 1, 4, and 10 of the year. You can also specify a range such as 130 for the minutes column; this means that the command is run every minute from one minute after the hour through 30 minutes after the hour. Example: 0 0 1,8,15,22 * * /home/userA/job1 . The following entry in the crontab file will execute job1 on days 1, 8, 15, and 22 of every month at the zero minute and zero hour of the day.

*cron Daemon:
A system daemon called cron executes the jobs listed in the crontab file. The cron program uses the
crontab file to read the information about scheduled jobs and saves it in the system memory. The system runs the cron program periodically (usually every minute) to get the information about any job that is scheduled to run at this minute. If there are any entries in the crontab file, the jobs are executed automatically.

* crontab command:
The crontab command is used to modify the crontab file. The crontab file is a system file that should not be shared. Users can create, edit, or delete their crontab files by using the crontab command that specifies the information of the jobs that need to run for these users. The crontab command is executed from the command line with the users permission as assigned on the shell, just as the other commands are executed from the command line. The crontab file specifies the information of the jobs that need to run for these users.

**Options of the crontab cmd:

Option -e

Description The crontab -e command helps in editing and replacing the crontab file. The crontab -e command loads the crontab file in the text editor available in your system, so that you can use the editing options of the editor to edit the crontab file. You can use the export EDITOR=vi command to load the crontab file in the vi editor. When you complete the editing, the crontab command sends a signal

-l -r

to the cron program to read the recently updated crontab file again. If the crontab file does not exist for the user, the crontab -e command will create the crontab file. Lists the current crontab file entries. Deletes the current crontab file.

Managing Communication:

Consider an office scenario where all computers that your team members are using are connected through a network. Usually, in such setups, information and files are shared over the network. Communicating with other computers in such a scenario is important for you to be able to send messages and access shared files and resources. You can use commands in UNIX or Linux to communicate with other computers in a network.

Access Remote Computers:


The telnet utility allows you to connect to and use the applications and resources of another computer. Once connected, you can use the remote system in the same way you use your local system. You can work with files and directories and manage processes and jobs on the remote computer.

* telnet command:- The telnet command is used to log in to the remote system.
The syntax of the telnet command is telnet

[argument].

The argument to the telnet command is the machine name of the remote system or the IP address. You need to enter a username and password to log in to the remote system. In the telnet connection, the commands you enter are sent to the remote system and the output from the remote system is available on your screen.

**telnet Session cmds:


You can use telnet commands without any argument to start the telnet service on your system and open a telnet> prompt where you can enter telnet session commands. Various commands can be used in a telnet session.

Option open

Description

close quit display ? or help z

** Secure Shell (SSH):

Secure Shell (SSH) is a protocol that provides a secure alternative to telnet. When you communicate with another computer using telnet, others can read the communication. SSH fixes this problem by encrypting your login ID and password as well as your communication with other computers. You should always use ssh for logging in to a remote system.

SSH Process:
The first time you connect to a remote computer, it authenticates your computer by scanning your computer's digital fingerprints and adds your computer to its list of known hosts. This happens only once. Once you are added to a computer's list of known hosts, you can connect to that computer using your password.

* ssh command:
The ssh command is used to securely log in to the remote system. The syntax of the ssh command is ssh [hostname or IP address of the remote machine]. You need to provide either the hostname or the IP address of the remote machine you want to connect to.

You do not have to provide the username while connecting to the remote system using the ssh command. This is because the ssh command uses the username that you are currently logged in with. You can use the -l option to log in with a different username using the syntax ssh -l [username][ hostname or IP address of the remote machine].

**Transfer Files:
File Transfer Protocol (FTP) is used to transfer files among computers. The computer where
the files are stored is called a remote computer, and the computer that requests files from the remote computer is called a local computer. A local computer needs to establish connection with the remote computer before transferring files

. . When you connect to the remote computer using FTP, you will connect to the home directory of the remote computer.

* ftp command:
The ftp command is used to open a session on the remote computer to transfer files. You can type the name or the IP address of the remote system you want to connect to with the ftp command. You can also run the ftp program and identify the remote system.

When you enter the ftp command, the ftp server will prompt you for a valid username and password, which are required to establish the ftp connection.

**Anonymous FTP:
There is a special login for ftp that allows users to read files from the specific directories on the remote hosts. The anonymous login has limited access to the remote system where some remote hosts may assign the write permission to the user. The anonymous login is useful to distribute publicly available software or to provide access for the archived files. You can use anonymous as the username and any string as the password to connect to the remote host.

**Cmds used in an Ftp Session:

After you established the ftp session, you can use the mget or the mput command to transfer multiple files. Use mget * to transfer all files from the current remote directory to the current local directory. The FTP service prompts you to confirm each file transfer by typing Y for yes or N a no. If you want to cancel file transfer, press Ctrl+C, and enter Y to continue canceling file transfer. You can use the quit command (or bye or exit) command to close the ftp connection. You can enter different commands once the ftp connection is established. The table lists the commands for file and directory management on the local system as well as on the ftp server.

If You Need To

Use This Command

Change the working directory on the remote computer cd [path to remote directory] Change the working directory on the local computer lcd [path to local directory] List the contents of the current working directory of the dir remote system Create a directory on the remote system mkdir [name of the directory] Remove a directory on the remote system rmdir [name of the directory] Rename a file on the remote system. This is same as rename [old filename] [new filename] using the mv command to rename a file on the local system. Delete a file on the remote system. This is same as using delete [filename] rm command to remove one file on the local system. Delete multiple files on the remote system. This is same mdelete [filename of multiple files separated by space] as deleting multiple files using rm command on the local system. Quit ftp connection. bye or quit The table lists the commands used in the ftp connection for the type of file transfer.

If You Need To
Set the file transfer type to ASCII Set the file transfer type to binary Print a "#" mark each time a data block is transferred

Use This Command ascii binary hash

* get and put commands:


The get and put commands are most commonly used to transfer files in the FTP connection. The get command is used to transfer files from a remote system to the local system. The syntax of the get command is get [remote file] [local file]. The put command is used to transfer files from the local system to the remote system. The syntax for the put command is put [local file] [remote file].

**Ways to Use get & put cmds:


There are different ways to transfer files between the remote and the local system using get and put commands. The table lists the commands used in the ftp connection to transfer files from a remote system to local system

If You Need To
Transfer a file from the current remote directory to the current local directory Transfer a file from a specific location on the remote computer to the current local directory Transfer a file from a specific location on the remote computer to a specific location on the local computer Transfer and rename a file from the current remote directory to the current local directory

Use This Command


get [remote file name] get [path to remote file] get [path to remote file] [path to local directory] get [remote file name] [new local file name]

The table lists the commands used in the ftp connection to transfer files from the local system to the remote system.

If You Need To
Transfer a file from the current local directory to the current remote directory Transfer a file to a specific location on the remote computer from the current local directory Transfer a file to a specific location on the remote computer from a specific location on the local computer Transfer and rename a file from the current local directory to the current remote directory

Use This Command


put [local file name] put [local file name] [path to remote directory] put [path to local file] [path to remote directory] put [local file name] [new remote file name]

*SFTP Command:
To transfer your files securely, you can use an enhanced and secured version of FTP, called Secure FTP (SFTP). SFTP is a combination of regular FTP and SSH. SFTP encrypts the contents of files before they are transferred. Besides encrypting the file, SFTP also provides a secure login, by not transferring the password in clear text.

For security

reasons, SFTP is the preferred method of transferring files.

* sftp command:
The sftp command is used to transfer files over an encrypted SSH protocol. Because sftp uses the ssh protocol for communication, you do not have to provide the username while connecting to the remote system using the sftp command.

**The sftp cmd in Non-Interactive mode:


You can also download a file from the remote system to the local system using the sftp [user@]host:[file on remote system] [file on local system] command. For example, the command sftp netserver2:file1 file1.cp will copy file1 from the current working directory of the remote system to the current working directory of the local system with the filename file1.cp. You need not to establish the sftp connection with remote system in this case. **Cmds used in a SFTP session: SFTP does not support all the same commands as ftp. For example, sftp does not have mget and mput commands, but you can use wildcards with the get and put commands to transfer multiple files. For example, the command get *.txt will transfer all files with the .txt extension from a remote system to the local system. The commands that have the same functionality in the ftp and sftp connections are: bye, cd, get, lcd, mkdir, put, quit, rename, and rmdir. The table lists some of the commands used in an sftp connection for files and directory management and some miscellaneous commands that are not available in ftp

If You Need To
List the contents of the directory specified by the path on the local system. If the path is not mentioned, the contents of the current working directory on the local system will be listed. Create a directory on the local system. List the contents of the directory specified by the path on the remote system. If the path is not mentioned, the contents of the current working directory on the remote system will be listed. Delete a file on the remote system. Quit sftp connection. Display the help on commands used in the sftp connection. Execute a command on the local systems shell. .

Use This Command


lls [path]

lmkdir [name of the directory] ls [path]

rm [name of the file] bye or exit or quit ? or help ! [command]

* SCP:
Secure Copy (SCP) is a secure method of transferring files between computers. It is secure because it encrypts the contents of the files and your password using SSH before transferring them. Unlike FTP or SFTP, SCP does not start an interactive session on the remote computer when transferring files.

A remote computer maintains a list of trusted and known hosts. When you perform the secure copy for the first time, the remote computer scans your computer's digital fingerprints and adds your computer to its list of known hosts. Thereafter, each time you use the command, you only need to provide your password to complete the transfer of files.

* scp command:
The scp command is used to copy files on a network. You can transfer a file from a host to a remote system, as well as from a remote to a host system using the scp command. The scp command uses the ssh protocol for data transfer and uses the same authentication and security as ssh. The syntax of the scp command is scp [option] [[user] host1 : file1 ] [[user] host2 : file2 ].

The options used with the scp command are listed below.
If You Need To Keep the modification time, access time, and the mode of the copied file same as the original file Disable the progress bar Recursively transfer the directories Use This Command -p -q -r

**Different ways to use SCP:


You can specify the filename and the path of the file you want to transfer with the scp command. The table lists different ways to use the scp command.

If You Need To Transfer a file from the current local directory to the remote home directory Transfer a file to a specific location on the remote computer from the current local directory Transfer a file to a specific location on the remote computer from a specific location on the local computer Transfer and rename a file from the current local directory to the current remote

Use This Command scp [local file name] [your login id]@[remote computer address] scp [local file name] [your login id]@[remote computer address]:[path to remote directory] scp [path to local file] [your login id]@[remote computer address]:[path to remote directory] scp [path to local file] [your login id]@[remote computer address]: [new remote

directory Transfer every file from the current local directory to the current remote directory

file name] scp -r [your login id]@[remote computer address]

----------------

You might also like