FractalNet
Home Software Music Other
Overview   Overview
Guitar Chord Explorer   GCE
Marble Challenge   Marble Challenge
ASCIIroids   ASCIIroids
Particle System Designer   PSD (Second Life)
Gravity Bubbles   Gravity Bubbles
FractalNet   FractalNet
EcoSim   EcoSim
Cosmic Dust   Cosmic Dust
LandMesh   LandMesh
Imagencrypter   Imagencrypter
Foxy Detector   Foxy Detector
MacDiff   MacDiff
Asteroid Belt   Asteroid Belt
Matrix Attack   Matrix Attack
Solar Swarm   Solar Swarm
Mac OS X Terminal guide   OS X Terminal
Tools   Tools
Names :-)   Names :-)
Mac OS X Terminal guide

Terminal

Page under construction since 16th June 2008...

The Mac OS X Terminal is an extremely useful and powerful application, but many Mac users are put off by the fact that all interaction takes place via typed commands due to the lack of a GUI.  I am therefore creating this guide to illustrate just how useful the Terminal can be.

If you have never used the Terminal before, then check out some of these introductory articles: 1, 2, 3.  (Note that, since Mac OS X is based on Unix, many Unix tutorial articles are also applicable to the Terminal.)  The three commands which you should learn first are:

lsList the contents of a directory
cd Move to a different directory
sudo Authenticate yourself as a superuser to gain more security privileges

You should also learn the four special directory symbols:

. Current directory
..Parent directory
/ Root directory
~ Home directory

It’s also useful to know that OS X stores three different timestamps for every file:

ctimeinode change time (last change time of the file’s owner, permissions, etc)
atimeAccess time (when the data was last accessed)
mtimeModification time (when the actual contents were last modified)

Many Terminal commands have a very large number of available options, so I will only be documenting the options which I find most useful.  If you want more detailed information about a command, consult its manual page.


Table of contents

lsList the contents of a directory
findSearch for files
fs_usageDisplay real-time filesystem activity


ls ls  –  View manual page

Description Lists the contents of a directory.  By default, it lists the contents in alphabetical order, in multiple columns.
Syntax
ls options
List the contents of the current directory, using the specified options.

ls options directory
List the contents of the specified directory, using the specified options.
Options The following options control the layout of the listing.  Only one of these options should be used.

-l List in long format.  The following information is displayed for each file: file mode, number of links, owner name, group name, number of bytes in the file, mtime, and the filename.  (Note: this option is the lowercase letter “L”.)
 
-1 Force output to be one entry per line.  (Note: this option is the digit “one”.)
 
-m Stream output format – list files across the page, separated by commas.


The following options specify how the listing is sorted.

-S Sort contents by size (largest first) instead of alphabetically.
 
-t Sort contents by mtime (most recently modified first) instead of alphabetically.
 
-c When used with -l, display ctime instead of mtime.  When used with -t, sort contents by ctime (most recently changed first) instead of mtime.
 
-u When used with -l, display atime instead of mtime.  When used with -t, sort contents by atime (most recently accessed first) instead of mtime.
 
-r Reverse the order of the sort.


Other options include:

-h When used with -l, use the following suffixes for displaying file sizes: B (byte), K (kilobyte), M (megabyte), G (gigabyte), T (terabyte) and P (petabyte).
 
-T When used with -l, display complete time information for the file, including month, day, hour, minute, second, and year.
 
-a Include directory entries whose names begin with a dot (.).
 
-R Recursively list subdirectories encountered.
 
-G Enable colourised output.
Examples ls -l -S -h -T
List the contents of the current directory, in long format, sorted by size (largest first), and displaying suffixes for file sizes and complete time information.

ls -1 -t -c -r -G /Users/
List the contents of the directory /Users/, with one entry per line, sorted by ctime (oldest change first), and colourised output.


find find  –  View manual page

Description Searches in the specified root directory (including subdirectories), and lists all files which match the search criteria.
Syntax
find root_directory search_criteria

root_directory     Files and subdirectories in this root directory will be searched.
search_criteria     Only files which match all these criteria will appear in the search results.

