Mac Terminal Reference: 40+ Useful OS X Terminal Commands
来源:互联网 发布:access与数据透视表 编辑:程序博客网 时间:2024/04/29 23:12
Reference:http://www.leftcolumn.net/2009/02/15/40-useful-mac-terminal-commands/
Here are a bunch of Mac terminal commands sorted into general categories. I have intentionally omitted long bash scripts and AppleScripts and focussed instead on small useful commands that can be plugged into bigger scripts or used on their own… enjoy!
Terminal & Shell Basics
cmd+n – Open a new Shell in a new window
cmd+t – Open a new Shell in a new tab of the current window
control+d – Logout the Shell in the current tab / window
cmd+d – Split pane. this is not a new shell, just a way of displaying the current Shell.
System
Restart Mac OS X:
sudo shutdown -r now
Shutdown Mac OS X:
sudo shutdown now
Power Management / Energy Saving
Get overview of current Power Management Settings:
pmset -g
Put display to sleep after 15 minutes of inactivity:
sudo pmset displaysleep 15
Put Computer to sleep after 30 minutes of inactivity:
sudo pmset sleep 30
…Also see my post about hibernate mode and Safe Sleep on the Mac
OS X Look and Feel
Permanently disable Dock icon bouncing
If you don’t like the way Mountain Lion now makes the User ‘Library’ folder invisible, you can disable this.
chflags nohidden ~/Library
…you don’t need to relaunch the Finder.
Disable Dashboard (don’t forget to drag the Dashboard Dock icon off the Dock too):
defaults write com.apple.dashboard mcx-disabled -boolean YESkillall Dock
Enable Dashboard:
defaults write com.apple.dashboard mcx-disabled -boolean NOkillall Dock
Force the Finder to show hidden files (very useful for Web Developers who need to edit .htaccess files, for example):
defaults write com.apple.finder AppleShowAllFiles TRUE
Force the Finder to hide hidden files (ie: back to the default setting):
defaults write com.apple.finder AppleShowAllFiles FALSE
Networking
Ping a host to see whether it’s available:
ping -o leftcolumn.net
Troubleshoot routing problems to a host using traceroute:
traceroute leftcolumn.net
Check whether a host is running an HTTP server (ie: check that a Web Site is available):
curl -I www.leftcolumn.net | head -n 1
Automatically enable Internet Sharing at startup
Manage Windows networks (a drop-in for the NET command on Windows). Too many options to list here, so run this for details:
man net
Use dig to discover Domain information:
dig www.leftcolumn.net Adig www.leftcolumn.net MX
…and you can also retrieve all available stuff in the DNS Zone for a domain with eg:
dig -t ANY google.co.nz
Who is logged in to your Mac?
w
What’s my user name? This is really useful for bash scripts etc.
whoami
Show routing table:
netstat -r
Show active network connections:
netstat -an
Show network statistics:
netstat -s
Troubleshooting
List all open files (this will take a few seconds to complete on most Macs):
lsof
Restart Bonjour – handy when a Mac ‘disappears’ from the Network:
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.mDNSResponder.plistsudo launchctl load /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
Eject a CD… it’s never happened to me but you can eject a stuck cd with the following. Note that it won’t always be ‘disk1′:
diskutil eject disk1
Text Manipulation terminal commands
Sometimes you need to take some text from the clipboard or a file, transform it somehow and then use it. Here are a bunch of mac terminal commands that do text manipulation. I’ve assumed you want to transform text from the clipboard and back again; see the notes at the end of the article for info on how to write to and from files instead.
Count number of lines in the text in the Clipboard:
pbpaste | wc -l
Count number of words in the text in the Clipboard:
pbpaste | wc -w
Sort lines of text in the Clipboard and copy them back to the Clipboard:
pbpaste | sort | pbcopy
Reverse each line of text in the Clipboard (ie: make each line appear backwards) and copy them back to the Clipboard:
pbpaste | rev | pbcopy
Strip duplicate lines from lines of text in the Clipboard and copy only one instance of each duplicate line back to the Clipboard (output is sorted):
pbpaste | sort | uniq | pbcopy
Find duplicate lines from lines of text in the Clipboard and copy only one instance of each duplicate line (stripping non-duplicates) back to the Clipboard (output is sorted):
pbpaste | sort | uniq -d | pbcopy
Strip duplicate lines from lines of text in the Clipboard and copy only one instance of each line (stripping duplicates entirely) back to the Clipboard (output is sorted):
pbpaste | sort | uniq -u | pbcopy
Tidy up HTML in the Clipboard and copy it back to the Clipboard:
pbpaste | tidy | pbcopy
Display the first 5 lines from the Clipboard:
pbpaste | head -n 5
Display the last 5 lines from the Clipboard:
pbpaste | tail -n 5
Convert tabs to spaces for the lines in the Clipboard:
pbpaste | expand | pbcopy
Other useful commands
Password protect your web site! Create a CRYPTed user/password for using in a .htpasswd file. Save the outputted results of A below to a file called .htpasswd in the directory you want to secure. Then save the contents of B to a file called .htaccess in the same folder.
A:
htpasswd -nb username password
B:
AuthType BasicAuthName "restricted area"AuthUserFile /path/to/your/site/.htpasswdrequire valid-user
Display a history of commands used in the terminal by the current user:
history
Convert a file to HTML. Support formats are Text, .RTF, .DOC.
textutil -convert html file.extension
Nano is a very easy-to-use text editor for quick changes to text files. It is less powerful than VIM but has the advantage of clearly showing you the common editing commands:
nano [file_to_edit]
…In nano, use ctrl+o to Save and ctrl+x to quit.
Greg shared a way of tidying the terminal window. Essentially this command scrolls down a page, clearing the current view:
clear
iTunes
Change iTunes link behaviour to point at local iTunes Library instead of iTunes Store:
defaults write com.apple.iTunes invertStoreLinks -bool YES
Change iTunes link behaviour to point at iTunes Store instead of local iTunes Library (ie: back to the default):
defaults write com.apple.iTunes invertStoreLinks -bool NO
Other Mac OS X Terminal Resources
Mac OS X Hacking Tools (old but detailed list for the obsessive only).
Cameron Hayne’s Bash Scripts
Mac OS X Hints
Apple Forums
Note: For commands where I’ve used pbcopy to get the contents of the Clipboard as input, you can use the contents of a file as input instead. Swap pbpaste for:
cat [/path/to/filename]
And to put the results into a file on your desktop, just swap | pbcopy for:
> ~/Desktop/filename.txt
- Mac Terminal Reference: 40+ Useful OS X Terminal Commands
- Mac OS X Terminal speed open
- 2014.2.20 mac OS X terminal
- Mac OS X terminal命令收集
- Mac OS X Terminal:终端使用初级教程
- Mac OS X Terminal 终端使用初级教程
- Mac OS X Terminal:终端使用初级教程
- mac os x 设置Terminal快捷键
- Mac OS Terminal设置
- Mac OS X下终端(Terminal)常用的命令
- How to make a terminal alias for Mac OS X
- Mac OS X Terminal 101:终端使用初级教程
- Mac OS X Terminal 101:终端使用初级教程
- Mac OS X Terminal 101:终端使用初级教程
- Mac OS X Terminal 101:终端使用初级教程
- Mac OS X Terminal 101:终端使用初级教程
- Mac OS X Terminal command入门与命令汇总
- Mac OS X Terminal 101:终端使用初级教程
- 神奇的拉普拉斯平滑(Laplacian Smoothing)及其在正则化上的应用~
- Arrays.asList 分析
- 将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替
- OpenSSL API编程
- Linux 添加用户及相关配置
- Mac Terminal Reference: 40+ Useful OS X Terminal Commands
- 关于城市列表按首字母排序、分组及查找的一个解决方案(mysql端)java
- FPGA音频编解码驱动及I2C写入代码
- UVA - 216 Getting in Line (求最短路径)
- SQL Server 2008中文版标准教程读书笔记 表数据的增删改查(复习SQL Server 四)
- 华为机试题:求偶数个复数的平均值
- android振动功能Vibrator
- [译文]JOAL教程 第六课 高级加载方式与错误处理
- iOS静态库(.a 和framework)