introduction to linux instructor: bennett m. tanyag unit - 2
TRANSCRIPT
UNIT 2: Using the Linux System
Learning ObjectiveTo learn how to log in and outTo understand the command formatTo learn about input and output redirectionTo learn to use some basic commandsTo understand the usage of pipes and filters
Logging in and out
Users identify themselves to the system through the login procedure.
Every Linux system has a system administrator who administers, monitors and manages the system.
The system administrator creates accounts and provide login names for the accounts.
The administrator also provides the initial password for every account
Each user of the system is identified with a unique account. The users are identified by their login name, and protection is
provided through user passwords.
A typical login prompt looks like this:mycomputer login:
mycomputer is the name of the computer Every computer is uniquely identified ba a name given to it. The user enters the login name Linus and immediately the
password prompt is shown as mycomputer login: Linuspassword:
The password entered is not echoed to the screen If the password is incorrectly entered, a message appears
login incorrect
Logging in and out
UNIX systems are case sensitive systems User is logged in to the system, when both the login name and
password match The space where the user is logged into is called as the home
directory When a user logs in, the command prompt appears as follows
/home/linus The above is the home directory of the user. The shell is now in command, waiting to take input from the user The default shell is the bash (Bourne Again Shell) Other shells are csh (C shell), ksh (Korn shell), sh (shell) and tcsh
(enchanced C shell).
Logging in and out
Before leaving the system, it is important that we log out of the system.
This can be accomplished by typing either Logout Exit Ctrl-d
Anyone of the above can be used to log out of the system.
Remember to log out always to prevent other users from messing around with your files.
Logging in and out
Command Format
A command is a program that does a specific job There are hundreds of commands and most of them have options
available, yielding thousands of possibilities. The command man, meaning manual pages, is used to find the
details about each command To run even the man command we need to know the structure of a
Linux command linecommand –opn1, opn2 …,arg1, arg2… argn
Where Command is the command name Opn1 to Opnn are the range of n options possible for that command Arg1 to argn are the range of n arguments that may be passed to the
command.
passwd command
The command to change the password is passwd./home/Linus$ passwdChanging password for Linus(current) UNIX password: TorvaldsNew Unix password: HelsinkiRetype new Unix password: Helsinki/home/Linus$ The password entered by the user cannot resemble the old
password Some of the error messages given by the system are:
BAD PASSWORD: is too similar to the old one. BAD PASSWORD: it is too short BAD PASSWORD: is too simple
BASIC UNIX COMMANDS
To execute a UNIX command, simply type the name of the command at the UNIX prompt (much like in DOS) followed by the Enter or Return key ().
Some basic UNIX commands:1. date
The date command gives the user the current date and time. Only the system administrator (or the superuser) can change the system date and time.
Example :$ date Mon Oct 25 19:24:50 PST 1993
clear
The clear command will simply clear the screen and move the $ prompt to the upper left-hand corner of the screen. This is similar to the cls command in DOS.
BASIC UNIX COMMANDS
who The who command gives a list of users currently logged in the system. The output will give the user name or account name together with the TTY or terminal number (the exact terminal being used by the user).
Example :
$ who root tty0user1 tty17user5 tty6mitch tty8
BASIC UNIX COMMANDS
whoamiThe whoami command gives the account name of the user currently logged in at a particular terminal. For example, if someone happens to see a terminal left on with someone logged in, typing whoami at the prompt will give the account name of that user.
Example :$ whoami mitch
BASIC UNIX COMMANDS
bcThe bc command will enable user to access the on-line calculator of UNIX.
Example:$ bc 13 + 18 312 * 8 16<^D>$ _
BASIC UNIX COMMANDS
cal or cal year or cal month year
The cal command gives the user the calendar for the current month and year. The cal year command will give the user the whole calendar for the year specified. The cal month year command will give the calendar for the specified month and year.
BASIC UNIX COMMANDS
Example :$ cal
JulySun Mon Tue Wed Thu Fri Sat 1 23 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 30 31
BASIC UNIX COMMANDS
banner
The banner command prints large letters on screen.Example:$ banner HI# # ## # ## # ## # # # # # # ## # ## # ## # #
BASIC UNIX COMMANDS
BASIC UNIX COMMANDS
ls commandThe ls (list) command lists the name of files in a given directory. Typing ls without any option and argument at the prompt gives the user a listing of the files in columns and in alphabetical order.
Example:$ ls arithmetic chapter1.doc eric.letfigures memo.to.henry report.1sample.c test.program.c
Some options and arguments of the ls command:The l option gives a long listing of the files that includes information such as the size of the file, the owner of the file, the date and time the file was last modified, etc.Example:
$ ls -l -r-xr-xr-x 1 linus staff 4222 Jan 13 11:10 arithmetic-rw-r----- 1 linus staff 512 Dec 1 8:00 chapter1.doc-rw-rw-r-- 1 linus staff 1020 Feb 9 10:15 eric.letdrwx------ 2 linus staff 2023 Jun 4 9:31 figures
BASIC UNIX COMMANDS
The a option gives a listing of all the files including hidden files (those which start with a ".")Example:
$ ls -a . .. .profile.sh_history arithmetic chapter1.doceric.let figures memo.to.henryreport.1 sample.c test.program.c
In some UNIX versions, the ls command will list the files one line per file. Use the C option to list the files by columns.
BASIC UNIX COMMANDS
The ls command can also have arguments.
Example:$ ls arithmetic arithmetic$ ls *.c sample.c test.program.c
BASIC UNIX COMMANDS
The ls command can also have a combination of arguments and options.
$ ls -l sample.c
-rw-r--r-- 2 linus staff 20432 Oct 25 10:30 sample.c
Multiple Options
The rule of separation can be applied in case of multiple options. A dash "-" symbol should precede an option and a space should separate each option.
Example:$ ls -a -l
This will give you a long listing of all the files.
A user may also combine multiple options in a single dash symbol.
Example:$ ls -al
Take note that there should be no space between the a and the l.Also, the ordering of the options is not important. Therefore, ls -l -a and ls -la will also work.
Multiple Argunments
The rule of separation is also applicable in case of multiple arguments. A space separates one argument from another.
Example:
$ ls -l arithmetic eric.let sample.c
Multiple Commands
A user may enter several commands at a single UNIX prompt. In such a case, a semi-colon (;) delimits each command unit. The commands will then be executed consecutively.
Example:
$ ls; ls -a; ls -l
UNIX FILENAMES
UNIX filenames can have fourteen (14) characters consisting of letters, numbers, periods, and/or dashes.
UNIX filenames do not follow the concept of file extensions as in DOS. In other words, the period is just like an ordinary character.
Example:letters.to.me and c.prog.linus are valid UNIX filenames
UNIX filenames are case sensitive unlike in DOSExample:
payroll.1994, PAYROLL.1994, and Payroll.1994 are all different filenames in UNIX.
WILD CARDS Like in DOS, UNIX also uses wild cards in
selecting certain files to be operated on. In UNIX systems have 3 wild cards, these are:
* asterisk ? Question mark [ ] brackets
1. The Asterisk "*"
The asterisk wild card will match any number of characters that occur at the designated position in the pattern.
Examples:1. ch* represents all filenames that start with ch followed by any
number of characters.$ ls ch* chapter1 change.doc cherry_text
2. *er represents all filenames that start with any number of characters and end with an er.
$ ls *er files.october system.ver
3. *itch* represents all filenames that start with any number of characters, followed by the string itch and then followed by any number of characters again.
$ ls *itch* ritchie.doc itchy.let mitch.let
In DOS, *.* represents all files (any name and any extension). This is not applicable in UNIX since there are no such things as file extensions. Instead, *.* will be interpreted by UNIX as any file with a period in its filename. To represent all files in UNIX, just simply type *.
2. The Question Mark "?"
Like in DOS, the question mark will match any single character. Examples:
1.?ark represents all filenames that start with any single character followed by the string ark.
$ ls ?ark mark lark 5ark ark
2. henry? represents all filenames that start with the string henry and ends with any single character. $ ls henry?
henry1 henry2 henryx henry3. letter?.doc represents all filenames that start with the
string letter, followed by any single character, and then followed by the string doc.$ ls letter?.doc
letter1.doc letters.doc letter.doc
The Square Brackets "[ ]"
The square brackets are similar to the question mark in the sense that it will match any single character. However, square brackets provide the user with options in choosing the character.
Examples:1. letter[246].doc represents all filenames that start with the string
letter, followed by either a 2, 4, or 6, and then followed by the string.doc.
$ ls letter[246].doc letter2.doc letter4.doc letter6.doc
2. memo_[a-c] represents all filenames that start with the string memo_ followed by any letter from a to c.$ ls memo_[a-c].doc
memo_a.doc memo_b.doc memo_c.doc3. letter[3-5] represents all filenames that start with the
string letter followed by any number from 3 to 5.$ ls letter[3-5]
letter3 letter4 letter5
4.message[4-6m-o] represents all filenames that start with the string message followed by any number from 4 to 6 or any letter from m to o.
$ ls message[4-6m-o] message4 message5 message6
messagem messagen messageo
5.notice[1-2][b-d] represents all filenames that start with the string notice followed by any number from 1 to 2 and then by any letter from b to d.
$ ls notice[1-2][b-d] notice1b notice1c notice1dnotice2b notice2c notice2d
6.element[!5-9] represents all filenames that start with the string element and NOT followed by any number from 5 to 9.
UNIX FILE SYSTEM The UNIX File System organizes all files
into a single hierarchy of directories just like in DOS. In fact, DOS copied its directory structure from UNIX. The highest directory level is called the root (/). Take note that DOS represents its own root directory by a backward slash (\).
The root directory contain numerous subdirectories, many of which are standard in UNIX systems. Some of UNIX’s important subdirectories are:1.bincontains the executable programs that make up most of the important UNIX system commands2.etccontains important system administration programs and data files3.libcontains some of the libraries that programming language compilers use
4. dev
contains UNIX device files5. tmp
contains temporary files to hold data for a short period of time
6. usr
contains files of general use to the users of a UNIX system
Path
A file could be in any directory and its location is called its path.
a chain of directory names that tell UNIX how to find a particular fileAn Absolute Path traces the location of the file from
the root directoryExamples:
/usr/linus/docs/mydoc/usr/linus/letters/letter1
A Relative Path traces the location of the file starting at the current working directory
Examples:Assuming that the current working directory is docs:
../letters/letter1
../letters/letter2
Files and Directories
What are files?Everything in UNIX is a fileA file is sequence of bytesFiles a re placeholders for dataData can be simple ASCII text, formatted
documents, source, programs, executable files, pictures etc.
What are Directories? A directory is a placeholder for files. Directories can hold any kind of files. UNIX directory is a special kind of file that maintains a list of all
the files in it. The organizational scheme is powerful, as a file in a directory
can be another directory. This would be called a subdirectory of the original directory. Directories are the branches of the tree and the files are the
leaves of the tree. As a tree has both internal and leaf nodes with a root at the top,
in UNIX too we have a root that is the basis of the structure of directories and files.
UNIX FILE OWNERSHIP AND SECURITY In a multi-user environment like UNIX, each file has a designated
owner.Example:$ ls -l
-r-xr-xr-x 1 Linus staff 4222 Jan 13 11:10 arithmetic-rw-r----- 1 Linus staff 512 Dec 1 8:00 chapter1.doc-rw-rw-r-- 1 Linus staff 1020 Feb 9 10:15 eric.letdrwx------ 2 Linus staff 2023 Jun 4 9:31 figures
Displayed above is a sample output of the ls -l command. There are eight (8) columns shown.
Explanation of the ls-l Output -rw-rw-r-- 1 mitch staff 1020 Feb 9 10:15 eric.let
Column ExplanationFirst Security Access ModesSecond Number of LinksThird Owner of the File or
DirectoryFourth Group where the Owner
belongsFifth Size of the FileSixth Date the file was last
modifiedSeventh Time the file was last
modifiedEight Name of the File or Directory
What Security Access Mode means: - r w - r w - r - -
The security access mode consists of four components:
First Character A “d” means this is a directory, while a "-" means this is just a regular file (text or executable file).
Next Three The Security Access Mode for the owner of the file or directory.
Next Three The Security Access Mode for the group mates of the owner.
Last Three The Security Access Mode for the other users in the system who do not belong to the owner's group.
The security modes
"r" stands for the read permission. This is for printing files, copying files, and displaying the contents of the file. For directories, this allows the user to list the files (by using the ls command) that are in the directory. Octal code is 4.
"w" stands for write permission. This is needed in order for the user to modify or update a file. For directories, this allows the user to create and delete files in the directory. Octal code is 2.
The security modes
"x" stands for the execute permission. This is needed in order for a user to run or execute a program. It is not applicable for non-executable files such as ASCII text files. For directories, this allows the user to access the files in the directory. Without the execute permission, the write permission for a directory is useless. Octal code is 1.
The security modes
chmod command
The chmod command allows the user to define or change the security access mode of files.
Syntax:
chmod XXX filename(s)
where:chmod command name
xxx stands for a three (3) - digit numerical code for the security access mode. The first
digit is for the owner, the second if for the group, and the third is for others.
filename(s) is the name of the file/directory whose security access mode is being changed
chmod command
Examples:
owner - read, write, execute (4 + 2 + 1 = 7)
group - read, write (4 + 2 = 6)
others - read (4)
then the security access code is 764
$ chmod 764 eric.let
Owner - read, write (4 + 2 = 6)
group - read (4)
others- no access (0)
then the security access code is 640
$ chmod 640 eric.let
Examples:
owner - read, write, execute (4 + 2 + 1 = 7)
group - read, write,execute (4 + 2 + 1 = 7)
others - read, write, execute (4 + 2 + 1 = 7)
then the security access code is 777
$ chmod 777 eric.let
Examples:
$ ls -l arithmetic--------- 1 mitch staff 4222 Jan 13 11:10 arithmeticTo give the read and execute access for the owner, execute for the group, and no access for others:
$ chmod 510 arithmetic$ ls -l arithmetic
-r-x--x--- 1 mitch staff 4222 Jan 13 11:10 arithmetic
Examples:
$ ls -l eric.let
-rw-rw---- 1 Linus staff 1020 Feb 9 10:15 eric.let
To give the read and write access for others:
$ chmod 666 eric.let
$ ls -l eric.let-rw-rw-rw- 1 Linus staff 1020 Feb 9 10:15 eric.let
Examples:
pwd (print working directory) command
Everytime a user logs-in into an UNIX system, he is automatically taken to his home directory (/usr/Linus). However, a user can move to any directory in the file system as long as he has access to it. In order for the user to determine which directory he is currently in, the command to use is pwd.
Example:$ pwd/usr/Linus
FILE / DIRECTORY COMMANDS
cd (change directory) command
If a user wants to move from one directory to another, he should use the cd command.
Format:
cd directory_name
Note: Typing cd alone will always take the user to his home directory
The mkdir command is for creating new directories or subdirectories within the user’s home directory (or in any directory to which he is authorized to do so) and for creating subdirectories within subdirectories.
Format:mkdir directory_name(s)
mkdir (make directory) command
rmdir (remove directory) command
The rmdir command is for removing or deleting directories. The command can remove a directory or subdirectory only if there are no files in it. However, this command is only possible if the user is authorized to do so.
Format:rmdir directory_name(s)
cp (copy) command
The cp command is for copying files within the same directory or to other directories. However, this command is only possible if the user is authorized to do so.
Format:
cp source_name destination_name
mv (move) command
The move command is for renaming files or moving files from one directory to another. However, this command is only possible if the user is authorized to do so.
Format:
mv source_name destination_name
UNIX TEXT FILES
DEFINITION OF A TEXT FILE A text file consists of almost exclusively of
printable characters (letters, digits, and punctuation). It contains lines of legible data, such as English text. Each line in a text ends with a special nonprintable (invisible) character called a newline, which UNIX uses to determine whether one line ends and the next begins.
A text file differs from an executable file in that the latter mostly consists of nonprintable characters and “strange” characters such as , , , and many others.
THE file COMMAND
The file command determines the type and organization of a particular file. It examines a few hundred bytes at the beginning of the file for hints about the kind of data the file contains.
The format of the file command is:
file filename
File Command reports
If the file command notes that the file consists almost entirely of printable characters, it may identify that file as ascii text.
2. If the file consists of printable characters, many of which happen form words that file finds in the dictionary, it may report the file as English text.
1. If the file command notes that the file consists almost entirely of printable characters, it may identify that file as ascii text.
2. If the file consists of printable characters, many of which happen form words that file finds in the dictionary, it may report the file as English text.
File Command reports
3. If the file consists almost entirely of nonprintable characters, it may report the file as executable.
4. If the file contains what seems to be programming constructs from C or FORTRAN, the file is identified as source code for those languages, reporting c program text or fortran program text.
File Command reports
VIEWING TEXT FILES
There are three different commands in viewing the contents of text files in UNIX:catmorepg
cat
cat commandFormat:
cat filename(s)The cat command is for displaying the contents of a text file on-screen. It is very much similar to the type command of DOS. However, cat will continuously display the contents of the file without regard to whether the user is finish reading the contents or not. In order for the user to catch up, he can freeze the screen by pressing <Ctrl-S>. To unfreeze the screen, simply type <Ctrl-Q>.
pg command
pg commandFormat:
pg filenameThe pg command is more appropriate for viewing large files since it displays the contents of text files one screen at a time. To view the next screen, the user simply presses the Enter key and it displays the next screen of data. pg repeats this process until it reaches the end of the file.
more command
more commandFormat:
more filenameThe more command is very much similar to the pg command in the sense that it also displays the contents of a text file one screenful at a time. However, in order to view the next screen, the user presses the spacebar key instead of Enter key . Furthermore, more updates the user on the percentage of the file that was viewed already. The user may also use the ENTER key if he wants to scroll-up the screen one line at a time.