Apple Terminal

From HaFrWiki
Jump to: navigation, search

Terminal is a low level tool to access all your Apple iMac settings. That makes terminal a very nice and powerful tool, but also a very dangerous one.

Defaults

All Apple designed applications have settings that can be changed using the terminal. The default setting of these variables are stored in the directory structure of the App in a file Defaults.plist. This file is ReadOnly and can not be altered. The first way to alter the content of this file, is by using the command:

  defaults write <name-of-application> <variable-name> <variable-type> <new-settings>

Finder

As always an example helps. To show all hidden files in Finder use:

  defaults write com.apple.finder AppleShowAllFiles -bool YES
  or
  defaults write com.apple.finder AppleShowAllFiles TRUE

It maybe necessary to reload finder. To kill al Finder instances:

   killall Finder

Please note this command is case sensitive.

Alternative

There is an alternative for this way of editing. Because this way may cause the system to be instable this method is not recommended for beginners. The defaults ate mostly stored in the file Defaults.plist, a property file of the Xcode system. This file can not be edited without making modification.


Step 1: Get the location of the Defaults.plist file. First go to the directory where the Application is located i.e. for Safari

  > cd /Applications/Safari.app
  > find . -name Def*.plist

Go to the found directory

  > cd Contents/Resources


Step 2: Inspect the current access rights of file

  > stat -x Defaults.plist
    File: "Defaults.plist"
    Size: 4191         FileType: Regular File
    Mode: (0644/-rw-r--r--)         Uid: (    0/    root)  Gid: (    0/   wheel)
  Device: 14,2   Inode: 4775514    Links: 1
  Access: Wed Oct 19 11:51:38 2011
  Modify: Wed Oct 19 11:52:49 2011
  Change: Wed Oct 19 11:53:14 2011

Note the Mode value for chmod is 0644.

Step 3: Change the access right of the file. Because the file is protected use the sudo command

  > sudo chmod -v ogn+w Defaults.plist
  Password: XXXXXXX

The system master password is necessary for performing this command.

Step 4: Check the changed mode:

  > stat -x Defaults.plist

Note that the Mode value has been changed by chmod into 666.

Step 5: Edit the file with vi

  > vi Defaults.plist


Step 6: After changing the file, reset the file access to the old value

 > sudo chmod -v 644 Defaults.plist

Dock

Make hidden applications's dock icons translucent

  defaults write com.apple.Dock showhidden -bool YES

iTunes

Normally the arrows next to artists and albums in your iTunes library search the iTunes store when you click them. This command changes them so that clicking will search your iTunes library instead.

   defaults write com.apple.iTunes invertStoreLinks -bool YES

Safari

Enable the debug menu in Safari.

   defaults write com.apple.safari IncludeDebugMenu -bool YES

Set the history limit in Safari to a certain number of items and and/or a certain age.

   defaults write com.apple.Safari WebKitHistoryItemLimit 2000

and/or

   defaults write com.apple.Safari WebKitHistoryAgeInDaysLimit 30

Unix on Mac

The Unix on the Mac is of course not completely the same as other Unix variants. Here some useful issues:

chmod & stat

To view the human readable and the decimal version of the file access rights, you can use the following trick:

  stat -f  "%Sp - %p :  %N" <filename> 
  •  %Sp : Shows the file access in the human readable form (-rw-r--r--)
  •  %p  : Shows the file access in a decimal form ( 100644)
  •  %N : Shows the filename

Example output:


Another example:

  stat -f "%Sp ->owner=%SHp group=%SMp other=%SLp  %p : %N " *

Shows:

  -rw-r--r-- ->owner=rw- group=r-- other=r--  100644 : Defaults.plist 
  -rw-r--r-- ->owner=rw- group=r-- other=r--  100644 : DownloadResume.tif 
  -rw-r--r-- ->owner=rw- group=r-- other=r--  100644 : DownloadResumePressed.tif 
  -rw-r--r-- ->owner=rw- group=r-- other=r--  100644 : DownloadResumePressed_Selected.tif 

