Make things happen quickly without touching the mouse
This tutorial describes how to make use of the macOS Terminal to make your life easier and less frustrating.
Some Mac users may require the ability to erase a disk or erase a hard drive from the command line on Mac OS, a task which is typically performed through the Disk Utility application from the GUI. The command line approach to disk erasure in macOS is a bit different and it requires precise syntax to. Answer: Type DIR to show the folders and files in command prompt. DIR is the MS DOS version of LS, which lists the files and folders in the current directory. Here is a huge list of all the Linus terminal commands and their Windows equivalents. To get help on a Windows command, use the /? Option, for example date /?
What Apple calls the Terminal is what Linux people call the shell (more specifically, the Bash shell). It’s also called a command-line terminal, abbreviated as CLI.
Information here is often used in interview questions.
Open Terminal (several ways)
Terminal is kinda buried perhaps because those who use a MacOS laptop just for social media probably won’t need a Terminal.
But if you’re a developer, it’s hard to get away from using a CLI.
There are different ways to open a Terminal command line.
My preferrence is a way that doesn’t require reaching for a mouse and using the least number of keystrokes:
Alternately, if you prefer moving your mouse:
PROTIP: If you are at the Finder program (since Yosemite) you can open a Terminal to a folder listed within Finder by pointing your mouse on it, then tapping with two fingers on the touchpad/mousepad.To enable that:
Text wrapping
This page contains notes for system administrators and developers,who need to control Macs below the UI level, which requiretyping commands into a command-line terminal screen.
iTerm2 for split pane
Many prefer to install and use iTerm2 instead of the built-in Terminal program.Install iTerm2 using Homebrew:
List Of Command Prompt Commands
Terminal does not support but iTerm2 does support dividing the CLI into several rectangular “panes”, each of which is a different terminal session:
Pressing the shortcut again restores the hidden panes.
On Linux, there is the
screen command.
Command Prompt For Macbook Air
See Iterm2 Cheat Sheet of iTerm2 keyboard shortcuts. https://github.com/nobitagit/iterm-cheat-sheet/blob/master/README.md
Alphabetical Commands list
A list of all commands native to macOS is listed alphabetically at https://ss64.com/osx.
Exit
To exit from the Terminal shell:
exit
Get back in for the remainder of this tutorial.
Shutdown
CAUTION: To kill all apps and shutdown a Mac right away (with no warning and no dialog):
sudo shutdown -h now
Text Command Line Bash Shortcuts
These come from the bash terminal on Linux machines here: Press control with your pinkie, then …
Environment Variables
A big reason to use a command-line terminal is to set environment variables.
Like on PCs, the PATH system environment variable storeswhere the operating system should look to find a particular program to execute.
Default editor
Command history
Foreground processes and background jobs
Folders accessed by developers
Terminal File Listing Home Folder
By default, the Terminal shows the hard drive and lowest level file folder name, in white letters over black.
Show Hidden Invisible Files in Finder
By default, the Mac’s Finder does not show hidden files.
For more on this, see this.
Create Terminal AliasesWireless up and down
Most developers leave files un-hidden.
tree alias or brew install
OSX does not come with the tree command that many other Linux distributions provide. So add it using:
If you don’t want to install a program, add an alias for a tree command by adding this in the ~/.bash_profile script:
Alternately, add it by installing a command using brew:
Active Terminal sessions need to be closed so new Terminal | Shell | New Window | Shell has this activated.
See list of parameters:
List only 2 levels deep with human-readable file size kilobytes and sort by last modified date:
Cursor to Screen Hot Corners
By default, if you move the mouse to one of the corners of the screen,stuff happens. It can be annoying.
Hosts file
Mac, Windows, and Linux systems have a hosts file that locally does the work of the public DNS– translating host names (typed on browser address field) to IP address numbers.
Terminal Ping Host
Find the IP address of a website host name:
SSH tunnel
To access a remote server through a port that is not open to the public:
DNS Configuration with NameBench
Analysis at one time showed this ranking by speed:
Google Namebench tries the speed of various DNS servers from YOUR machine (which takes some time) and pops up in your browser this:
An example:
Clear DNS Cache
Different commands are needed for different versions of OS.OSX 10.10 added requirement for sudo when using the built-in discoveryutil:
sudo discoveryutil udnsflushcaches
Bash Profile Configuration
The profile file is run during boot-upto configure the terminal to define file path, shims, and autocompletion handlers.
This is the single biggest frustration with people using Linux on Mac.
One of the earliest articles on bash hereshows shell variables, environment variables, and aliases.
Each operating system has its own file name for its profile:
PROTIP: If there is both a .bash_profile and a .profile file, boot-up only executes the first one it finds.
According to the bash man page, .bash_profile is executed during login before the command prompt,while .bashrc is executed for interactive non-login shells such aswhen you start a new bash instance by typing /bin/bash in a terminal.
Here’s what my profile file begins:
https://github.com/gcuisinier/jenv/blob/master/README.md
Operating System Kernel
I can use Linux commands in my version of the operating system:
uname -a (a for all) or uname -rvm
returns:
14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
which is a combination of:
uname -r for release number,
uname -v for kernel version, uname -m for model:
x86_64 for Intel or AMD 64-bit or
i*86 for 32-bit.
For more information about Darwin operating systemdeveloped at Apple, see:
NOTE: lsb_release -awhich works on Debian, RHEL 6.6, and Ubuntu is not recognized on Gentoo nor CentOS 6,which has no folder /etc/lsb-release.
See Distriwatch.com,which describes releases of different Linux distributions.
Setup Your Mac Like a Pro
Paul Irish is one of top pros among developers, and now a Google Evangelist.He put his Mac configuration settings ongithub.com/paulirish/dotfiles. But he recommends cloning github.com/mathiasbynens/dotfiles/.
On the Git page notice that he has established an industry convention of usingProjects folder we defined earlier.
On the Git page I clicked on Clone in Desktop.
The library is called dotfiles because that’s what hidden files are called,and most configuration files are hidden.
Edit terminal prompt setting
Paul Irish offers his setup-a-new-machine.sh athttps://github.com/paulirish/dotfilesZShell (included with Mac and can be set as the default in Terminal)* oh-my-zsh as a ZShell framework* The oh-my-zsh Git plugin* And the oh-my-zsh theme called jnrowe</p>
By default, if you have a long file name, it would leave little room to type in commands before it wraps to the next line.
To redefine what appears in the prompt,edit this file using the vi editor that comes with each Mac:vi .bashrc Copy this and paste to the bottom of the .bashrc file:
The command above uses global parameters $USER and $PWD,plus colors from this list.
Root user for sudo commands
If you try a command that responds about “permissions denied”, you need to execute as a root user.
The root user has the ability to relocate or remove required system files and to introduce new files in locations that are protected from other users. A root user has the ability to access other users’ files.
Any user with an administrator account can become the root user or reset the root password.
Under a *nix system like MacOS you must have “root” (administrative) privileges to start IP-services using ports smaller than 1024.
After MacOS install, the root or superuser account is not enabled. While it is possible to enable the root account, once enabled, if forgetten, you’ll have to reboot from the installer drive (a hassle).
PROTIP: There are several ways to invoke sudo*
PATH
NOTE: The folders that bash looks into are in bin:
/bin/echo $PATH
On a fresh Yosemite, that would contain:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Each additional app adds to the front of the list:
/Library/Frameworks/Python.framework/Versions/3.4/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Separating the folders between colon separator:
New folders are added to the front of the PATH using a command such as:
export PATH=<new folders>:$PATH
Depending on how you’re setup, file ~/.profile or ~/.bash_profile or ~/.bash_login contains the path echo’d.
Or your PATH may be set in /etc/profile for all users
Create Windows-like shortcuts with parameters using text editor
http://www.jesseweb.com/coding/automator/create-windows-like-shortcuts-with-parameters/
Mac OSX doesn’t allow you to create shortcuts like Windows.OSX alias don’t allow parameters (ex. create a Screen Sharing shortcut that connects to a specific computer).
Jessie suggests this to create a Windows like shortcut with parameters in the Comments field.
Another alternativeis to use a text editor to create URL shortcut fileslike the ones Windows Internet Explorer stores its bookmarks. Apple Safari recognizes them when clicked within Finder.So they are cross-platform.
Mount .dmg files using hdiutil tool
IPv6 compatibility with Curl command line apps
curl http://localhost:3000
Previously, when invoked on Mac OS 10.10 (Yosemite), you needed to add a parameter to make the request use IPv4:
curl http://localhost:3000 –ipv4
Otherwise, even if the URL loads fine in a browser, you will see an error message such as:
curl: (7) Failed to connect to localhost port 3000: Connection refused
This occurs because curl, under Yosemite, uses IPv6 by default but some apps, such as LoopBack.io, by default uses IP v4.
See if you see IP v6 entries in your hosts file (::1 localhost, fe80::1%lo0 localhost). If they are there it is likely that curl is making requests using IP v6.
You can make your LoopBack app use IPv6 by specifying an IPv6 address as shown below:
Command Prompt For Mac AddressLargest files taking up disk space
Linux has a ncdu (NCurses Disk Usage) utility to list files in order of how much space they occupied.
Empty Trash
When files or folders are moved to Trash, they are sent to folder
~/.Trash .
To recover disk space taken up by files which have been moved to Trash, there are several ways:
Ulimit Too Many Files
By default, operating systems limit how many file descriptors to allow.Each operating system version has a different approach.
Windows Command Prompt For Mac
Linux operating systems have this command:
ulimit -a
On my Sierra the response was:
PROTIP:
launchctl is a rough equivalent to the systemctl command used in Linux systems.launchctl interfaces with launchd to load, unload daemons/agents and generally control launchd.
Disable System Integrity Protection
Some programs make calls to the operating system which OSX began to see as a threat, beginning with El Capitan.
Apple says System Integrity Protection blocks code injection (and many other things).
But what about useful programs (such as XtraFinder)which works by injecting its code into Finder and other application processes?
To get around this, you need to partially disable System Integrity Protection in OS X El Capitan.See Apple’s article on how:
Skill Certification
Video course Mac OS X Support: Installation and Configureis the first of courses on Plurasight towardApple Certified Support Professional (ACSP)
Dotfile Settings from others
Daemons and Agents
Resources:
[2] VIDEO
[3] Advanced Bash-Scripting Guide by Mendel Cooper 2012
[4] CommandLineFu.com
https://zwischenzugs.com/2018/01/06/ten-things-i-wish-id-known-about-bash/https://leanpub.com/learnbashthehardway
https://blog.flowblok.id.au/2013-02/shell-startup-scripts.htmlhttps://bitbucket.org/flowblok/shell-startup/src/default/
https://linuxaria.com/howto/7-hidden-features-of-bash
More on OSX
This is one of a series on Mac OSX:
Please enable JavaScript to view the comments powered by Disqus.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |