Docfuntional

Documentation by Dysphunkional

Command Line Tips

Here are some general tips for working with command line in FreeBSD. Most of them will also apply to BSD in general too.

Tab Completion

Most command line shells support completing the names of commands or directory paths by pressing the tab key on the keyboard. If there is only one possibility then tab will fill it in. If there are more than one you can press tab twice and it will show you a list of possibilities and then you can type the next couple characters and then try tab again. This works on practically every UNIX derived shell like bash and tcsh. Tab completion also works in the command prompt on most newer versions of Windows and in PowerShell. Exactly what can be completed by tab varies between shell to shell and OS to OS so just try it and see if it works.

Working with ncurses

A lot of command line tools will give you a GUI style menu system called ncurses that will look something like this.
PHP5 install options
Most terminals don’t work with a mouse so you will have to use your keyboard to navigate. To move around in a list use the up and down arrow keys. To check and uncheck the boxes press the spacebar. To switch between the options on the bottom press the right or left arrow keys or use the tab key. To select one of the bottom options press the Enter key.

See what Network Ports are in Use

sockstat -l

The Ports Collection

If you need to install software from source you will need to have the ports collection installed. It can be installed by running:

portsnap fetch extract

If you already have ports installed you can update it by running:

portsnap fetch update

Installing Software

There are three main ways to install software on BSD systems. The old package system, the new and improved package system and from source using the port collection. You can only have one of the two package systems in a jail at a time. You can upgrade from the old to new systems by using the pkg2ng command but you cannot downgrade from the new to the old. By default jails of the plugin type uses the new system and jails of the port or standard type use the old system. If you want a new jail of the port or standard type to use the new system uncheck the vanilla box when creating the jail.
To install a package with one of the package management systems you need to know the name of the package and to install a package from source you need to know the name of the package and in which category it is in. You can find that out by going to www.freshports.org and searching for the package you want to install. If you have just installed a command and you are using the tcsh shell run the following command so that you can use it right away:

rehash

Old Package System

Install a package

pkg_add -r packagename

Uninstall a package

pkg_delete packagename

List installed packages

pkg_info

New Package System

Install a package

pkg install packagename

Uninstall a package

pkg remove packagename

List installed packages

pkg info

Search for packages

pkg search searchterm

Update the package repository

pkg update

Ports Collection

Installing a package from source means that your computer needs to get the source code for all the programs and then compile them and all the dependencies. This can take a while but you can choose more options than you can for precompiled programs (known as binaries) and the resulting programs will be optimized for your hardware. In general you would do run the commands below and then follow the prompts.
Install from source

cd /usr/ports/category/packagename/
make install clean

Portmaster

If you are going to be installing from source portmaster can make things a lot easier.

cd /usr/ports/ports-mgmt/portmaster/
make install clean

Select the boxes for bash and/or zsh if you use one of those shells.

Useful Software

Here are a few programs I like to install in any Jail

Nano

I prefer the Nano text editor to vi since it is much easier to use. The most common commands for it are listed at the bottom of the screen with their keyboard shortcuts (the ^ means the CTRL key on the keyboard). Use one of the package management systems to install nano. You can set nano to be your default editor by adding it to your profile.

echo 'EDITOR=nano' >> ~/.profile
echo 'export EDITOR' >> ~/.profile

This will take effect the next time you log in.

Nano tips

Adding the w and c command line switches will make nano aware of changes to the size of the window you are viewing it with so lines that used to be cut off will fill in when you make the window bigger.

Bash

Bash is a powerful command line shell with better tab completion than most other shells once you install the bash-completion package. Both package managers let you install more than one package at time by separating the package names with spaces like this:

pkg install bash bash-completion

To get tab completion to work right away run:

source /usr/local/share/bash-completion/bash_completion.sh

Create configuration files for bash

These config files will give you a coloured prompt and set up tab completion.
First make a dummy .bash_profile that will run .bashrc

