05.linux basic-operations-1

16
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA Linux: Basic Operations - 1 Minsuk Lee Hansung University, Seoul, Korea [email protected]

description

 

Transcript of 05.linux basic-operations-1

Page 1: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

Linux: Basic Operations - 1

Minsuk LeeHansung University, Seoul, Korea

[email protected]

Page 2: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

Simple Shell usage• Invoking Terminal

• Type any command and [ENTER]• BIG TIPs !!

– Use ‘←,↑→↓’, [INS], [DEL],[HOME],[END] keys to edit command line– Try [tab] - It completes filename or shows all available choices– When output stops, try [SPACE], mouse scroll, ‘q’ to continue or to

quit– $ exit -- will end the terminal

Page 3: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

man (1)• Shows manual page of Linux commands, libraries, utilities• Usage : $ man [options] [section] item

– Item can be program, function, file, anything in /usr/share/man/*/

• Options :– Sections

1. Executable programs or shell commands2. System calls (functions provided by the kernel)3. Library calls (functions within program libraries)4. Special files (usually found in /dev)5. File formats and conventions e.g. /etc/passwd6. Games7. Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)8. System administration commands (usually only for root)9. Kernel routines [Non standard]

‘$ man printf ‘ show man page of shell command printf‘$ man 3 printf ‘ show man page of printf() library‘$ man -a printf ‘ show man page of both of the two

Page 4: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

man (2)• Options :

-a : display all available manual pages of the item-k : list all man page items includes the given item name ($ apropos –r item)-f : shows very short descriptions-Tps : output as Postscript format (to be converted into pdf)

• Example$ man man$ man -Tps 3 printf | ps2pdf - printf.pdf

• Alternatives$ info item : shows detail information, if the item is available in /usr/share/info$ command –help : shows help messages (options) of the command

• PLEASE READ MAN PAGES OF COMMANDS, FUNCTIONS YOU USE !!

Page 5: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

man (3) – ‘$ man ls’

Page 6: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

man (4) – ‘$ man 3 printf’

Page 7: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

ls• Lists directory contents• Usage : $ ls [options] file-or-directory…

– If file-or-directory is missing, current directory is assumes

• Options :-a : list all, including hidden files and directories (starting with .)-l : show details (permission, owner, group, size, dates)-R : list files recursively traversing child directories-1 : lists one item in one line

Page 8: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

cd (1)• Changes the shell working (current) directory• Usage : $cd directory

– Directory can be• directory-name, /adir/bdir/cdir, ..

– Linux DO NOT USE ‘\’ (‘\’), BUT USE ‘/’ in directory hierarchy– ‘.’ means current directory, ‘..’ means parent directory– If no directory is specified, it assumes home directory– If directory name starts with ‘/’,

it’s absolute directory from the root directory– If directory does not start with ‘/’,

it’s relative to current directory

• Examples‘$ cd’ moves current directory to my home (same as ‘$ cd ~’‘$ cd ..’ move current directory to parent directory‘$ cd /usr/share/man’ move current directory to /usr/share/man‘$ cd ~user’ moves current directory to user’s home directory

Page 9: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

cd (2)• Some more information on directories

‘$ pwd’ shows current directory‘$ mkdir directory’ makes a new directory‘$ rmdir directory’ removes the directory‘$ pushd directory’ saves current directory,

and moves to the specified directory‘$ dirs’ shows the pushed directory

‘$ popd’ returns to the saved directory

Page 10: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

cp, mv• Copies files and directories• Usage: $ cp [options] source… destination

– If destination is existing directory, source is copied into the destination direc-tory

– If destination file is existing, it’s overwritten• Options:

-a : copy as is (preserving all the attributes of the source)-b : make a backup of each existing destination file-f : if destination file exists and cannot be opened, remove it and retry-i : prompt before overwrite-l : link (hard) files instead of copying-n : do not overwrite (ignoring –i)-r : copy directories recursively-s : make symbolic links instead of copying-u : copy only when source is newer than destination

• Usage: $ mv [options] source… destination– Rename source to destination or move source to destination directory

Page 11: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

rm• removes files or directories• Usage: $ rm file…

– Remove files

• Options-f : never prompt-i : prompt before every removal-r : remove directories and its contents recursively -v : verbose mode, (explain what is being done)

• IMPORTANT NOTICE !!!– THERE IS NO “RECYCLING BIN or TRASH CAN” in Linux for UN-

DELETE• Desktop file browser usually support UNDELETE function, but not for ‘rm’

• How to delete a file with name starts with ‘-’ ?– ‘$ rm -- -foo’ or ‘$ rm ./-foo’

Page 12: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

Name pattern matching• Specifying multiple files(directories)

– Work for ALL commands• ‘*’ means any string (multiple, any characters including null)• ‘?’ means any single character• [a-s] : means any single character between ‘a ’ and ‘s ’

– E.g., [1-7c-f], [acf2A-Z], …

– ‘$ rm * ’ : means everything in current directory– ‘$ rm directory/* ‘ : means everything in directory– ‘$ rm s*s ‘ : means files or directories start and end with ‘s ’– ‘$ rm 6[ab]x* ‘ : means files or directories start with ‘6ax ’

or ‘6bx ’

• See man page of bash ( $ man bash ) for more– Such as ?(patten-list), *(patten-list), +(pattern-list), …

Page 13: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

more• Displays files on screen page by page

• Usages : $ more [options] file…• Options:

-num : specifies screen size in lines(e.g., ‘$ more -7 file’)

• Commands after screen stops:‘h’ : help screen[SPACE] or ‘z’ : next num lines[ENTER] or ‘1’ : next line‘q’ : exit‘f ‘ : next screen‘b’ : 또는 - ^B : 이전 페이지 (back)‘/pattern ‘ : find and go to the position of pattern‘n’ : find next occurrence of the pattern‘=‘ : print current line number‘!command ’ : run shell command‘^L’ (Ctrl-L) : refresh screen‘:n’ : next file‘:p’ : previous file‘:f’ : show current file name and line number

vi (text editor)commands

Page 14: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

cat• Concatenates files and print on display• Usage : $ cat [options] file…

– Concatenate files, and print on display (standard output)– Used to print, create simple file

• Options :• -E : display ‘$’ at the end of each line• -b : number non-empty lines• -n : number all lines• -T : show TAB as ^I• -v : show non-printing control characters except LF and TAB

Page 15: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

Pipe and redirection• Standard Input and Output

– In Shell, It’s Keyboard input and Screen Output, by default– Pipe feeds standard output into other command’s standard input– Redirection redirects the standard input/output from/to files

‘$ ls –l | more’ : feed the output of ‘ls –l’ to ‘more’‘$ ls –1 | sort –r | more’ : ‘ls -1’ then, ‘sort’ in reverse order, and ‘more’ ‘$ ls –l > file ’ : redirect the output of ‘ls –l’ to file (overwrite or create)‘$ ls –l >> file ’ : redirect the output of ‘ls –l’ to file (append)‘$ cat a b > c ’ : concatenate file ‘a ‘ and ‘b ‘ into file ‘c ‘‘$ sort < source > destination ’ : ‘sort’ source, into destination

‘# cat /dev/cdrom > foo.iso’

• Standard Error‘$ command 2> file’ : redirect error message of command to file

Page 16: 05.linux basic-operations-1

CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

Let’s Practice• Open a terminal• Move to /etc and see what’s in it, and see /etc/passwd

– $ cd /etc– $ ls or $ ls –l or $ ls –l | more– $ more /etc/passwd or $ cat /etc/passwd

• Copy /etc/passwd into my home directory as sample– $ cp /etc/passwd ~/sample

• Return to my home directory and Triple it into big-sample, and see– $ cd ~ or just $ cd // move back to my home– $ cat sample sample sample > big-sample // cat sample three times and redirect into big-sample– $ ls –l // see the file size– $ more big-sample // try all commands of more– $ sort big-sample > sorted-sample // see what happens in sorted-sample

• Make a new directory ‘data’ and copy sample and move results the files into it– $ mkdir data // – $ cp sample data– $ mv *-sample data– $ ls –l data– $ ls –l

• Remove Everything– $ rm –rfi sample data // see what happening, answer with ‘n’ !!– $ rm –rf sample data– $ ls -l

/etc is a directory ofSystem Configuration

/etc/passwd isA user list of a System