Or

  drwxr-xr-x ->owner=rwx group=r-x other=r-x  40755 : /Users/frielink/Public 
  lrwxr-xr-x ->owner=rwx group=r-x other=r-x  120755 : /Users/frielink/Send Registration 
  drwxr-xr-x ->owner=rwx group=r-x other=r-x  40755 : /Users/frielink/Sites 
  drwxr-xr-x ->owner=rwx group=r-x other=r-x  40755 : /Users/frielink/VOF De Kunst 
  -rwxr-xr-x ->owner=rwx group=r-x other=r-x  100755 : /Users/frielink/finderAll.sh 

Another very useful command:

  stat -x <file>

Gives:

   File: "Defaults.plist"
   Size: 4190         FileType: Regular File
 Mode: (0644/-rw-r--r--)         Uid: (    0/    root)  Gid: (    0/   wheel)
 Device: 14,2   Inode: 4775514    Links: 1
 Access: Wed Oct 19 09:47:47 2011
 Modify: Mon Jul 25 23:24:53 2011
 Change: Mon Jul 25 23:24:53 2011



brew

The most easiest way to install ant and maven use (home)brew [1].

brew install ant
brew install maven

The targets are installed in the directory

  • /usr/local/Cellar/ant/1.9.2./bin/ant
  • /usr/local/Cellar/maven/3.1.1/bin/mvn

Symbolic links are created in:

  • /usr/local/bin/ant
  • /usr/local/bin/mvn

Do not use the M2_HOME, it will cause maven (mvm) to crash.

tmux

The command line app 'tmux' is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.

When tmux is started it creates a new session with a single window and displays it on screen. A status line at the bottom of the screen shows information on the current session and is used to enter interactive commands.

A session is a single collection of pseudo terminals under the management of tmux. Each session has one or more windows linked to it. A window occupies the entire screen and may be split into rectangular panes, each of which is a separate pseudo terminal (the pty(4) manual page documents the technical details of pseudo terminals). Any number of tmux instances may connect to the same session, and any number of windows may be present in the same session. Once all sessions are killed, tmux exits.

See the Install for information on Install and Configuration for Apple OS X.

Install

# First install tmux
brew install tmux
 
# For mouse support (for switching panes and windows)
# Only needed if you are using Terminal.app (iTerm has mouse support)
Install http://www.culater.net/software/SIMBL/SIMBL.php
Then install https://bitheap.org/mouseterm/
 
# More on mouse support http://floriancrouzat.net/2010/07/run-tmux-with-mouse-support-in-mac-os-x-terminal-app/
 
# Enable mouse support in ~/.tmux.conf
set-option -g mouse-select-pane on
set-option -g mouse-select-window on
set-window-option -g mode-mouse on
 
# Install Teamocil to pre define workspaces
https://github.com/remiprev/teamocil
 
# See http://files.floriancrouzat.net/dotfiles/.tmux.conf for configuration examples

Notes:


Before installing tmux on an iMac (OSX) you need to have Xcode. After installing Xcode you need to accept the license.

$ xcodebuild -license


If you receive this as an error:

