Core utilities

From ArchWiki
Revision as of 12:33, 10 September 2014 by Alad (talk | contribs) (move cron to related articles)
Jump to navigation Jump to search

zh-CN:Core Utilities zh-TW:Core Utilities




  • cat is able to work with multiple lines, although this is sometimes regarded as bad practice:
$ cat << EOF >> path/file
first line
last line
A better alternative is the echo command:
$ echo "\
first line
last line" \
>> path/file


dd is a command on Unix and Unix-like operating systems whose primary purpose is to convert and copy a file.

Note: cp does the same as dd without any operands but is not designed for more versatile disk wiping procedures.

Checking progress of dd while running

By default, there is no output of dd until the task has finished. With kill and the USR1 signal you can force status output without actually killing the program. Open up a second root terminal and issue the following command:

# killall -USR1 dd
Note: This will affect all other running dd processes as well.


# kill -USR1 pid_of_dd_command

For example:

# kill -USR1 $(pidof dd)

This causes dd to output immediate progress into the terminal. For example:

605+0 records in
605+0 records out

Using pipe viewer

As an alternative you can use pv to monitor the dd-pipeline:


To use the pipe viewer more easily you can add this to your bashrc or zshrc:

copy() {
    size=$(stat -c%s $1)
    dd if=$1 &> /dev/null | pv -petrb -s $size | dd of=$2

And use it with:

# copy /source/file /destination/file

dd spin-offs

Other dd-like programs feature periodical status output, e.g. a simple progress bar.

dcfldd is an enhanced version of dd with features useful for forensics and security. It accepts most of dd's parameters and includes status output. The last stable version of dcfldd was released on December 19, 2006.[1]
GNU ddrescue is a data recovery tool. It's capable of ignoring read errors, which is a useless feature for disk wiping in almost any case. See the official manual for details.




Colored output

grep's color output can be helpful for learning regexp and and addtional grep functionality.

To enable grep coloring write the following entry to the shell configuration file (e.g. if using Bash):

alias grep='grep --color=auto'

To include file line numbers in the output, add the option -n to the line.

The environment variable GREP_COLOR can be used to define the default highlight color (the default is red). To change the color find the ANSI escape sequence for the color liked and add it:

export GREP_COLOR="1;32"

GREP_COLORS may be used to define specific searches.

Standard error

Some commands send their output to standard error, and grep has no apparent effect. In this case, redirect standard error next to standard out:

$ command 2>&1 | grep args

or Bash 4 shorthand:

$ command |& grep args

See also I/O Redirection.


iconv converts the encoding of characters from one codeset to another.



See man iconv for more details.

Convert a file in place



See man sponge for details.



Object Purpose Manual Page Name
ip addr protocol address management ip-address
ip addrlabel protocol address label management ip-addrlabel
ip l2tp tunnel Ethernet over IP (L2TPv3) ip-l2tp
ip link network device configuration ip-link
ip maddr multicast addresses management ip-maddress
ip monitor watch for netlink messages ip-monitor
ip mroute multicast routing cache management ip-mroute
ip mrule rule in multicast routing policy db
ip neigh neighbour/ARP tables management ip-neighbour
ip netns process network namespace management ip-netns
ip ntable neighbour table configuration ip-ntable
ip route routing table management ip-route
ip rule routing policy database management ip-rule
ip tcp_metrics management for TCP Metrics ip-tcp_metrics
ip tunnel tunnel configuration ip-tunnel
ip tuntap manage TUN/TAP devices
ip xfrm manage IPsec policies ip-xfrm






See List of applications#Terminal pagers for alternatives.

Colored output through environment variables

Add the following lines to your shell configuration file:

export LESS=-R

Change values as you like. References: ANSI escape code.

Colored output through wrappers


export LESS='-R '



$ less compressed_file.tar.gz
==> use tar_file:contained_file to view a file in the archive
compressed_file.tar.gz (END)



Vim as alternative pager




export PAGER='vimpager'
alias less=$PAGER


Colored output when reading from stdin

Note: It is recommend to add #Colored output through environment variables to your ~/.bashrc or ~/.zshrc, as the below is based on export LESS=R

When you run a command and pipe its standard output (stdout) to less for a paged view (e.g. pacman -Qe | less), you may find that the output is no longer colored. This is usually because the program tries to detect if its stdout is an interactive terminal, in which case it prints colored text, and otherwise prints uncolored text. This is good behaviour when you want to redirect stdout to a file, e.g. pacman -Qe > pkglst-backup.txt, but less suited when you want to view output in less.

Some programs provide an option to disable the interactive tty detection:

# dmesg --color=always | less

In case that the program does not provide any similar option, it is possible to trick the program to believe that its stdout is an interactive terminal with the following utilities:

  • stdoutisatty — A small program which invokes a "fake interactive tty". || stdoutisattyAUR
Example: stdoutisatty program | less
  • socat — A relay for bidirectional data transfer between two independent data channels. It is based on GNU readline. || socat
Example: socat EXEC:"program",pty STDIO | less
  • script — An utility that makes typescript of a terminal session. || util-linux
Example: script -fqc "program" | less or [2]
  • unbuffer — A script based on sh and Tcl. || expect
Example: unbuffer program | less

Alternatively, using zpty module from zsh: [3]

zmodload zsh/zpty

pty() {
	zpty pty-${UID} ${1+"$@"}
	if [[ ! -t 1 ]];then
		setopt local_traps
		trap '' INT
		zpty -r pty-${UID}
		zpty -r pty-${UID}
	zpty -d pty-${UID}

ptyless() {
	pty $@ | less


$ ptyless program

To pipe it to other pager(less in this example):

$ pty program | less



See the main article.



alias ls='ls --color=auto'
eval $(dircolors -b)




mkdir (make directory) is a command to create directories.

alias mkdir='mkdir -p -v'
The -v switch make it verbose.
Tip: If you just want a temporary directory, a better alternative may be mktemp (make termporary): mktemp -p.



alias mv=' timeout 8 mv -iv'




  • It can be very dangerous, so it is prudent to limit its scope:



Here is a handy list of sed one-liners examples.

Tip: More powerful alternatives are AWK and even Perl language.




shred is a Unix command that can be used to securely delete files and devices so that they can be recovered only with great difficulty with specialised hardware, if at all. shred uses three passes, writing pseudo-random data to the device during each pass. This can be reduced or increased.

The following command invokes shred with its default settings and displays the progress.

# shred -v /dev/sdX

Alternatively, shred can be instructed to do only one pass, with entropy from e.g. /dev/urandom.

# shred --verbose --random-source=/dev/urandom -n1 /dev/sdX

shred can be very dangerous so it is prudent to limit its scope:

alias shred=' timeout 3 shred -v'



Permissions related utilities

  • ls -l lists files attributes.

These utilities are explained in the File permissions and attributes article. More advanced permission use cases are satisfied by capabilities and ACL.

See also