To search in directories which require high security privileges, use:
sudo find root_directory search_criteria
Search criteria
-name pattern The filename must match pattern (case sensitive).  This can be an exact string, such as "readme.txt", or you can use the special wildcards, including * (matches any zero or more characters) and ? (matches any one character).  These wildcards may be matched explicitly by escaping them with \.  For example, -name "*.txt" matches all files ending in .txt, and -name "questions\?.txt" matches files with the exact name questions?.txt.  More information on patterns can be found here and here.
 
-iname pattern Like -name, but the match is case insensitive.
 
-maxdepth n The depth of the file into the directory tree must not be greater than n directories.  For example, -maxdepth 1 will only match files in the current directory, and -maxdepth 2 will match files in the current directory and one level of subdirectories.
 
-size Rnc If R is not supplied, then the file’s size must be exactly n bytes.
If R is +, then the file’s size must be more than n bytes.
If R is -, then the file’s size must be less than n bytes.
For example, -size 1024c matches files of exactly 1024 bytes, and -size +2048c matches files larger than 2048 bytes.
 
-newerXt timestamp The file must have a newer atime (X=a), ctime (X=c) or mtime (X=m) than timestamp.  There are many valid formats for timestamp, including absolute (eg. '22 Jun 2006 7:40pm') and relative (eg. '1 minute ago', '3 hours ago', etc).  More information on timestamp formats can be found here.
 
-type filetype The file must be of type filetype.  The possible types include d (directory), f (normal file) and l (symbolic link, also known as an alias or shortcut).

Any of the above criteria can have -not added before it, to reverse the condition.  For example, -not -name "*.txt" will match all files whose names don’t end in .txt, and -not -newermt '22 Jun 2006' will match all files whose modification times are not later than 22nd June 2006.

-ls Print detailed information for each matching file.
Examples find ~ -ls -iname "data*" -maxdepth 3
Search from the home directory, and print detailed information for all files whose names begin with data (case insensitive) and are not more than 3 directories deep (including current directory).

find /Users/michaelhogg/Desktop/ -ls -newermt '22 Jun 2006' -size +1024c
Search from the directory /Users/michaelhogg/Desktop/, and print detailed information for all files which were modified after 22nd June 2006 and are at least 1024 bytes in size.


fs_usage fs_usage  –  View manual page

Description Displays filesystem activity in real-time.  The output is formatted according to the size of your window – a narrow window will display fewer columns of data, so use a wide window for maximum data display.
Syntax
sudo fs_usage options processes

options     Filtering and display options.
processes     A space-separated list of process names or IDs.
Options
-w Forces a wider, more detailed output, regardless of the window size.  The data displayed will wrap when the window is not wide enough.
 
-f mode Turns on output filtering based on the mode, which can be network (only network related output is displayed) or filesys (only file system related output is displayed).
Processes The sampled data can be limited to a list of process names or IDs.  (Use Activity Monitor to view a list of active processes).  Processes are supplied to fs_usage as a space-separated list of quoted names (eg. "Safari" "Finder") or IDs (eg. 344 451).
Output format The data columns displayed are as follows:
  • Timestamp
  • System call name
  • File descriptor  F=x
  • Byte count   B=x
  • Error number   [x]
  • Pathname of file (the beginning may be truncated)
  • Fault address   A=0xnnnnnnnn
  • Physical disk block number   D=0xnnnnnnnn
  • File offset   O=0xnnnnnnnn
  • select() system call ready descriptors count   S=x
  • Elapsed time (a W indicates “wait” – the process was scheduled out during this file activity)
  • Process name
Example sudo fs_usage -w -f filesys "Mail" "Dock" "Finder"
Display filesystem activity in wide format for processes called Mail, Dock and Finder.


Automatic country lookup for comments powered by hostip.info
Comments

No comments yet.  Be the first to add a comment!


Add a comment

Contact me  –  Page last updated on 15th August 2008  –  Website hosted by 5quidhost (highly recommended!)
Michael Hogg