Fundamental Unix Commands

Overview of the Apple OS X BSD UNIX implementation

Opening a terminal

To access the UNIX subsystems in OS X you need to open the terminal application. The terminal is located in the Utilities folder under applications.

Navigating in the terminal
  • The horizontal arrow ←→ keys move the cursor left and right
  • The vertical arrow ↑↓ keys page through the command history.
  • Use the mouse to select text (by highlighting) and ⌘c and ⌘v to cut and paste text
  • Delete deletes text behind the cursor the ⌦ key deletes text after the cursor.

There are numerous concise ways to specify directory information.
  • . indicates your current directory
  • .. indicates the directory right above you on the tree, or parent to the current directory.
  • ~ indicates the users own home directory
  • ~smith indicates the user Smith's own home directory
  • / the root directory, or top of the tree.

Basic Unix Commands

CP: cp copies a file from a source name to a target name.

cp [modifiers] /<filename> /<filename>

Some common modifiers for copy are:
-r for recursive (used to copy whole directories)
-f force (don’t ask me if I want to do it just do it)
-P preserve permissions

For example
cp /usr/share/tcsh/examples/login ~/login

copies the default login file for the tcsh shell located in the /usr/share/tcsh/examples to your home directory. See the article on shells for more information on the login file.

MV: mv moves a file from one directory to another, or from one name to another, or a combination of both. It often confuses new users that renaming in Unix is the same as moving.

mv [modifiers] /<filename> /<file>

Some common modifiers for move are:
-r for recursive (used to copy whole directories)
-f force (don’t ask me if I want to do it just do it)
-P preserve permissions

For example
mv login .login

just renames the file login to .login.

RM: rm removes a file or with the -r modifier, a directory.

rm [modifiers] /<filename/directory>

Some common modifiers for move are:
-r for recursive (used to delete whole directories)
-f suppresses confirmation prompts asking if you really want to delete read-only files.

For example
rm -rf /usr/local/bin

removes the directory bin, in /usr/local, if you have permission to do so. Note this is NOT something that is a good idea to try.

CD: cd changes the current working directory.

PWD: pwd displays the current working directory.

MKDIR: mkdir creates a new directory
. Some common modifiers for mkdir are:
-p used to create a whole hierarchy of folders in one step.

For example
mkdir -p work/version1/code

creates the directory code inside of the directory version1, which is inside a directory work. And work is created in the current working directory.

LS: ls displays the contents of a specified directory. By default it displays the current directory.
ls [modifiers]
ls [modifiers] /<filename/directory>

Some common modifiers for ls are:
-l provides a long listing of the file information.
-lt provides a long listing but now sorted in chronological order
-a lists all files, including hidden dot files.
-d lists the directory itself (the default behavior lists the contents of the directory instead).

For example
ls -t /usr/share/tcsh/examples

lists the contents of the directory /usr/share/tcsh/examples in the chronological order in which they were created.

Last of all, is the command that helps you find out more about any command. This is the man command.

MAN: man accesses the built in manual pages. For example to find all the modifier options for ls, type:
man ls

to find the name of a command which does something specific use the -k modifier.

man -k password

lists all the commands which deal with passwords. Alternatively use the command apropos which searches through the header lines of the man pages for whatever keyword you supply, and lists the man pages containing it. For example,

apropos copy

produces a list of all the man pages that contain copy in their header lines.

This is just the begining, there is more Unix to come, but this will get you started.

No comments:

Post a Comment