Chapter 3 Utilities Dr. Marjan Trutschl marjan.trutschl@lsus.edu Louisiana State University, Shreveport, LA 71115
Chapter 3: The Utilities ¤ Special Character – Syntax Revisited ¤ Basic Utilities ¤ Working with Files ¤ Pipelines ‘|’ Communicating Between Processes ¤ File Compression Utilities ¤ Displaying User and System Information
Special Characters ¤ Special Characters: ¤ & ; | * / ’ ” ` [ ] ( ) $ < > { } # / \ ! ~ ¤ All have special meanings, escape them with ‘\’ or quotes -Double quotes don’t escape -Single quotes escape everything -’\’ escapes only one thing -The ‘$’ character changes it into a variable, which had a default value of zero
Basic Utilities ¤ There Are Numerous Utilities ¤ You can do pretty much anything that you can think of ¤ If you don’t see a utility, then install it with a package manager like ‘apt-get’ for Linux or for Mac, try Homebrew, Nix or MacPorts ¤ Check the man page when you download them to figure them all out NEdit is a graphical text editor with syntax highlighting for Linux. It wasn’t installed, so I downloaded it and installed it with a single command.
Basic Utilities ¤ Try These Handy Utilities: ¤ ‘ls’: Lists the files in the current directory, try adding ‘-ah’ ¤ ‘rm’: Deletes a file, adding ‘-i’ ask first, ‘-r’ delete a directory ¤ ‘cat’: Displays a file
Working with Files ¤ What to Do Without Windows? ¤ ‘scp’: Secure Copy ‘scp source-file destination-file’ ¤ If it’s a remote location, don’t forget the ‘:’ ¤ ‘cp’ is the less secure version of this ¤ ‘touch’: Create an empty file ¤ ‘mv’: Change the name or location of a file Careful! This can clobber ¤ ‘lpr’: Print the file, like on archaic paper your files!
Working with Files ¤ ‘grep’: Search filenames and contents ¤ You’ll use grep like a verb, it’s very versatile ¤ ‘head’ and ‘tail’: Display the first or last ten lines of a file ¤ ‘sort’: Sorts a list alphabetically or numerically and removes duplicates ¤ ‘uniq’: Removes non-unique entries from a sorted list ¤ ‘diff’: Identifies the differences between two files ¤ ‘file’: Summarizes file contents, without opening them ¤ ‘echo’: Displays text, or allows it to be appended/redirected ¤ ‘date’: Displays the current date ¤ ‘script’: Records your session
Working with Files: Try This ¤ Start with a list of things (maybe citrus fruits), in no particular order, that contains non-adjacent duplicates ¤ touch citrus.txt ¤ Creates an empty file ¤ echo **paste your list here** > citrus.txt ¤ Adds the text to the file ¤ sort -u citrus.txt > sorted.txt ¤ Sorts and removes the duplicates ¤ diff -u sorted.txt citrus.txt ¤ Displays the changes between the two files
Communicating Between Processes ¤ Using the Pipeline ¤ It’s a pun, just in case you didn’t get that, we’re talking about the straight line character above the backslash. ¤ It removes a lot of intermediary steps by allowing one utility to push data into another, without saving it to a file ¤ Like this: This kind of thing makes the last exercise a lot simpler by removing a step in the middle.
File Compression Utilities ¤ File Compression: More Than Just Zipping Up ¤ Each compression utility has different features and is sometimes not compatible with others ¤ Archiving and compression are completely different features ¤ Archiving puts numerous files together into a package ¤ Compression reduces storage size by efficiently coding it ¤ bzip2, zip and gzip are common compression utilities ¤ bzcat, bunzip2, unzip and gunzip for decompression ¤ tar makes a good archive that preserves file permissions ¤ -c: Create -v: Verbose -f: to File -t: Table -x: eXtract
File Compression Utilities ¤ Trial Run ¤ This illustrates archiving and compressing the citrus.txt file, and then putting it right back where it came from:
Displaying User and System Information ¤ More Useful Utilities ¤ which: The utility’s file PATH (important for shell scripting) ¤ whereis: Every file location of the utility ¤ who: Users on the system ¤ uptime: System uptime ¤ w: Uptime, who and more data ¤ free: Available RAM ¤ write: Send a quick message ¤ mesg: Enable/disable messages sent with ‘write’ utility ¤ w3m: Text-based web browser
¤ Some Simple Demonstrations:
Summary ¤ Special Character – Syntax Revisited ¤ Basic Utilities ¤ Working with Files ¤ Pipelines ‘|’ Communicating Between Processes ¤ File Compression Utilities ¤ Displaying User and System Information
Exercise ¤ Try this out: ¤ Use w3m to download a web page ¤ Send its text only output to a file without a text editor ¤ scp that file to another folder ¤ tar it ¤ gzip it ¤ write a message to a friend to tell them why you love bash
Work Cited ¤ Sobell, Mark G. ”The Utilities." A Practical Guide to Linux Commands, Editors, and Shell Programming . Upper Saddle River, NJ: Prentice Hall/Pearson, 2013. N. pag. Print. ¤ Wikipedia . Wikimedia Foundation, n.d. Web. 21 Dec. 2015.
Recommend
More recommend