Difference between revisions of "Core utilities"

From ArchWiki
Jump to: navigation, search
m (added related page)
(2 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
{{Article summary end}}
 
{{Article summary end}}
  
This article deals with so-called "core" utilities on a GNU/Linux system, such as '''less''', '''ls''', and '''grep'''. The scope of this article includes -- ''but is not limited to'' -- those utilities included with the GNU {{Package Official|coreutils}} package. What follows are various tips and tricks and other helpful information related to these utilities. If sections grow too detailed, please split into separate articles.
+
This article deals with so-called "core" utilities on a GNU/Linux system, such as '''less''', '''ls''', and '''grep'''. The scope of this article includes those utilities included with the GNU {{Package Official|coreutils}} package. What follows are various tips and tricks and other helpful information.
 
 
==alias==
 
[[Wikipedia:alias|alias]] is a command in various shells, such as [[Bash]], which enables a replacement of a word with another string. It is often used for abbreviating a system command, or for adding default arguments to a regularly used command.
 
 
 
Personal aliases are preferably stored in {{Filename|~/.bashrc}}, and system-wide aliases (which affect all users) belong in {{Filename|/etc/bash.bashrc}}.
 
 
 
An example excerpt from {{Filename|~/.bashrc}} covering several time-saving aliases:
 
{{File
 
|name=~/.bashrc
 
|content=<nowiki>
 
# modified commands
 
alias diff='colordiff'              # requires colordiff package
 
alias grep='grep --color=auto'
 
alias more='less'
 
alias df='df -h'
 
alias du='du -c -h'
 
alias mkdir='mkdir -p -v'
 
alias nano='nano -w'
 
alias ping='ping -c 5'
 
alias ..='cd ..'
 
 
 
# new commands
 
alias da='date "+%A, %B %d, %Y [%T]"'
 
alias du1='du --max-depth=1'
 
alias hist='history | grep $1'      # requires an argument
 
alias openports='netstat --all --numeric --programs --inet --inet6'
 
alias pg='ps -Af | grep $1'        # requires an argument (note: /usr/bin/pg is installed by the util-linux package; maybe a different alias name should be used)
 
 
 
# privileged access
 
if [ $UID -ne 0 ]; then
 
    alias sudo='sudo '
 
    alias scat='sudo cat'
 
    alias svim='sudo vim'
 
    alias root='sudo su'
 
    alias reboot='sudo reboot'
 
    alias halt='sudo halt'
 
    alias update='sudo pacman -Su'
 
    alias netcfg='sudo netcfg2'
 
fi
 
 
 
# ls
 
alias ls='ls -hF --color=auto'
 
alias lr='ls -R'                    # recursive ls
 
alias ll='ls -l'
 
alias la='ll -A'
 
alias lx='ll -BX'                  # sort by extension
 
alias lz='ll -rS'                  # sort by size
 
alias lt='ll -rt'                  # sort by date
 
alias lm='la | more'
 
 
 
# safety features
 
alias cp='cp -i'
 
alias mv='mv -i'
 
alias rm='rm -I'                    # 'rm -i' prompts for every file
 
alias ln='ln -i'
 
alias chown='chown --preserve-root'
 
alias chmod='chmod --preserve-root'
 
alias chgrp='chgrp --preserve-root'
 
</nowiki>}}
 
 
 
==extract==
 
The following function will extract a wide range of compressed file types. Add the function to {{Filename|~/.bashrc}} and use it with the syntax {{Codeline|extract <file1> <file2> ...}}
 
 
 
{{File
 
|name=~/.bashrc
 
|content=<nowiki>
 
extract() {
 
    local c e i
 
 
 
    (($#)) || return
 
 
 
    for i; do
 
        c=''
 
        e=1
 
 
 
        if [[ ! -r $i ]]; then
 
            echo "$0: file is unreadable: \`$i'" >&2
 
            continue
 
        fi
 
 
 
        case $i in
 
        *.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))
 
              c='bsdtar xvf';;
 
        *.7z)  c='7z x';;
 
        *.Z)  c='uncompress';;
 
        *.bz2) c='bunzip2';;
 
        *.exe) c='cabextract';;
 
        *.gz)  c='gunzip';;
 
        *.rar) c='unrar x';;
 
        *.xz)  c='unxz';;
 
        *.zip) c='unzip';;
 
        *)    echo "$0: unrecognized file extension: \`$i'" >&2
 
              continue;;
 
        esac
 
 
 
        command $c "$i"
 
        e=$?
 
    done
 
 
 
    return $e
 
}
 
</nowiki>}}
 
 
 
{{note|[[Bash]] users should make sure extglob is enabled: {{codeline|shopt -s extglob}}. It is enabled by default if using [[Bash#Advanced completion|Bash completion]]. [[Zsh]] users should do: {{codeline|setopt kshglob}} instead.}}
 
 
 
Another way to do this is to install ''unp'' package.
 
  
 
==grep==
 
==grep==

Revision as of 23:57, 11 October 2011

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

This article deals with so-called "core" utilities on a GNU/Linux system, such as less, ls, and grep. The scope of this article includes those utilities included with the GNU Template:Package Official package. What follows are various tips and tricks and other helpful information.

grep

grep is a command line text search utility originally written for Unix. The Template:Codeline command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.

Beyond aesthetics, Template:Codeline's color output is immensely useful for learning Template:Codeline and Template:Codeline's functionality.

To add it, write the following entry to Template:Filename:

export GREP_COLOR="1;33"
alias grep='grep --color=auto'

The variable Template:Codeline is used to specify the output color, in this example a light yellow color.

Although Template:Codeline's man page states that Template:Codeline is deprecated and that Template:Codeline is preferable, this variable does not work as of Template:Package Official version 2.5.4, so use Template:Codeline for now.

less

less is a terminal pager program used to view the contents of a text file one screen at a time. Whilst similar to other pages such as Template:Codeline and Template:Codeline, Template:Codeline offers a more advanced interface and complete feature-set.[1]

Enable code syntax coloring by adding this line to .bashrc:

export LESS="-R"

Frequent users of the command line interface might want to install Template:Package Official:

# pacman -S lesspipe

Users may now list the compressed files inside of an archive using their pager:

Template:Command

Template:Codeline also grants Template:Codeline the ability of interfacing with files other than archives; serving as an alternative for the specific command associated for that file-type (such as viewing HTML via Template:Package Official).

Re-login after installing Template:Codeline in order to activate it, or source Template:Filename.

ls

ls is a command to list files in Unix and Unix-like operating systems.

Colored output can be enabled with a simple alias. File Template:Filename should already have the following entry copied from Template:Filename:

alias ls='ls --color=auto'

The next step will further enhance the colored Template:Codeline output; for example, broken (orphan) symlinks will start showing in a red hue. Add the following to Template:Filename and relogin, or source the file:

eval $(dircolors -b)