$ brew install tmux
Error: You must `brew link pkg-config' before tmux can be installed
$ brew link pkg-config
Linking /usr/local/Cellar/pkg-config/0.28... Warning: Could not link pkg-config. Unlinking...
Error: Permission denied - /usr/local/Library/LinkedKegs

You will need to do the following command to get owner rights on the required folder:

$ sudo chown -R `whoami` /usr/local

And follow with

$ brew install tmux

Usage

Command Description
$ tmux Begins a new tmux session
Ctrl-b : Command prompt
Ctrl-b , Rename window
Ctrl-b % Splits a window/pane into 2 panes
Ctrl-b " Splits a window/pane into 2 pane horizontally
Ctrl-b o Switches from one pane to the next
Ctrl-b c Creates a new window and switches to the new window
Ctrl-b n Moves to the next window
Ctrl-b p Moves to the previous window
Ctrl-b w Chooses the window
Ctrl-b d Detaches from a session.
$ tmux attach -t [session name] Re-attaches to a session.
Ctrl-b ? Lists all command keys.

Aliases

Alias Command Description
attach attach-session [-dr] [-t target-session] If run from outside tmux, create a new client in the current terminal and attach it to target-session. If used from inside, switch the current client.
detach detach-client [-P] [-a] [-s target-session] [-t target-client] Detach the current client if bound to a key, the client specified with -t, or all clients currently attached to the session specified by -s.
has has-session [-t target-session] If exists return 0 else return 1.
  kill-server Kills the tmux server, clients and sessions.
  kill-session [-a] [-t target-session] Destroy the given session, closing any indows linked to it and no other sessions, and detaching all clients attached to it. If -a is given, all sessions but the specified one is killed.
lsc list-clients [-F format] [-t target-session] Lists all clients attached to the server.
lscm list-commands Lists the syntax of all commands supported by tmux.
ls list-sessions [-F format] List all sessions managed by the server.
lockc lock-client [-t target-client] Lock target-client, see the lock-server command.
locks lock-session [-t target-session] Lock all clients attached to target-session.
new new-session [-AdDP]
  [-F format] [-n window-name]
  [-s session-name] [-t target-session]
  [-x width] [-y height] [shell-command]
Creates a new session with name session (-s session-name) and optional window-name (-n window-name).
If -t is given, the new session is grouped with target-session. This means they share the same set of windows - all windows from target-session are linked to the new session and any subsequent new windows or windows being closed are applied to both sessions. The current and previous window and any session options remain independent and either session may be killed without affecting the other. Giving -n or shell-command are invalid if -t is used.
The -A flag makes new-session behave like attach-session if session-name already exists; in the case, -D behaves like -d to attach-session.
refresh refresh-client [-S] [-t target-client] Refresh the current client if bound to a key, or a single client if one is given with -t. If -S is specified, only update the client's status bar.
rename rename-session
  [-t target-session] new-name
Rename the session to new-name.
showmsgs show-messages [-t target-client] Any messages displayed on the status line are saved in a per-client message log, up to a maximum of the limit set by the message-limit session option for the session attached to that client. This command displays the log for target-client.
source source-file path Execute commands from path.
start start-server Start the tmux server, if not already running, without creating any sessions.
suspend suspend-client [-t target-client] Suspend a client by sending SIGTSTP (tty stop).
switch switch-client [-lnpr] [-c target-client]
  [-t target-session]
Switch the current session for client target-client to target-session. If -l, -n or -p is used, the client is moved to the last, next or previous session respectively. -r toggles whether a client is read-only (see the attach-session command).

Tutorial

Creates a first session called session1 which will have its first window called s1win1:

   $ tmux new -s session1 -n s1win1

Creates a second session from within the running tmux client instance. Start with typing Ctrl-b ;, which gives you a prompt, now type

  new -s session2 -n s2win1

Useful Terminal Commands

A small recap from the page 40 Terminal Tips and Tricks you never thought you needed [2].

View File System Usage

What's is your Mac doing when it comes to reading and writing to disk.

$ sudo fs_usage

To cancel use ctrl-C.

Rebuild spotlight

Spotlight sometimes does not work properly. To re-index use the mdutil command (Manage the metadata stores used by Spotlight): [3]

$ sudo mdutil -E /

Open documents

To open documents directly from Terminal in the required application:

$ open -a /Applications/<appname.app> /<path-to-the-file>

Check Mac uptime

To see how long your Mac is up use:

$ uptime

Install OSX Update

Install system updates without having to launch it.

# To see available software updates for your Mac:
$ sudo softwareupdate -l
# After a few minutes, you’ll be given a list of available updates.
# If you’d like to install all available updates, enter:
$ sudo softwareupdate -ia

View all Processes

The app Activity Monitor can give us a detailed view of what our Mac is currently doing. To replicate much of its functionality in Terminal: [4]

# Basic Usage
$ top
# Sort on memory descending
$ top -o mem
# Sort on command name ascending
$ top -o +command

Use ctrl-C to end or more elegant Q.

See also

top

References

top

  1. [https://brew.sh brew homepage.
  2. Computers++.com, 40 Terminal Tips and Tricks You Never Thought You Needed
  3. ss64-osx, mdutil - Manage the metadata used by Spotlight.
  4. ss64-osx, top, List running processes on the system, in sorted order.
    Periodically displays a list of processes on the system in sorted order.
    The default key for sorting is pid, but other keys can be used instead.