160 likes | 315 Views
CO004 Projects on Operating Systems. UNIX - Basics 02. Outline. Directory Display a file Input/Output Redirection Pipe. 1. Directory. Absolute directory /home/s 1 0 X 157/program Relative directory: the directory relative to your current working directory
E N D
CO004 Projects on Operating Systems UNIX - Basics 02
Outline • Directory • Display a file • Input/Output Redirection • Pipe
1. Directory • Absolute directory • /home/s10X157/program • Relative directory: the directory relative to your current working directory • E.g., your current working directory: /home/s10X157/ • Then, you can use “cd ../program”, which is a relative directory.
Several directories: • . current directory • .. parent directory • ~ home directory of the current user. • E.g., [s10X0157@localhost] cd ~/program • Will change current working directory to / home/s10X0157/program
2. DisplayFiles • cat (concatenate) • tac (display a file in reversing way) • nl (display a file with line no.) • more (display a file in pages) • less (display a file in pages) • head • tail
cat • Display a file • -E :end-of-line is displayed as $; • -n :display with line no. ; • -T :[tab] is shown as ^I ;
tac • Display a file in reversing way • Display the last line first, then the second last line, so on…
nl • display a file with line no. • The same as cat - n file
more display a file with pages Press ‘space’ to the next page Only forward No backward less display a file with pages Similar to “more” Allow backward more and less
head [-n number] file List the first n lines of file tail [-n number] file List the last n lines of file head and tail
od • od -t • Display a file in non-text file • -t :with various (TYPE), e.g. c :ASCII d[size] :decimal display; o[size] :octal display; x[size] :hexadecimal display ;
3. Redirecting the Output • We use the > symbol to redirect the output of a command. % cat > list1 pearbananaapple^D {this means press [Ctrl] and [d] to stop} % cat list1 • Appending to a file >> % cat >> list1 peach grape orange ^D (Control D to stop)
3. Redirecting the Input • Using < you can redirect the input to come from a file % sort < list1 • To output the sorted list to a file, type, % sort < lists > slist Note that lists and slist should be different.
4. Pipes • To see who is on the system with you, type % who • One method to get a sorted list of names is to type, % who > names.txt % sort < names.txt • We can use pipe to do this % who | sort • will give the same result as above, but quicker and cleaner. To find out how many users are logged on, type % who | wc -l
Wildcards • The * wildcard • The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name. For example % ls list* This will list all files in the current directory starting with list.... • Try typing % ls *list This will list all files in the current directory ending with ....list • The ? wildcard • The character ? will match exactly one character.So ?ouse will match files like house and mouse, but not grouse. Try typing % ls ?list
Reference • 鳥哥的 Linux 私房菜 http://linux.vbird.org/ • UNIX Quick Reference Sheet http://sunsite.utk.edu/UNIX-help/quickref.html