chapter 3: command line utilities doin’ stuff. in this chapter … special characters redirection...
TRANSCRIPT
![Page 1: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/1.jpg)
Chapter 3:Command Line Utilities
Doin’ stuff
![Page 2: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/2.jpg)
In this chapter …• Special characters
• Redirection
• More utilities than you shake a stick at
![Page 3: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/3.jpg)
Typing Commands• Beware of special characters
• Characters that have special meaning to the shell
• Shell expands, modifies and interprets special characters before issuing the command
![Page 4: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/4.jpg)
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\]
![Page 5: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/5.jpg)
Special Characters con’t• All special characters have special meaning
to the shell
• We’ll explore these in great detail in upcoming chapters
![Page 6: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/6.jpg)
Utilities• Linux & Unix come with thousands of utilities
• Some used explicitly, others implicitly
• Some text-based, some GUI, some both
![Page 7: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/7.jpg)
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 auto-complete 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
![Page 8: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/8.jpg)
ls: LiSt files• Used to list files contained in a directory
• Can narrow the search using pattern matching
• 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’
![Page 9: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/9.jpg)
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
![Page 10: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/10.jpg)
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
![Page 11: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/11.jpg)
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
time
![Page 12: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/12.jpg)
hostname: Where am I?• hostname will display the name of the
system you are currently logged onto
• Usually a fully qualified domain name (FQDN)
• Example:– hostname displays:
ares.bcs.solano.cc.ca.us
![Page 13: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/13.jpg)
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
![Page 14: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/14.jpg)
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
![Page 15: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/15.jpg)
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
![Page 16: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/16.jpg)
grep: global regular expression print
• Used to search for strings in files/output
• Usage: grep expression filename
• Returns lines with expression in filename
• Example:– grep ‘automagically’ myfile
![Page 17: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/17.jpg)
head: display beginning• Displays beginning of file
• head -X filename displays first X lines
• Check out pg 727/691 for more options
![Page 18: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/18.jpg)
tail: duh• Displays the end of a file
• tail -X myfile displays the last X lines
• Check out pg 843/783 for more options
![Page 19: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/19.jpg)
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
![Page 20: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/20.jpg)
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
![Page 21: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/21.jpg)
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
![Page 22: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/22.jpg)
echo: display text• Displays (echoes) text back to the terminal
screen
• 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
![Page 23: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/23.jpg)
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
![Page 24: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/24.jpg)
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 typescript
![Page 25: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/25.jpg)
Text Converters• unix2dos and dos2unix
• Unix and DOS use different end of line characters
• Use these utilities when moving text files back and forth between Windows and Linux systems
• Weird script error? Try dos2unix
![Page 26: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/26.jpg)
Compressing files• bzip2 files
• gzip files
• compress files
• Each use their own algorithms and have their uses
![Page 27: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/27.jpg)
Uncompressing files• bunzip2 compressed-file
• gunzip compressed-file
• ucompress compressed-file
![Page 28: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/28.jpg)
tar: Tape ARchive• Packs and unpacks files from archives
• *Does not compress, only assembles*
• Tons of options, allowing you to add or remove files from archive, and also apply compression using third party support
![Page 29: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/29.jpg)
which: locates utilties• Will display the location of a utility
• which ls displays location of ls command you’re using
• In case of there being multiple locations, which only displays the first (i.e., the one you will be using)
![Page 30: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/30.jpg)
whereis: locates utilities• Similar to which, but displays the utilities in a
standard set of locations
• The first one listed may not be the one you will issue when you enter the command
• All depends on your PATH (chapter 4)
![Page 31: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/31.jpg)
Sidenote• which and whereis do not list shell builtins
• Shell builtins are functions that are internal to the shell itself – no binary executable
• To see if you’re using a builtin, use type
![Page 32: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/32.jpg)
apropos: what do I use?• Not sure what utility you’re looking for?
• Try apropos keyword
• Displays utilities and libraries related to your keyword
• Found one, but not sure? whatis utility to show what it does, or check man page
![Page 33: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/33.jpg)
locate: search for files• System maintains a database of files
• Your system administrator should configure a job to regularly update this database
• Searches for any kind of file – not just utilities
• Some systems use slocate (secure)
• Latest distros use mlocate via locate
![Page 34: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/34.jpg)
who: Who’s online?• Displays what users are logged on
• Also displays when they logged on, and with what device (terminal or console, etc)
• Also try who am i
![Page 35: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/35.jpg)
finger: reach out and touch …• finger by itself displays users logged on like
who, but also shows idle time and office location
• finger username shows info about that user, like home directory, last logon, their shell, if they have unread mail, and .plan and .project files
![Page 36: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/36.jpg)
w: What’s up?• w is similar to who by showing who’s logged
on
• Also shows system uptime, and memory and CPU load averages
• Good overall status of the system
![Page 37: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/37.jpg)
write: send a message• write username opens up a text-based chat
with the user
• Type message
• Wait for response
• CTRL-D to exit write
![Page 38: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/38.jpg)
mesg: Enable/disable write• Usage: mesg y|n
• Turns off whether users can write you or not
• Useful if you don’t want to be bugged
![Page 39: Chapter 3: Command Line Utilities Doin’ stuff. In this chapter … Special characters Redirection More utilities than you shake a stick at](https://reader036.vdocument.in/reader036/viewer/2022062500/56649e685503460f94b63a89/html5/thumbnails/39.jpg)
mail: system mail• Our system is a closed system
• You can send mail to other users on the system
• No public mail