commands

advertisement
Introduction to
Linux OS
AUBG ICoSCIS Team
Prof. Volin Karagiozov
March, 09 – 10, 2013
SWU, Blagoevgrad
Outline
• Some basic mostly used command
ls, pwd, mkdir, cd, man, cp, mv, rm
•
•
•
•
•
•
Text editors (vi, nano)
Input/output redirection, pipes
File system and access modes
Protecting files and directories
Sharing files
The power of the “command line”
Entering commands
Command options and arguments
$
$
$
$
date
ls –a
ls –al
cal
Command name
Option(s)
The who command
The finger command
$ finger
$ finger user_name
The write command
The talk command
The mesg command
$ mesg y
$ mesg n
$ mesg
Allows receiving the messages
Argument
Denies receiving the messages
Specifying a Terminal
Terminal characteristics
$ echo $TERM
$ stty
$ TERM=vt100
$ export TERM
$ echo $TERM
Environment variable
Displays value(s) on the screen
Definition of the variable TERM
Makes the new value known to the system
Listing the content of a Directory
$ ls
$ ls –a
$ ls -al
$ ls -l
total 1272
drwxr-xr-x
drwx------rwxr-xr-x
-rw-r--r--rw-r--r--rw-r--r--rwxr-xr-x
-rw-r--r--
$ ls -al|more
total 1396
drwxr-xr-x
21
drwxr-xr-x 117
-rw------1
-rw------1
-rw-r--r-1
drwxr-xr-x
2
drwx-----5
-rwxr-xr-x
1
-rw-r--r-1
2
5
1
1
1
1
1
1
volin
root
volin
volin
volin
volin
volin
volin
volin
volin
volin
volin
volin
volin
volin
volin
volin
users
root
users
users
users
users
users
users
users
users
users
users
users
users
users
users
users
4096
4096
1504
108
68
57
1472
1561
4096
4096
0
107
0
4096
4096
1504
108
Jan
Jan
Dec
Dec
Sep
Jan
Sep
Oct
Dec
Jan
Sep
Oct
Dec
Dec
Dec
Oct
Nov
21
21
3
3
4
21
3
10
16
21
3
10
16
16
14
10
27
11:17
10:59
18:11
18:11
21:54
11:23
10:31
13:48
22:41
11:23
10:31
13:48
22:41
22:59
22:50
13:48
17:08
COS120
Desktop
a
awk1
awk2
awktest
b
bank.pl
.
..
.ICEauthority
.Xauthority
.addressbook
COS120
Desktop
a
awk1
Displaying the contents of a Text File
$ cat f1 f2
$ more ffff
File name(s)
Displays the content page by page
More commands
$ date
$ cal
Options?
How to learn about the
commands?
$date
Mon Jan 28 11:43:14 EET 2002
$cal
January
Su Mo Tu We
1 2
6 7 8 9
13 14 15 16
20 21 22 23
27 28 29 30
2002
Th Fr
3 4
10 11
17 18
24 25
31
Sa
5
12
19
26
Searching the man-pages with Keywords
• If you do not know the name of the command to carry out a
particular task, you can search the man-pages with keyword.
$ man –k sort
or
$ apropos sort
• The output from these commands might be more than one screen. How
can you manage to get page-by-page output?
• Texinfo database in Linux contains full description of the commands.
$ info <name_of_the_command>
“Play” for a couple of minute with these commands
More commands
There are three vital things to know when you're
working on a Unix system:
1.
2.
3.
Who am I?
Where am I?
What am I?
$ whoami
$ pwd
$hostname
$uname –a
$who am i
(print working directory)
Try this commands now!
Getting Around
• You can move around from directory to directory using the cd
command. Give the name of the directory you want to move to,
or give no name to move back to your home directory. Use the
pwd command to see where you are (or rely on the prompt, if
configured):
$ cd
$ pwd
Each directory contains two special hidden
$ cd /
directories named "." and "..".
$ pwd
•"." refers always to the directory in which
it is located.
$ cd /home
•".." refers always to the parent of the
$ ls –F
directory.
$ cd ~
$ pwd
$ cd ..
$ pwd
Try this commands and tell what “/”, “~” and “..” symbols mean?
Absolute path of directory john
/users/john/work
Relative pathname from work to play
../play
Files in the directory tree
Exercise: Exploring the filesystem
Take a tour of the directory system, hopping one or many steps at a time,
with a mixture of cd and pwd commands.
Go to your home directory.
Enter cd
Find your working directory.
Enter pwd
Change to new working directory.
Enter cd /etc
List files in new working directory.
Enter ls
Change directory to root and list files.
Enter cd /; ls
Change to a new directory.
Enter cd usr
Give a wrong pathname.
Enter cd xqk
Change to a new directory with its absolute pathname.
Enter cd /etc
List files in another directory.
Enter ls /bin
Find your working directory (notice that ls didn't change it) Enter pwd
Return to your home directory.
Enter cd
Environment Variables
• Unix commands and other programs have many options.
Some options are controlled by configuration files, others
by command-line switches, and still others by
environment variables. Each environment variable has a
name and a bit of text (usually one line's worth)
associated with it.
• To Examine one Environment Variable use command
$ echo $var_name
Environment Variables …
• The following environment variables contain important and/or useful
information:
PATH - A colon-delimited list of directories that the shell will search
through when you type a command.
TERM - The current terminal type, which in term controls whether the
screen can do colors, scrolling, etc.
DISPLAY - The machine on which X-windows based applications will
display their windows. Usually of the form: hostname:0.0, where
hostname is the name or IP address of your computer. (Don't worry
about the meaning of the 0.0 -- it's "nothing")
HOME - Path to your home directory.
USER - Your user name.
$ echo $HOME
$ printenv | more
Using echo to display text on the terminal
• The echo utility copies to the terminal anything you
put on the command line after echo.
$ echo Hi
$ echo Hello World!
$ echo date
$ echo ”date”
$ echo ’date’
$ echo `date`
Hi
Hello World!
date
date
date
File manipulation commands (copy)
• Copy file
cp
cp source-file destination-file
$ cd
$ cat
>
file1
This is a source file named file1
Ctrl+D
$ ls
$ cp
file1 file2
$ ls
$ cp
file2 file2.0130.week2
$cd
$cp /home/vkaragiozov/cos231/memo01
.
File manipulation commands (rename)
Changing the name of the file mv
mv existing-file new_file
$ mv
$ ls
file1
file1.week1
More commands …
• grep to Find a String
$
$
$
$
cat memo01
grep
quiz
grep
quizzes
grep –v
quiz
memo01
memo01
memo01
• wc (word count) command – print the numbers of bytes, words and
lines in file
$
$
$
$
wc
wc
wc
wc
memo01
–w memo01
–c memo01
–l memo01
More commands …
• head to look at the top of the file
$ head
memo01
$ head –1 memo01
$ head –3 memo01
• tail to look at the end of the file
$ tail memo01
$ tail –1 memo01
$ tail –4 memo01
More commands …
• sort to display a File in Order
$ sort mem01
• uniq to remove adjacent duplicated lines in a file
$ cat > test
Line3
Line1
Line3
Line2
Line1
Ctrl+D
$ cat test
$ uniq test
What about to use first sort command
and after it uniq?
Recap …
• Searching the man-pages with Keywords and TexInfo database
(apropos, info)
• More commands (whoami, pwd, hostname, uname)
• Getting around from directory to directory (cd, “..”, “.”, “/”, “~”)
• Absolute and relative path of directory
• Environment variables
• Using echo to display text on the terminal (“…”, ‘…’, `…`)
• File manipulation commands (copy, mv, head, tail)
• More commands (grep, wc, sort, uniq)
Using echo to display text on the terminal
• The echo utility copies to the terminal anything
you put on the command line after echo.
$ DATE=tomorrow; export DATE
$ echo $DATE
$ echo DATE
$ echo ”$DATE”
$ echo ‘$DATE’
$ echo ‘$DATE$’
$ echo \$DATE
$ echo “$DATE$”
tommorow
DATE
tommorow
$DATE
$DATE$
$DATE
???
Special Characters & Quoting Characters
• Special characters are those which have a special meaning
to the shell:
&;|*?‘“`[]()$<>{}^#/\!
• Quoting is used to remove the special meaning of certain
characters or words to the shell. Quoting can be used to
disable special treatment for special characters, to prevent
reserved words from being recognized as such, and to
prevent parameter expansion.
Quoting mechanisms
There are three quoting mechanisms:
the escape character, single quotes, and double quotes
1.
2.
3.
A non-quoted backslash (\) is the escape character. It preserves
the literal value of the next character that follows, with the
exception of <newline>.
Enclosing characters in single quotes preserves the literal value of
each character within the quotes. A single quote may not occur
between single quotes, even when preceded by a backslash.
Enclosing characters in double quotes preserves the literal value
of all characters within the quotes, with the exception of $, `, and \.
The characters $ and ` retain their special meaning within double
quotes. The backslash retains its special meaning only when
followed by one of the following characters: $, `, ", \, or
<newline>. A double quote may be quoted within double quotes by
preceding it with a backslash.
Using whereis and which to Find Utilities
• Purpose – to know the full pathname associated with the
command. Why?
• The shell searches for a command in so called search path
• How can you display the search path? Do it now!
• /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/java/jdk1.3/bin:/usr/
X11R6/bin:/usr/local/mysql/bin:/usr/local/netscape:/soft/u01/app/oracl
e/product/8.1.7/bin:/home/volin/bin
$whereis grep
$which grep


grep: /bin/grep /usr/man/man1/grep.1.gz
/bin/grep
Using diff to Compare Two Files
• Copy two files named colors.1 and colors.2
from
~vkaragiozov@aubgin.local/cos231
to your home directories.
• Compare the files:
$ diff colors.1 colors.2
4d3
< yellow
Compressing the files
• file utility gives the information about the content
of the file
$ file *
• compress (uncompress) utilities
$ compress –v memo01
$ file *
$ zcat memo01
$ uncompress memo01
$ file *
File and Directory Wildcards
• When you have a number of files named in series (for
example, chap1 to chap12) or filenames with common
characters (like aegis, aeon, and aerie), you can use
wildcards (also called metacharacters) to specify many
files at once. These special characters are:
– * (asterisk) - An asterisk is replaced by any number of
characters in a filename. For example, ae* would match aegis,
aerie, aeon, etc. if those files were in the same directory. You can
use this to save typing for a single filename (for example, al* for
alphabet.txt) or to name many files at once (as in ae*).
File and Directory Wildcards …
• ? (question mark) - A question mark is replaced by
any single character (so h?p matches hop and hip, but not
help).
• [ ] (square brackets) - Square brackets can surround
a choice of characters you'd like to match. Any one of the
characters between the brackets will be matched. For
example, [Cc]hapter would match either Chapter or
chapter, but [ch]apter would match either capter or hapter.
Use a hyphen (-) to separate a range of consecutive
characters. For example, chap[1-3] would match chap1,
chap2, or chap3.
Using Wildcards - practice
Let’s create quickly the files we need:
$ touch chap10 chap2 chap5 cold chap1a.old
chap3.old chap6 haha chap1b chap4 chap7 oldjunk
And try the following commands:
$ ls chap?
$ ls chap??
$ ls chap[5-8]
$ ls chap*
$ ls *old
$ ls *a*a*
Creating Directories
• To create a new directory, use the mkdir
command. The format is:
$ mkdir dirname(s)
dirname is the name of the new directory. To make several
directories, put a space between each directory name. To
continue our example, you would enter:
$ mkdir spy
boston.dine
Removing Files and Directories
The rm command removes files. The syntax is simple:
$ rm filename(s)
rm removes the named files, as the following examples show:
$ ls
$ rm *old chap10
$ ls
$ rm ch*
$ ls
Caution: rm *
rm –rf *
DO NOT TRY THIS!
Removing Directories
• Just as you can create new directories, you can also
remove them with the rmdir command. As a precaution,
the rmdir command will not let you delete directories that
contain any files or subdirectories: the directory must first
be empty. (The rm -r command removes a directory and
everything in it. It can be dangerous for beginners,
though.)
• The syntax is rmdir dirname(s)
If a directory you try to remove does contain files, you will
get a message like "rmdir: dirname not empty".
• To delete a directory that contains some files:
1. Enter cd dirname to get into the directory you want to delete.
2. Enter rm * to remove all files in that directory.
3. Enter cd .. to go to the parent directory.
4. Enter rmdir dirname to remove the unwanted directory.
Exercise: Manipulating files
In this exercise, you'll create, rename and delete files
Go to home directory.
Copy distant file to working directory.
Create new directory.
List working directory.
Move file to new directory.
Change working directory.
Copy file to working directory.
List filenames with wildcard.
Remove files.
Go up to parent directory.
Remove directory.
Verify that directory was removed.
Enter cd
Enter cp /etc/passwd myfile
Enter mkdir temp
Enter ls -F
Enter mv myfile temp
Enter cd temp
Enter cp myfile myfile.two
Enter ls -l myfile*
Enter rm myfile*
Enter cd ..
Enter rmdir temp
Enter ls -F
Standard Input, Standard Output and
Standard Error
• In general, if no filename is specified in a command, the
shell takes whatever you type on your keyboard as input to
the command (after you press the first [RETURN] to start
the command running, that is). Your terminal keyboard is
the command's standard input.
• As a command runs, the results are usually displayed on
your terminal screen. The terminal screen is the
command's standard output.
• The error messages are usually displayed on your terminal
screen, also. We call it standard error stream.
Standard Input, Standard Output and
Standard Error
• Standard input is associated with so called
file descriptor 0.
• Standard output is associated with so called
file descriptor 1.
• Standard error is associated with so called
file descriptor 2.
• So, by default, each command takes its input from
the standard input, sends the results to the standard
output and sends the error messages (if any) to the
standard error
Input/Output Redirection
• Two default cases of input/output can be varied.
This is called input/output redirection.
• In the following descriptions, if the file descriptor
number is omitted, and the first character of the
redirection operator is <, the redirection refers
to the standard input (file descriptor 0). If the first
character of the redirection operator is >, the
redirection refers to the standard output (file
descriptor 1).
Input/Output Redirection
• You can use a given file as input to a command
that doesn't normally accept filenames by using
the "<" (less-than symbol) operator.
• The > operator - When you add "> filename" to
the end of a command line, the results of the
command are diverted from the standard output to
the named file. The > symbol is called the output
redirection operator.
The >> operator
• You can add more text to the end of an existing file,
instead of replacing its contents, by using the >> (append
redirection) operator. Use it like the > (output redirection)
operator. So,
cat file2 >> file1 appends the
contents of file2 to the end of file1.
• Exercise:
$ date > today
$ who > users
$ cat today > today_users
$ cat users >> today_users
$ cat today_users
Redirecting
Standard Output and Standard Error
• Bash allows both the standard output (file descriptor 1)
and the standard error output (file descriptor 2) to be
redirected to the file
• There are two formats for redirecting standard output and
standard error:
&>file_name
and
>&file_name
Of the two forms, the first is preferred. This is semantically
equivalent to
>file_name
2>&1
Redirection - examples
$
$
$
$
$
$
$
cd; cat /etc/passwd > passwd
cat passwd
who > users
date > today
Error message
ls
cat users
cat today
$
$
$
$
$
more memo01 unexist_file
more memo01 unexist_file 2>test
cat test
more memo01 unexist_file &>test
cat test
::::::::::::::
memo01
::::::::::::::
Due on Tuesday, 4th you should submit
by e-mail to your Instructor
the first homework which will be published on
our course Web-page.
My e-mail address is: vkaragiozov@aubg.bg
Use the Subject: COS231a/or b - HW 01
Greetings,
Volin Karagiozov
Mon Jan 28 12:05:08 EET 2003
unexist_file: No such file or directory
unexist_file: No such file or directory
Pipes
• In addition to redirecting input/output to a named file, you
can connect two commands together so that the output
from one program becomes the input of the next program.
Two or more commands connected in this way form a
pipe.
• To make a pipe, put a vertical bar (|) on the command line
between two commands. When a pipe is set up between
two commands, the standard output of the command to the
left of the pipe symbol becomes the standard input of the
command to the right of the pipe symbol.
• Any two programs can form a pipe as long as the first
program writes to standard output and the second program
reads from standard input.
• Each command in a pipeline is executed as a separate
process (i.e., in a subshell).
Named Pipes
• The other sort of pipe is a “named” pipe, which is sometimes called a
FIFO. FIFO stands for “First In, First Out” and refers to the property
that the order of bytes going in is the same coming out. The “name” of
a named pipe is actually a file name within the file system. Pipes are
shown by ls as any other file with a couple of differences:
$ mkfifo fifo1
$ ls -l fifo1
prw-r--r-- 1 AUBGIN\vkaragiozov AUBGIN\domain users 0 Feb 4 09:42 fifo1
• One very useful application of named pipes is to allow totally unrelated
programs to communicate with each other. For example, a program
that services requests of some sort (print files, access a database) could
open the pipe for reading. Then, another process could make a request
by opening the pipe and writing a command.
Example of using named pipes
• Two virtual consoles. fifo1 is a named pipe
• On the first console execute
$ ls -l > fifo1
• On the second console:
$ cat < fifo1
Filters
• The grep program searches a file or files for lines that have
a certain pattern. The syntax is:
grep pattern file(s)
• The name "grep" derives from the ed (a UNIX line editor)
command g/re/p which means "globally search for a
regular expression and print all lines containing it."
• A regular expression is either some plain text (a word, for
example) and/or special characters used for pattern
matching. When you learn more about regular expressions,
you can use them to specify complex patterns of text.
Exercise: Redirecting input/output, pipes
and filters
• In the following exercises you'll redirect output,
create a simple pipe, and use filters to modify
output.
Redirect output to a file.
Sort output of a command.
Append sorted output to a file.
Display output to screen.
Display long output to screen.
Enter who > users
Enter who | sort
Enter who | sort >> users
Enter more users or less users
Enter ls -l /bin | more or ls -l /bin | less
Output from ls -al
Protecting and Sharing Files
• Directory Access Permissions
A directory's access permissions help to control access to the files
in it. These affect the overall ability to use files and subdirectories
in the directory. (Once you have access to a directory, the ability to
read or modify the contents of specific files is controlled by the file
access permissions
• File Access Permissions
The access permissions on a file control what can be done to the
file's contents. The access permissions on the directory where the
file is kept control whether the file can be renamed or removed.
Filesystem Permission Bits
chmod command
Directory Access Permissions:
• To keep yourself from accidentally removing files (or adding or renaming files)
in a directory, use chmod 555 dirname. To do the same, but also deny other
users any access, use chmod 500 dirname.
• To protect the files in a directory and all its subdirectories from everyone else
on your system - but still be able to do anything you want to dp there - use
chmod 700 dirname.
• To let other people on the system see what's in a directory - and read or edit the
files if the file permissions let them - but not rename, remove, or add files - use
chmod 755 dirname.
• To let people in your UNIX group add, delete, and rename files in a directory of
yours - and read or edit other people's files if the file permissions let them - use
chmod 775 dirname.
• To give full access to everyone on the system, use chmod 777 dirname.
chmod command
File Access Permissions
• To make a private file that only you can edit, use chmod 600 filename.
To protect it from accidental editing, use chmod 400 filename.
• To edit a file yourself, and let everyone else on the system read it
without editing, use chmod 644 filename.
• To let you and all members of your UNIX group edit a file, but keep
any other user from reading or editing it, use chmod 660 filename.
• To let non-group users read but not edit the file, use chmod 664
filename.
• To let anyone read or edit the file, use chmod 666 filename.
Download