Tải bản đầy đủ

linux crash course chapter 03 3

Chapter 3:
Command Line Utilities
Doin’ stuff

In this chapter …
• Special characters
• Redirection
• More utilities than you shake a stick at

Typing Commands
• Beware of special characters
• Characters that have special meaning to the
• Shell expands, modifies and interprets
special characters before issuing the

Special Characters

• &;|*?‘“`[]()$<>{}^#/\%!~+
• Plus whitespace (tabs, spaces, newlines)
• Do not use these in filenames unless you
have to
• To use them, either put in single quotes,
or proceed with a backslash
– ls ‘filename with special chars!!’
– ls \[cat\]

Special Characters con’t
• All special characters have special meaning
to the shell
• We’ll explore these in great detail in
upcoming chapters

• Linux & Unix come with thousands of utilities
• Some used explicitly, others implicitly
• Some text-based, some GUI, some both

Some tips before we start
• Tab completion
– When typing a filename or command name, you
can type the first few letters then hit TAB to autocomplete the command

• Pipe (|) symbol
– Used to chain commands together
– The output of one command becomes the input
of another
– We’ll revisit this in detail later

ls: LiSt files
• Used to list files contained in a directory
• Can narrow the search using pattern
• Examples

– ls  displays ‘all’ the files in the directory
– ls cats  displays the file cats in the directory
– ls ca*  displays files starting with ‘ca’

cat: catenate a file
• Displays the contents of one or more files
• Beware – don’t try with binary files
• Examples
– cat myfile  displays contents of myfile
– cat file1 file2  displays contents of file1
followed by contents of file2

rm: ReMoves a file
• Similar to del in DOS
• Use the –i option to invoke interactive mode,
which prompts you if you’re sure
• Examples:
– rm myfile  deletes myfile
– rm –i myfile  prompts you before deleting

more and less: pagers
• more and less are similar in that they both
break up long files into page long chunks
• Press h to display possible commands
• Examples
– less myfile  displays myfile one page at a

hostname: Where am I?
• hostname will display the name of the
system you are currently logged onto
• Usually a fully qualified domain name
• Example:
– hostname  displays:

cp: CoPies files
• Usage: cp sourcefile destinationfile
• Creates a copy, leaves sourcefile intact
• If destinationfile exists, it will be overwritten
– Unless you use –i option

• Example:
– cp myfile myfile.backup

mv: MoVe files / change name
• Usage: mv existingfile newfile
• Just like cp, can overwrite with –i option
• Renames a file, which can also move it to
another directory
• Examples:
– mv myfile foshizzle
– mv /dir1/myfile /dir2/myfile

lpr: Line PRinter

Places files into the print queue
Usage: lpr [-Pprintername] files
You can check the status of queue with lpq
You can delete a job with lprm
Sorry, we don’t have a printer

grep: global regular expression print

Used to search for strings in files/output
Usage: grep expression filename
Returns lines with expression in filename
– grep ‘automagically’ myfile

head: display beginning
• Displays beginning of file
• head -X filename displays first X lines
• Check out pg 727/691 for more options

tail: duh
• Displays the end of a file
• tail -X myfile displays the last X lines
• Check out pg 843/783 for more options

sort: displays sorted info
• sort displays data in a sorted manner, without
altering the original file
• Lots of options – sort alphabetically,
numerically, with or without repeats, reverse
order, etc
• Check out pg 817/762

uniq: removes duplicates
• uniq displays data, omitting successive
repeat entries
• Have to sort file first – otherwise it might not
find all duplicates
• Does not alter original file

file: what kind of file is this?
• Usage: file filename
• Tells you what kind of file you’re working with
and what kind of data is in it
• Examples include program, shell builtin,
ASCII text, compressed data, etc

echo: display text
• Displays (echoes) text back to the terminal
• Can print out contents of shell variables
• Useful in shell scripts
• In other words, seems dumb now but we’ll
use it a lot down the road

date: displays time and date
• Command options can change formatting
• Privileged accounts can use date to change
date and time
• Can be useful for scripting

script: captures session
• Captures all input and output on the terminal
and saves to a file
• A good way to document your work, or
capture errors for analysis
• Type script to start capture, exit to quit
• By default stores everything in the file

Text Converters
• unix2dos and dos2unix
• Unix and DOS use different end of line
• Use these utilities when moving text files
back and forth between Windows and Linux
• Weird script error? Try dos2unix

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay