If you are moving onto macOS/OS X from GNU/Linux, you would probably find out that the command line tools shipped with OS X are not as powerful and easy to use as the tools in Linux. The reason is that macOS/Mac OS X uses the BSD version command line tools, which are different from the Linux version, while they are both compliant with POSIX standards. But we can easily install the GNU command line tools by using Homebrew in Mac OS X and set them as default.
Note: you need to notice that you may have some compatibility issues with shell scripts written specifically for OS X after you have replaced your OS X commands with the GNU version. Although the very vast majority of shell scripts have no problem, you just need to be aware that when there comes a problem, this may be the spot to check on.
First, visit Homebrew homepage and follow the installation instructions to install Homebrew.
Shortcut: install the latest XCode and then run the following command to install:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then add the following line to your .bashrc or .zshrc:
export PATH="$(brew --prefix coreutils)/libexec/gnubin:/usr/local/bin:$PATH"
Install the GNU Command Line Tools
brew install coreutils
GNU Coreutils contains the most essential UNIX commands, such as ls, cat.
Then you may probably want to install the following ones (For some of the packages, you need to run brew tap homebrew/dupes first, but only once for your system):
brew install binutils brew install diffutils brew install ed --with-default-names brew install findutils --with-default-names brew install gawk brew install gnu-indent --with-default-names brew install gnu-sed --with-default-names brew install gnu-tar --with-default-names brew install gnu-which --with-default-names brew install gnutls brew install grep --with-default-names brew install gzip brew install screen brew install watch brew install wdiff --with-gettext brew install wget
--default-names option will prevent Homebrew from prepending a g to each of the newly installed commands, thus we could use these commands as default commands over the ones shipped by OS X.
In addition, some GNU command line tools already exist by default on OS X, but you may want a newer version:
brew install bash brew install emacs brew install gdb # gdb requires further actions to make it work. See `brew info gdb`. brew install gpatch brew install less brew install m4 brew install make brew install nano
As a complementary set of packages, the following ones are not from GNU, but you can install and use a newer version instead of the version shipped by OS X:
brew install file-formula brew install git brew install openssh brew install perl brew install python brew install rsync brew install svn brew install unzip brew install vim --override-system-vi brew install macvim --override-system-vim --custom-system-icons brew install zsh
Now you should have an easier command line system in your OS X. Have fun with them!
Update 1: You may also want to add $HOMEBREW_PREFIX/opt/coreutils/libexec/gnuman to the MANPATH environmental variable, where $HOMEBREW_PREFIX is the prefix of Homebrew, which is /usr/local by default. (Thanks Matthew Walker!) Alternatively, there is also a one-line setup which you could put in your shell configuration files here by quickshiftin.
Update 2 All the
brew install commands above have been put into one script on GitHub for downloads.