More Related Content
Similar to 05.linux basic-operations-1
Similar to 05.linux basic-operations-1 (20)
More from Minsuk Lee (20)
05.linux basic-operations-1
- 1. Linux: Basic Operations - 1
Minsuk Lee
Hansung University, Seoul, Korea
minsuk@hansung.ac.kr
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 2. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 3. 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 commands
2. 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/passwd
6. Games
7. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 4. 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 !!
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 5. man (3) – „$ man ls‟
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 6. man (4) – „$ man 3 printf‟
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 7. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 8. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 9. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 10. cp, mv
• Copies files and directories
• Usage: $ cp [options] source… destination
– If destination is existing directory, source is copied into the destination directory
– 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 11. 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 UNDELETE
• 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‟
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 12. 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), …
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 13. 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 vi (text editor)
‘=‘ : print current line number commands
‘!command ’ : run shell command
‘^L’ (Ctrl-L) : refresh screen
‘:n’ : next file
‘:p’ : previous file
‘:f’ : show current file name and line number
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 14. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 15. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 16. Let‟s Practice
• Open a terminal /etc is a directory of
• Move to /etc and see what‟s in it, and see /etc/passwd System Configuration
– $ cd /etc
– $ ls or $ ls –l or $ ls –l | more /etc/passwd is
– $ more /etc/passwd or $ cat /etc/passwd
A user list of a System
• 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA