Skip to content

UNIX; or, Yes, I'm Afraid It's Terminal

tdkehoe edited this page May 10, 2015 · 1 revision

The Galvanize instructors want us to use the terminal for everything, and never use the Mac GUI or desktop apps. IMHO, one of the great things about computers is there's (at least) two ways to do everything. My plan is to learn two ways to do everything: Git from the command line and the Github desktop app, Cyberduck from the command line and the Cyberduck app, Cyberduck and FTP, etc. What are the advantages of learning two ways to of stuff?

  • If something doesn't work, do it the other way. E.g., you use Darwin (Apple's version of UNIX) and then you use LINUX or Windows UNIX, a non-standard UNIX command. e.g., open, won't work.
  • If you've forgotten how to do it one way, you know a second way to do it. Not that you'd ever forget how to write a UNIX command. :-)
  • If you learn two ways to do something, you'll likely learn something else. E.g., while looking for a menu item in a desktop app, you see another menu item that you were unaware of when using the terminal.
  • Better learning. Some people are more visual learners, some are more auditory learners, and some are more kinesthetic (movement) learners. According to Dawna Marcova's book The Open Mind, we're present (taking in information) in one mode but to convert information to knowledge we have to switch to a second mode, and to commit to long-term memory, or to experience deep insights, the third mode must also be used.
  • Better problem-solving. If I'm stuck on a problem, switching to an alternative approach can make the problem (and its solution) obvious. For example, when a website can't find a resource file, but you see that the file is in the correct directory, switch from the terminal to the GUI. Whoops, my directory website has a subdirectory named website!
  • Faster, easier. Sometimes the terminal is faster and easier, and sometimes the GUI or a desktop app is faster and easier. It depends what you're doing.
  • Safer. For example, I type rm -rf my_directory to remove a directory and its contents. But what if I'm tired or distracted or my finger slips and I type rm -rf? (That command erases your system drive, i.e., turns your computer into a brick.) In the Mac GUI I drag a folder to the Trash, or use File > Move to Trash. If I made a mistake, I can take it out of the Trash. And nothing in the Mac GUI will erase the system drive while it's booted.
Screen Shot 2015-04-25 at 7.20.09 PM

And there's more than one terminal. I plan to learn iTerm2, and see what features it has that the Mac terminal app lacks.


The rest of this post is UNIX commands that I use. Wikipedia has a full list of UNIX commands.

General

The tab key completes a partially typed command.

The up and down arrow keys scroll through previous commands.

The first part of a UNIX command is the program, the second part is the argument.

Viewing File Contents

cat - catenate - merges several files into a single file:

cat file1 file2 file3 > bigfile

I remember in 1983 Ken Lindsay explaining to me that concatenate means to tie ropes end to end to make a longer rope. (Catenate and concatenate mean the same thing.) But that's not what cat is usually used for. If you provide only one filename:

cat file.name

then cat displays the file on your screen. cat can also be used for copying files, and for many other purposes. Perhaps it's called cat because it has nine lives?

more file.name - displays a file a page at a time - Written in 1978 by Daniel Halbert. less file.name - an improved version of more. Programmers complained that more lacks the ability to move back or up to view previous pages of a file. Mark Nudelman wrote less in 1983-85 to do this. This led to various jokes such as "less > more."

echo string - prints the string to the screen and adds a newline character after it echo -n string - prints the string to the screen without a newline character echo doesn't display files, it just displays a string from the command.

Viewing Directory Contents

ls - list - lists the files in a directory ls -a - list all - displays all files including hidden files (hidden files begin with ".") ls -l - list long - displays files with permissions, dates, etc. ls -t - list by time - sorts the files by modification time. ls -h - list human-readable file sizes - shows files sizes in human-readable format.

ls --color displays different file types with different colors in some UNIX environments, but not Darwin. Maybe iTerm2 can do this?

Directory Navigation

pwd - print working directory - displays what directory you're in, and its path.

tree - displays files and subdirectories.

cd subdirectory - change directory - move to a subdirectory cd .. - move to the parent directory cd ~ - move to home directory. cd ~/directory/subdirectory - move to a directory somewhere else in the system

pushd - push directory - remembers your current working directory and moves you to another directory popd - pop directory - returns to the remembered working directory

open subdirectory - opens a subdirectory into a folder window. The Mac app ShellTo opens a terminal window from a folder, i.e., is the opposite of open subdirectory.

Opening Files and Websites

Open does more than open folder windows:

open file.name - open a file in its default app open -a "Application" file.name - open a file using a different app open http://www.website.com - opens a website

Open is Darwin, not standard UNIX.

File Operations

touch file.name - updates the access and/or modification date to the current time. Also creates a new file if the file doesn't exist. Flags can set the dates to match another file.

rm file.name - remove - deletes a file. Your file doesn't go in the Trash. It's gone forever.

cp oldfile.name newfile.name - copy - duplicates the file

mv oldfile.name newfile.name - move - changes a file's name

chmod - change mode - changes permissions. You can also do this with File > Get Info.

chown - change ownership

diff file1 file 2 - difference - displays the differences between two files

zip - compresses files. You can also right-click on the file and choose "Compress ." This doesn't work from the "All My Files" window. unzip - uncompresses files

wget http://www.example.com/ - World Wide Web get - gets files from websites. Not a standard UNIX command, needs to be installed.

Directory Operations

mkdir - make directory - creates a directory. mkdir -p - make directory - creates a directory with parent directories.

rmdir - remove directory - removes an empty directory. rm -rf directory - remove directory - removes a non-empty directory and its contents. It's safer to do this from the Mac GUI.

cp -R my_directory/ new_directory - copies a directory and its contents

Shell Operations

env - environment - prints environmental variables, e.g., the terminal program you're using, the shell you're using, etc. set - set environmental variables.

export - environment - export or set an environmental variable, e.g., the terminal program you're using, the shell you're using, etc.

exit - exit the shell.

Network Operations

hostname - prints the computer's name on the network

Searches

grep - globally search a regular expression and print - a utility for searching files for matching characters, strings, wild cards, etc.

find - finds files. You can also use Spotlight.

Documentation

man command - displays manual page for that command. The manual page opens in the vi editor. To get out of vi, type q.

apropos something - utility for finding a manual page about something. Not a standard UNIX command.

Editors

vi - visual editor. To get out of vi, type : (to exit to the ex editor) then q (quit if no changes were made), or q! (quit without saving changes), or wq (write and quit).

Miscellaneous Commands

xargs - execute arguments - ?

hash - prints every available command, and where it is located.

sudo - super user do this - tells the super user to do a command that the user doesn't have permission to do. Sudo will then prompt for a password.

&& - puts two commands on one line.

And my favorite...

kill - terminates processes kill -9 - This terminates processes really dead. Someday I'll write a detective novel about UNIX programmers, and call it Kill -9.

Clone this wiki locally
You can’t perform that action at this time.