UNIX Tips & Tricks To Save You Time In Terminal - Tips N TRIKS

Thursday, 13 February 2014

UNIX Tips & Tricks To Save You Time In Terminal

UNIX Tips & Tricks To Save You Time In Terminal

UNIX Tips & TricksMany web developers these days use the command line on Unix based machines. If they're not using it for version control, they'll more than likely be using it for server administration or compiling preprocessed CSS.
As a digital agency, we find that when using the command line, typing similar commands regularly can become frustrating. By the end of this tutorial, you'll be able to reduce your time spent in terminal typing long, laborious commands by using a range of Unix commands, functions and aliases using Mac OSX.
Recommended Level: Intermediate

UNIX for Mac Tutorial

SETTING UP AN ALIAS

Throughout this tutorial I'll be using the term 'aliases'. In Unix (bash) aliases are saved in your ~/.profile,~/.bash_profile or ~/.bashrc file and are extremely simple to add.
In Unix, aliases are custom commands written by you, the user, to abbreviate a command to a shorter version. This shorter command will be quicker to type and easier to remember.
When creating an alias I use Nano as my text editor, others may prefer to use Vim for example.
Text Editor
When you've input the above, hit enter. This will then open the file .bashrc.
The syntax for creating an alias is:
1
alias alias_title="command to execute"
The line above means that “alias_title” is now a shorter version of "command to execute" Saving and then running "alias_title" in Terminal will not work as “command to execute” as it is not a Unix command. To save this file in Nano, press CTRL + o, enter and then CTRL + x.
CTRL + o and CTRL + x are Nano short cut keys. CTRL + o allows you to save the file without exiting, and CTRL + xallows you to exit Nano. There are more shortcuts here.
For aliases to take effect you'll have to restart Terminal completely. On OS X, simply use the shortcut CMD + Q (Quit), then reopen Terminal.

GIT

Git version control is used en masse amongst the web development world and most developers tend to use the command line version. It can become rather frustrating typing the same commands each time, so why not set up some aliases.
1
2
3
4
5
6
alias gpp="git pull; git push"
alias gp="git pull"
alias gpu="git push"
alias gs="git status"
alias ga="git add ."
alias gr="git reset --hard HEAD"
If you know or use Git, you'll understand these commands already. If not, there is quite a comprehensive guide here. Next, you can just type "gp" in Terminal, and it will pull down any changes from Git. Let's take Git tips a step further and write some bash functions. - Reference: https://gist.github.com/AndrewHathaway/4983844
1
2
3
4
5
6
cap() {
git add . ;
git commit -m "$1";
git pull;
git push;
}
Git
This function will complete four commands in one and pass in the commit message. Following this, we can type cap 'commit message'; and it will perform the commands in the function.
For our last section on Git, we can identify our Git commands using colors. Using a text editor, add the following to your ~/.gitconfig file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
[color "status"]
added = yellow
changed = green
untracked = cyan
This will make our specific Git messages stand out. When you see a yellow color in a git status, it means those files are added to the commit. Awesome huh?
Git Commands using Color

SSH

If you SSH in to servers occasionally at work or at home, it's nice not to have to type user@host repeatedly as this line can take extra time, and occasionally contains an IP address or URL. Instead we can give them short names like "server1" or "server2". To do this, we add to the ~/.ssh/config file.
Using Short Names
To add a shortcut we use the following lines:
1
2
3
4
5
6
Host [NAME]
HostName [IP/DOMAIN]
User [USER]
ServerAliveInterval 30
ServerAliveCountMax 120
Where the square brackets are located, you'll need to fill in your details for the server. This name will be your shortcut for it, for example "google". The remainder should contain the SSH details.
1
2
3
4
5
6
Host google
HostName google.com
User root
ServerAliveInterval 30
ServerAliveCountMax 120
Using the above example, we can now type the following into terminal to SSH into that server:
1
ssh google

ERROR LOGS

As a developer, debugging is an important part of our jobs. It's always useful to see your PHP error log for example. To do this, I usually "tail" it.
Tail is a unix command; it prints out the last 10 lines of a file. You can use the "f" option to detect if any more lines are appended to that file and following that, it will then print them out.
It can take a lot of effort to locate your PHP error log regularly, so here is a handy alias.
1
alias tphp="tail -f /Applications/MAMP/logs/php_error.log
As I use MAMP to run my local server, my PHP error log is stored under their Application files. Yours may be located elsewhere.
PHP Error Log
To escape from this command and return to your usual Terminal press CTRL + CCTRL + C is used to abort current tasks that are running and it gives control back to the user. CTRL + C comes in handy with most commands and command line programs, like tail and git.
You can set up these commands for any files, error logs are just a good example. Another good example is the Apache error log.

GENERAL ALIASES & COMMANDS

Dotfiles
Occasionally you'll come across a file that's hidden, also called a dotfile. Dotfiles are files that start with "." and are hidden in Finder. To show or hide them, add the following aliases:
1
alias showdotfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder";
1
alias hidedotfiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder";
Restart Terminal, and type showdotfiles, then to hide them again hidedotfiles. Be careful when deleting/editing dotfiles - If you delete or edit the wrong file, you can damage an application or even the operating system.
Pubkey
Another useful alias for developers could be "pubkey":
1
alias pubkey="more ~/.ssh/id_rsa.pub | pbcopy | printf '=> Public key copied to pasteboard.\n'";
This command will copy your SSH public key to the clipboard. It will come in handy when setting up SSH keys for services like Github, Beanstalk and also the setup of SSH Known Hosts.
Helpful Unix Commands:
  • cat - Display or concatenate files
  • chmod - Change the permissions on a file or directory
    Note:Be sure not to go around "chmodding" everything as permissions are very sensitive and can cause security issues if permissions are wrongly given.
  • cp - Copy a file
  • mkdir - Make a directory
  • ls - List names of files in a directory
  • man - Manual for a command: "man [COMMAND"
To find out more about the commands above, Google unix [COMMAND] or type man [COMMAND] into Terminal.

USEFUL TRICKS

!! - "BANG BANG"
After typing a command, you may want to run it again or print what you've run previously. Type !! to run the command again, or !! -p to print the command. This is great if you find out that you don't have permissions, whilst also allowing you to do the following:
1
sudo !!
This will then run the last command with sudo. This will also work with any prefix.
Mkdir
When creating directories it can be frustrating to type the following:
1
2
3
4
5
mdkir tmp
cd tmp
mkdir cache
cd cache
mkdir models
Instead you can use the -p option:
1
mkdir -p tmp/cache/models
The -p option basically allows you to create all the folders required to make that path. In the example above, the tmpfolder doesn't exist so we'll need to create both that, as well as the cache folder. Following that, we'll create the models folder.
Clear
If your terminal window is getting cluttered and you'd like to start from fresh, you can type clear. This will clear your window as if you've just reopened Terminal.
History
The history command displays the most recently used and frequent commands. It's great for finding those commands that you've lost or forgotten.
History
PWD
The pwd command shows you your location in terms of directories.

USEFUL LINKS

Conclusion

I hope that the tips and tricks in this article have given you an idea of how useful Unix can be and how easy it is to customize. There are thousands of things to do differently in Unix, so why not have a play around and see. Below you'll find some useful links for further reading.

No comments:

Post a Comment