40 useful Mac OS X Shell scripts and terminal commands
There are a bunch of terminal commands under Mac OS x. I have classified these commands in a simple way. Many commands here are equally effective in other systems (Windows and Linux, especially Linux/Unix. I hope these commands will help you.
System
Restart Mac OS X:
Disable Mac OS X:
Power Management/power saving
Obtains information about current power management settings.
Set the monitor to off after no activity for 15 minutes
1 |
sudopmset displaysleep 15 |
Sleep the computer after being idle for 30 minutes
OS X appearance
Disable dashboard (do not forget to drag the dashboard Dock icon out)
1 |
defaults write com.apple.dashboard mcx-disabled -boolean YES |
Enable dashboard
1 |
defaults write com.apple.dashboard mcx-disabled -boolean NO |
Force the Finder program to display hidden files
1 |
defaults write com.apple.finder AppleShowAllFiles TRUE |
Force Finder programs not to show hidden files
1 |
defaults write com.apple.finder AppleShowAllFiles FALSE |
Network
Ping a host
Use traceroute to diagnose a route node of a host
Check whether an HTTP service is running on a host or whether a website is available
1 |
curl -I www.bkjia.net |head-n 1 |
Manage Windows networks (equivalent to the NET command in Windows). This command has many options. Run the following command to view these options.
Use dig to diagnose Domain Name Information
Check who is logging on to your Mac Machine
Show System route table
Show Active Network Connections
Show Network statistics
Fault Diagnosis
List all open files
Restart Bonjour-useful when there is no Mac in the Network
1 |
sudolaunchctl unload /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist |
2 |
sudolaunchctl load /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist |
Pop-up CD (not necessarily disk1)
Text operation commands
You often need to obtain some text from the clipboard or file and convert and use the text. The commands listed here are related to text processing.
Count the number of lines of text in the clipboard
Count the number of words in the text on the clipboard
Sort the text lines in the clipboard and re-write them back to the clipboard.
Reverse the text lines in the clipboard and return them to the clipboard.
Removes duplicate lines of text from the clipboard and writes them back to the clipboard.
1 |
pbpaste |sort|uniq| pbcopy |
Find the duplicate row in the Chinese text of the clipboard, copy the row, and write it back to the clipboard (including the row of the duplicate row)
1 |
pbpaste |sort|uniq-d | pbcopy |
Find the duplicate row in the Chinese text of the clipboard, copy the row, and write it back to the clipboard (excluding duplicate rows)
1 |
pbpaste |sort|uniq-u | pbcopy |
Clears HTML text from the clipboard and writes it back to the clipboard.
1 |
pbpaste | tidy | pbcopy |
Display the first five lines of text in the clipboard
Display the last five lines of text in the clipboard
Convert the tabs in the text in the clipboard into spaces.
1 |
pbpaste |expand| pbcopy |
Other useful commands:
1 |
htpasswd -nb username password |
B:
2 |
AuthName"restricted area" |
3 |
AuthUserFile /path/to/your/site/.htpasswd |
Display the commands previously entered in the terminal window
Convert a file to HTML. The supported formats include Text,. RTF, And. DOC.
1 |
textutil -convert htmlfile.extension |
Nano is a simple and easy-to-use text editor that can be used to quickly change text files. It is much weaker than vim, but it is very convenient.
In the nano editor, you can use ctrl + o to maintain and ctrl + x to exit.
Clear content displayed on the terminal
ITunes Problems
Change the iTunes link behavior to the local iTunes library instead of the iTunes Store.
1 |
defaults write com.apple.iTunes invertStoreLinks -bool YES |
Change the iTunes link behavior to iTunes Store instead of the local iTunes library
1 |
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: In the previous examples, we used pbpaste to obtain data from the clipboard, or cat to obtain data from the file.
Replace pbcopy:
1 |
> ~/Desktop/filename.txt |
Original English, OSCHINA Original translation