Friday, August 21, 2009

ls command


Shells
Most Linux distributions use the bash shell while the BSDs use tcsh, the bourne shell is only used for scripts. Filters are very useful and can be piped:

* grep Pattern matching
* sed Search and Replace strings or characters
* cut Print specific columns from a marker
* sort Sort alphabetically or numerically
* uniq Remove duplicate lines from a file

For example used all at once:

# ifconfig | sed 's/ / /g' | cut -d" " -f1 | uniq | grep -E "[a-z0-9]+" | sort -r
# ifconfig | sed '/.*inet addr:/!d;s///;s/ .*//'|sort -t. -k1,1n -k2,2n -k3,3n -k4,4n

The first character in the sed pattern is a tab. To write a tab on the console, use ctrl-v ctrl-tab.
bash
Redirects and pipes for bash and sh:

# cmd 1> file # Redirect stdout to file.
# cmd 2> file # Redirect stderr to file.
# cmd 1>> file # Redirect and append stdout to file.
# cmd &> file # Redirect both stdout and stderr to file.
# cmd >file 2>&1 # Redirects stderr to stdout and then to file.
# cmd1 | cmd2 # pipe stdout to cmd2
# cmd1 2>&1 | cmd2 # pipe stdout and stderr to cmd2

Modify your configuration in ~/.bashrc (it can also be ~/.bash_profile). The following entries are useful, reload with ". .bashrc".

# in .bashrc
bind '"\e[A"':history-search-backward # Use up and down arrow to search
bind '"\e[B"':history-search-forward # the history. Invaluable!
set -o emacs # Set emacs mode in bash (see below)
set bell-style visible # Do not beep, inverse colors
# Set a nice prompt like [user@host]/path/todir>
PS1="\[\033[1;30m\][\[\033[1;34m\]\u\[\033[1;30m\]"
PS1="$PS1@\[\033[0;33m\]\h\[\033[1;30m\]]\[\033[0;37m\]"
PS1="$PS1\w\[\033[1;30m\]>\[\033[0m\]"

# To check the currently active aliases, simply type alias
alias ls='ls -aF' # Append indicator (one of */=>@|)
alias ll='ls -aFls' # Listing
alias la='ls -all'
alias ..='cd ..'
alias ...='cd ../..'
export HISTFILESIZE=5000 # Larger history
export CLICOLOR=1 # Use colors (if possible)
export LSCOLORS=ExGxFxdxCxDxDxBxBxExEx

tcsh
Redirects and pipes for tcsh and csh (simple > and >> are the same as sh):

# cmd >& file # Redirect both stdout and stderr to file.
# cmd >>& file # Append both stdout and stderr to file.
# cmd1 | cmd2 # pipe stdout to cmd2
# cmd1 |& cmd2 # pipe stdout and stderr to cmd2

The settings for csh/tcsh are set in ~/.cshrc, reload with "source .cshrc". Examples:

# in .cshrc
alias ls 'ls -aF'
alias ll 'ls -aFls'
alias la 'ls -all'
alias .. 'cd ..'
alias ... 'cd ../..'
set prompt = "%B%n%b@%B%m%b%/> " # like user@host/path/todir>
set history = 5000
set savehist = ( 6000 merge )
set autolist # Report possible completions with tab
set visiblebell # Do not beep, inverse colors

# Bindkey and colors
bindkey -e Select Emacs bindings # Use emacs keys to edit the command prompt
bindkey -k up history-search-backward # Use up and down arrow to search
bindkey -k down history-search-forward
setenv CLICOLOR 1 # Use colors (if possible)
setenv LSCOLORS ExGxFxdxCxDxDxBxBxExEx

The emacs mode enables to use the emacs keys shortcuts to modify the command prompt line. This is extremely useful (not only for emacs users). The most used commands are:

* C-a Move cursor to beginning of line
* C-e Move cursor to end of line
* M-b Move cursor back one word
* M-f Move cursor forward one word
* M-d Cut the next word
* C-w Cut the last word
* C-u Cut everything before the cursor
* C-k Cut everything after the cursor (rest of the line)
* C-y Paste the last thing to be cut (simply paste)
* C-_ Undo

Note: C- = hold control, M- = hold meta (which is usually the alt or escape key).

LSCOLORS The value of this variable describes what color to use
for which attribute when colors are enabled with
CLICOLOR. This string is a concatenation of pairs of the
format fb, where f is the foreground color and b is the
background color.

The color designators are as follows:

a black
b red
c green
d brown
e blue
f magenta
g cyan
h light grey
A bold black, usually shows up as dark grey
B bold red
C bold green
D bold brown, usually shows up as yellow
E bold blue
F bold magenta
G bold cyan
H bold light grey; looks like bright white
x default foreground or background

Note that the above are standard ANSI colors. The actual
display may differ depending on the color capabilities of
the terminal in use.

The order of the attributes are as follows:

1. directory
2. symbolic link
3. socket
4. pipe
5. executable
6. block special
7. character special
8. executable with setuid bit set
9. executable with setgid bit set
10. directory writable to others, with sticky bit
11. directory writable to others, without sticky
bit

The default is "exfxcxdxbxegedabagacad", i.e. blue fore-
ground and default background for regular directories,
black foreground and red background for setuid executa-
bles, etc.

No comments: