Difference between revisions of "Core utilities"

From ArchWiki
Jump to: navigation, search
(Browsing compressed files)
m (it localization)
(48 intermediate revisions by 26 users not shown)
Line 1: Line 1:
[[Category:Utilities (English)]]
+
[[Category:System administration]]
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.
+
[[Category:Command shells]]
 +
[[zh-CN:Core Utilities]]
 +
[[it:Core Utilities]]
 +
{{Article summary start}}
 +
{{Article summary text|Tips and tricks related to so-called "core" utilities on a GNU/Linux system.}}
 +
{{Article summary heading|Related}}
 +
{{Article summary wiki|Commandline Tools}}
 +
{{Article summary wiki|General Recommendations}}
 +
{{Article summary wiki|GNU Project}}
 +
{{Article summary end}}
  
'''This page is currently under construction.'''
+
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 {{Pkg|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.
  
See also:
+
==grep==
 +
[[Wikipedia:grep|grep]] is a command line text search utility originally written for Unix. The {{ic|grep}} command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.
  
* [[Commandline Tools]]
+
Beyond aesthetics, {{ic|grep}}'s color output is immensely useful for learning {{ic|regexp}} and {{ic|grep}}'s functionality.
* [[Useful Commands]]
+
* [[General Recommendations]]
+
  
==alias==
+
To use the default colors for grep, write the following entry to {{ic|~/.bashrc}}:
[[Wikipedia:alias|alias]] is a command in various shells, such as [[Bash]], which enables a replacement of a word with another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command.
+
alias grep='grep --color=auto'
 +
Alternatively, you can set the {{ic|GREP_OPTIONS}} [[Environment Variables|environment variable]] [http://www.gnu.org/software/grep/manual/html_node/Environment-Variables.html] bearing in mind this may break some scripts that use {{ic|grep}} [http://brainstorm.ubuntu.com/idea/24141/]:
 +
export GREP_OPTIONS='--color=auto'
 +
To include file line numbers in the output, add "''-n''":
 +
alias grep='grep -n --color=auto'
  
Personal aliases are preferably stored in {{Filename|~/.bashrc}}, and system-wide aliases (which affect all users) belong in {{Filename|/etc/bash.bashrc.local}}.
+
The environment variable {{ic|GREP_COLORS}} may be used to specify different colors than the defaults.
  
An example excerpt from {{Filename|~/.bashrc}} covering several time-saving aliases:
+
==less==
{{File
+
[[Wikipedia:less (Unix)|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 {{ic|more}} and {{ic|pg}}, {{ic|less}} offers a more advanced interface and complete feature-set.[http://www.greenwoodsoftware.com/less/faq.html]
|name=~/.bashrc
+
=== Colored output through environment variables ===
|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'
+
  
# new commands
+
Add the following lines to your shell configuration file, e.g. {{ic|~/.bashrc}} if using [[Bash]]:
alias da='date "+%A, %B %d, %Y [%T]"'
+
export LESS=-R
alias du1='du --max-depth=1'
+
export LESS_TERMCAP_me=$(printf '\e[0m')
alias hist='history | grep $1'     # requires an argument
+
export LESS_TERMCAP_se=$(printf '\e[0m')
alias openports='netstat --all --numeric --programs --inet'
+
export LESS_TERMCAP_ue=$(printf '\e[0m')
alias pg='ps -Af | grep $1'         # requires an argument
+
export LESS_TERMCAP_mb=$(printf '\e[1;32m')
 +
export LESS_TERMCAP_md=$(printf '\e[1;34m')
 +
export LESS_TERMCAP_us=$(printf '\e[1;32m')
 +
export LESS_TERMCAP_so=$(printf '\e[1;44;1m')
 +
Change values as you like. References: [[Wikipedia:ANSI_escape_code#Colors]].
  
# privileged access
+
=== Colored output through wrappers ===
if [ $UID -ne 0 ]; then
+
You can enable code syntax coloring in {{ic|less}}. First, install {{Pkg|source-highlight}}, then add these lines to your {{ic|~/.bashrc}}:
    alias sudo='sudo '
+
export LESSOPEN="| /usr/bin/source-highlight-esc.sh %s"
    alias scat='sudo cat'
+
export LESS='-R '
    alias svim='sudo vim'
+
    alias root='sudo su'
+
    alias reboot='sudo reboot'
+
    alias update='sudo pacman -Su'
+
    alias netcfg='sudo netcfg2'
+
fi
+
  
# cd
+
Frequent users of the command line interface might want to install {{Pkg|lesspipe}}.
alias home='cd ~'
+
alias back='cd $OLDPWD'
+
alias cd..='cd ..'
+
alias ..='cd ..'
+
 
+
# ls
+
alias ls='ls -hF --color=always'
+
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'
+
</nowiki>}}
+
 
+
==Extracting compressed files==
+
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 e=0 c
+
  until (( $# == 0 )); do
+
    if [[ -f $1 && -r $1 ]]; then
+
        c=''
+
        case $1 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: cannot extract \`$1': Unrecognized file extension" >&2; e=1 ;;
+
        esac
+
        [[ $c ]] && command $c "$1"
+
      else
+
        echo "$0: cannot extract \`$1': File either does not exist or is unreadable" >&2; e=2
+
    fi
+
    shift
+
  done
+
  return $e
+
}
+
</nowiki>}}
+
 
+
{{note|make sure extglob is enabled: {{codeline|shopt -s extglob}}. It is enabled by default if using [[Bash#Advanced completion|Bash completion]].}}
+
 
+
==grep==
+
[[Wikipedia:grep|grep]] is a command line text search utility originally written for Unix. The {{codeline|grep}} command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.
+
 
+
===Colored output===
+
Beyond aesthetics, {{Codeline|grep}}'s color output is immensely useful for learning {{Codeline|regexp}} and {{Codeline|grep}}'s functionality.
+
 
+
To add it, write the following entry to {{Filename|~/.bashrc}}:
+
export GREP_COLOR="1;33"
+
alias grep='grep --color=auto'
+
 
+
The variable {{codeline|GREP_COLOR}} is used to specify the output color, in this example a light yellow color.
+
 
+
Although {{codeline|grep}}'s [[man page]] states that {{codeline|GREP_COLOR}} is deprecated and that {{codeline|GREP_COLORS}} is preferable, this variable does not work as of {{Package Official|grep}} version 2.5.4, so use {{codeline|GREP_COLOR}} for now.
+
 
+
==less==
+
[[Wikipedia:less (Unix)|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 {{Codeline|more}} and {{Codeline|pg}}, {{Codeline|less}} offers a more advanced interface and complete feature-set.[http://www.greenwoodsoftware.com/less/faq.html]
+
 
+
===Browsing compressed files===
+
Frequent users of the command line interface might want to install {{Package Official|lesspipe}}:
+
# pacman -S lesspipe
+
  
 
Users may now list the compressed files inside of an archive using their pager:
 
Users may now list the compressed files inside of an archive using their pager:
  
{{Command
+
{{hc
|name=$ less compressed_file.tar.gz
+
|$ less compressed_file.tar.gz
|output=<nowiki>
+
|<nowiki>
 
==> use tar_file:contained_file to view a file in the archive
 
==> use tar_file:contained_file to view a file in the archive
 
-rw------- username/group  695 2008-01-04 19:24 compressed_file/content1
 
-rw------- username/group  695 2008-01-04 19:24 compressed_file/content1
Line 137: Line 60:
 
</nowiki>}}
 
</nowiki>}}
  
{{Codeline|lesspipe}} also grants {{Codeline|less}} 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 {{Filename|html}} via {{Package Official|html2txt}}).
+
{{ic|lesspipe}} also grants {{ic|less}} 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 {{Pkg|html2text}}).
  
Re-login after installing {{Codeline|lesspipe}} in order to activate it, or source {{Filename|/etc/profile.d/lesspipe.sh}}.
+
Re-login after installing {{ic|lesspipe}} in order to activate it, or source {{ic|/etc/profile.d/lesspipe.sh}}.
 +
 
 +
=== Vim as alternative pager ===
 +
Vim has a script to view the content of text files, compressed files, binaries, directories. Add the following line to your shell configuration file, e.g. {{ic|~/.bashrc}} for Bash:
 +
alias less='/usr/share/vim/vim73/macros/less.sh'
  
 
==ls==
 
==ls==
 
[[Wikipedia:ls|ls]] is a command to list files in Unix and Unix-like operating systems.
 
[[Wikipedia:ls|ls]] is a command to list files in Unix and Unix-like operating systems.
  
===Colored output===
+
Colored output can be enabled with a simple alias. File {{ic|~/.bashrc}} should already have the following entry copied from {{ic|/etc/skel/.bashrc}}:
File {{Filename|~/.bashrc}} should already have the following entry copied from {{Filename|/etc/skel/.bashrc}}:
+
 
  alias ls='ls --color=auto'
 
  alias ls='ls --color=auto'
  
The next step will further enhance the colored {{Codeline|ls}} output; for example, broken (orphan) symlinks will start showing in a red hue. Add the following to {{Filename|~/.bashrc}} and relogin, or source the file:
+
The next step will further enhance the colored {{ic|ls}} output; for example, broken (orphan) symlinks will start showing in a red hue. Add the following to {{ic|~/.bashrc}} and relogin, or source the file:
 
  eval $(dircolors -b)
 
  eval $(dircolors -b)
 +
 +
== rm ==
 +
[[Wikipedia:rm_(Unix)|rm]] is a command to delete files and directories.
 +
It could be very dangerous so it is prudent to limit its scope:
 +
alias rm=' timeout 3 rm -Iv'
 +
This alias suspends {{ic|rm}} after three seconds, asks confirmation to delete three or more files, lists the operations in progress and does not store itself in the shell history file if the shell is configured to ignore space starting commands.
 +
 +
== Additional resources ==
 +
* [http://www.reddit.com/r/commandline/comments/19garq/a_sampling_of_coreutils_120/ A sampling of coreutils] [http://www.reddit.com/r/commandline/comments/19ge6v/a_sampling_of_coreutils_2040/ , part 2] [http://www.reddit.com/r/commandline/comments/19j1w3/a_sampling_of_coreutils_4060/ , part 3] an overview of commands in coreutils

Revision as of 19:41, 17 May 2013

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 -- but is not limited to -- those utilities included with the GNU 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.

grep

grep is a command line text search utility originally written for Unix. The grep 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, grep's color output is immensely useful for learning regexp and grep's functionality.

To use the default colors for grep, write the following entry to ~/.bashrc:

alias grep='grep --color=auto' 

Alternatively, you can set the GREP_OPTIONS environment variable [1] bearing in mind this may break some scripts that use grep [2]:

export GREP_OPTIONS='--color=auto'

To include file line numbers in the output, add "-n":

alias grep='grep -n --color=auto' 

The environment variable GREP_COLORS may be used to specify different colors than the defaults.

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 more and pg, less offers a more advanced interface and complete feature-set.[3]

Colored output through environment variables

Add the following lines to your shell configuration file, e.g. ~/.bashrc if using Bash:

export LESS=-R
export LESS_TERMCAP_me=$(printf '\e[0m')
export LESS_TERMCAP_se=$(printf '\e[0m')
export LESS_TERMCAP_ue=$(printf '\e[0m')
export LESS_TERMCAP_mb=$(printf '\e[1;32m')
export LESS_TERMCAP_md=$(printf '\e[1;34m')
export LESS_TERMCAP_us=$(printf '\e[1;32m')
export LESS_TERMCAP_so=$(printf '\e[1;44;1m')

Change values as you like. References: Wikipedia:ANSI_escape_code#Colors.

Colored output through wrappers

You can enable code syntax coloring in less. First, install source-highlight, then add these lines to your ~/.bashrc:

export LESSOPEN="| /usr/bin/source-highlight-esc.sh %s"
export LESS='-R '

Frequent users of the command line interface might want to install lesspipe.

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

$ less compressed_file.tar.gz
==> use tar_file:contained_file to view a file in the archive
-rw------- username/group  695 2008-01-04 19:24 compressed_file/content1
-rw------- username/group   43 2007-11-07 11:17 compressed_file/content2
compressed_file.tar.gz (END)

lesspipe also grants less 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 html2text).

Re-login after installing lesspipe in order to activate it, or source /etc/profile.d/lesspipe.sh.

Vim as alternative pager

Vim has a script to view the content of text files, compressed files, binaries, directories. Add the following line to your shell configuration file, e.g. ~/.bashrc for Bash:

alias less='/usr/share/vim/vim73/macros/less.sh'

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 ~/.bashrc should already have the following entry copied from /etc/skel/.bashrc:

alias ls='ls --color=auto'

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

eval $(dircolors -b)

rm

rm is a command to delete files and directories. It could be very dangerous so it is prudent to limit its scope:

alias rm=' timeout 3 rm -Iv'

This alias suspends rm after three seconds, asks confirmation to delete three or more files, lists the operations in progress and does not store itself in the shell history file if the shell is configured to ignore space starting commands.

Additional resources