nano -wc ~/.bash_profile

Now copy this info into the file and save and close it by pressing CTRL+X and then pressing the Y key when prompted to save your changes and ENTER when asked for the filename.

# .bash_profile

if [ -f ~/.bashrc ]; then

. ~/.bashrc

fi

# User specific environment and startup programs

Now edit the .bashrc file

nano –wc ~/.bashrc

Copy the following into the file and save it.

export CLICOLOR="YES"

bash_prompt_command() {
        # How many characters of the $PWD should be kept
        local pwdmaxlen=25
        # Indicate that there has been dir truncation
        local trunc_symbol=".."
        local dir=${PWD##*/}
        pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
        NEW_PWD=${PWD/#$HOME/\~}
        local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
        if [ ${pwdoffset} -gt "0" ]
        then
            NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
            NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
        fi
}

bash_prompt() {
        local NONE="\[\033[0m\]"    # unsets color to term's fg color

        # regular colors
        local K="\[\033[0;30m\]"    # black
        local R="\[\033[0;31m\]"    # red
        local G="\[\033[0;32m\]"    # green
        local Y="\[\033[0;33m\]"    # yellow
        local B="\[\033[0;34m\]"    # blue
        local M="\[\033[0;35m\]"    # magenta
        local C="\[\033[0;36m\]"    # cyan
        local W="\[\033[0;37m\]"    # white

        # empahsized (bolded) colors
        local EMK="\[\033[1;30m\]"
        local EMR="\[\033[1;31m\]"
        local EMG="\[\033[1;32m\]"
        local EMY="\[\033[1;33m\]"
        local EMB="\[\033[1;34m\]"
        local EMM="\[\033[1;35m\]"
        local EMC="\[\033[1;36m\]"
        local EMW="\[\033[1;37m\]"

        local UC=$EMG                 # user's color
        [ $UID -eq "0" ] && UC=$EMR   # root's color

        PS1="${UC}\u${EMB}@${EMM}\h ${C}\@ ${EMW}\${NEW_PWD} ${UC}\\$ ${NONE}"
}

PROMPT_COMMAND=bash_prompt_command
bash_prompt
unset bash_prompt

alias ls='ls -FGlh'
alias grep='grep --color=auto'
alias nano='nano -wc'

[[ $PS1 && -f /usr/local/share/bash-completion/bash_completion.sh ]] && \
source /usr/local/share/bash-completion/bash_completion.sh

tcsh customization

Here is my ~/.cshrc file with the changes from the default highlighted:

# $FreeBSD: release/9.1.0/etc/root/dot.cshrc 242850 2012-11-10 06:05:04Z eadler $
#
# .cshrc - csh resource script, read at beginning of execution by each shell
#
# see also csh(1), environ(7).
# more examples available at /usr/share/examples/csh/
#

alias h         history 25
alias j         jobs -l
alias nano      nano -wc
alias grep      grep --color=auto
alias ls        ls -lAFGh
alias la        ls -aF
alias lf        ls -FA
alias ll        ls -lAF

# A righteous umask
umask 22

set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin)

setenv  EDITOR  nano
setenv  CLICOLOR        YES
setenv  PAGER   more
setenv  BLOCKSIZE       K

if ($?prompt) then
        # An interactive shell -- set some stuff up
        if ($uid == 0) then
                set user = root
        endif
        set prompt = "[%B%n@%m %~]%# "
        set promptchars = "%#"

        set filec
        set history = 1000
        set savehist = (1000 merge)
        set autolist
        set autocorrect = ambiguos
        set complete = enhance
        set correct = cmd
        # Use history to aid expansion
        set autoexpand
        set autorehash
        set mail = (/var/mail/$USER)
        if ( $?tcsh ) then
                bindkey "^R" i-search-back
                bindkey "^W" backward-delete-word
                bindkey -k up history-search-backward
                bindkey -k down history-search-forward
        endif

endif
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: