Core utilities

From ArchWiki
Jump to navigation Jump to search

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




$ cat << EOF >> path/file
first line
last line
A better alternative is the echo command:
$ echo "\
first line
last line" \
>> path/file



See the main article.





Checking progress of dd while running


# killall -USR1 dd


# kill -USR1 pid_of_dd_command

For example:

# kill -USR1 $(pidof dd)


605+0 records in
605+0 records out

dd spin-offs






Colored output



alias grep='grep --color=auto'

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

export GREP_OPTIONS='--color=auto'





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



See man iconv for more 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






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


Get colored output when pipe STDOUT of a program to less

Note: It's recommend to add the above function to your ~/.bashrc or ~/.zshrc. Because this section is totally written based on this: export LESS=R

When we run a command and piping its STDOUT to less for a paged view(e.g. yaourt -Qe --date|less), you may found that the output is not colored any more. This is usually because the program try to detect the STDOUT whether is a interactive tty: if the STDOUT is to a interactive tty, then the program will print colored output, if not it prints uncolored output. This is totally a good behaviour when you want to redirect(write) the STDOUT to a file as a log(e.g. yaourt -Qe --date > pkglst-backup.txt). However, this will not be a good behaviour when you want to view the output in less. Some programs provide a option to disable the interactive tty detection, like this one:

# dmesg --color=always | less

However there are also some programs haven't provided similar option yet, in this case we need some trick on pty which can make the program believe that the STDOUT is a interactive tty. There are several method available, choose one your favourite:

1. stdoutisatty is a little C implement can invoke a 'fake interactive tty'. Before use this you need to install it (aur/stdoutisatty-gitAUR), the usage looks like this:

$ stdoutisatty yaourt -Qe --date | less

2. socat is a relay for bidirectional data transfer between two independent data channels. It based on GNU readline and available in offical repository (socat is in [extra]), the usage our needed is like:

$ socat EXEC:"yaourt -Qe --date",pty STDIO | less

3. script is an old enough command that make typescript of terminal session, the usage in this section looks like:

$ script -fqc 'yaourt -Qe --date' | less
Tip: Perhaps this helper function from stackoverflow will be useful:
function faketty {script -qfc "$(printf "'%s' " "$@")"}

4. unbuffer is a good (its man page is even longer than the program itself), old script based on sh and Tcl. It can do the trick well, unbuffer comes with expect. The usage what we need is looks like this,

$ unbuffer yaourt -Qe --date | less

5. zpty is a module of zsh, here is a tiny function from [3], put it in your ~/.zshrc:

ptyless() {
	zmodload zsh/zpty
	zpty ptyless ${1+"$@"}            # ptyless, name of pty
	zpty -r ptyless > /tmp/ptyless.$$ # Write to file, just dunno why pipeline doesn't work fine here
	less /tmp/ptyless.$$
	rm -f /tmp/ptyless.$$
	zpty -d ptyless                   # remove used pty

Usage is:

$ ptyless yaourt -Qe --date | less



See the main article.



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




mkdir (make,ectory) is a command to create directories.

alias mkdir='mkdir -p -v'
The -v switch make it verbose.



alias mv=' timeout 8 mv -iv'







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

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






# shred -v /dev/sdX




alias shred=' timeout 3 shred -v'





Permissions related utilities

  • ls -l lists files attributes.


See also