Linux Tips

From miki
Jump to navigation Jump to search

Disable Auto-Mount

You can temporarily disable automount by doing as root [1]:

$ rcdbus stop

Fast Copy

Using Netcat

When copying a directory that contains a lot of small files (like 1000+ files <1kB), it is faster to actually generate a tarball of the directory and have it unpacked on the fly on the destination server [2]:

# On DESTINATION box, launch first the listener
nc -l -p 2342 | tar -C /target/dir -xzf -
#On SOURCE box, launch the sender
tar -cz /source/dir | nc -w 3 Target_Box 2342

Using tar

Say you want to copy from directory /mnt/img to /mnt/usb

(cd /mnt/img ; tar -cf - *) | (cd /mnt/usb ; tar -xvf -)
sync                                                         # Flush file system buffers

Other

See here

Fast Search

Locate

! Power Tip ! Use locate to find files rapidly by their names. locate is very lightweight (hardly notice it's running) and very efficient. It works for any file in any local file system.

Recoll, Beagle, Google Desktop...

See Linux Software for more powerful search tool (searching in file content).

ag - the Silver Searcher

ag is a much faster alternative to grep (in fact a faster ack). See Linux Commands.

rg - ripgrep

rg is like Ag, but even faster. See Linux Commands.

ug - ugrep

ug is like rg, but even faster in some benchmark (however on my machine, ripgrep is consistenly 2x faster than ug built from source). See Linux Commands.

fd

fd is a simpler find. See Linux Commands.

Password prompt during boot

This is a tip for GDM but similar tip works for KDM. It prompts for a password needed to mount a password protected HOME partition [3].

Add at the end of /etc/gdm/Init/Default:

DEVICE=/dev/sda6
while [ ! -e /dev/mapper/cryptohome ]; do
zenity --entry --hide-text --text="Enter your passphrase:" | cryptsetup luksOpen $DEVICE cryptohome
done
mount /dev/mapper/cryptohome /home

Open File Manager at Current Directory from Terminal

Source [4]

xdg-open .               # Works for any desktop
gnome-open .             # In Gnome
nautilus .               # Open Nautilus specifically

Fix 'Operation not permitted' when removing (rm) a file as root

We try to remove a file as root but get an Operation not permitted error. How is that possible?

ls -l
# -rw-r--r-- 1 root root 144 Mar 14 07:33 extlinux.conf
# -r-------- 1 root root 60K Mar 14 07:06 ldlinux.sys
rm ldlinux.sys
# rm: cannot remove ‘ldlinux.sys’: Operation not permitted

Two possibilities:

  • The immutable file attribute is set
  • The filesystem is mounted read-only.

In the latter case, just mount the filesystem with write access. In the former case, use chattr:

lsattr ldlinux.sys
# ----i----------- ldlinux.sys
chattr -i ldlinux.sys
rm ldlinux.sys

Shell tips

Linux documentation

man hier              # Documentation on filesystem, /bin, /etc, /usr...
man builtins          # bash built-in commands (part of man bash)
man ascii

Color in Shell

Some tips to provide a more colorful shell experience.

  • cat with color syntax highlighting
  • Colored diff colordiff
  • Color with git
  • Preserve color with less
  • Colors in man pages (using either less or most as pager)
  • Vim-based color viewer view

cat with highlighting

The examples below create a script ccat, similar to cat but with syntax highlighting.

Using source-highlight [5]

Define a script ccat:

#!/bin/bash
src-hilite-lesspipe.sh $1

The included less script is also nice. Add to your .bashrc:

export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=FRX                          # At least R!

Some examples

ccat file.c
ccat file.c | less -FRX
Using highlight [6]

highlight comes with package highlight. Example of script:

#!/bin/bash
highlight -O ANSI "$1"

Note that highlight can produce output in many different format (e.g. html)

Alternatives

On Ubuntu:

colordiff

colordiff is a Perl wrapper around diff to show diff outputs in color:

colordiff file1 file2                     # Diff in colors
alias diff="colordiff -W ${COLUMNS:-130}" # Handy alias that also adds auto-width

Note that colordiff output can be piped through less, or colordiff can also take a diff as standard input:

colordiff file1 file2 | less -FSRX        # Page output
<patchfile colordiff                      # Colorize stdin

git

See Git (configuration section).

less

Use option -R to preserve ANSI color escape sequence in input:

colordiff file1 file2 | less -R       # As command-line option
export LESS=R                         # Better use env. var $LESS
colordiff file1 file2 | less          # ... no need to give -R anymore
export LESS=FRX                       # Even better
colordiff file1 file2 | less          # ... quit asap if less than 1-page

In git, less is called with the following option:

export lESS=FSRX                      # Default git options
                                      # ... F - quit if less than 1-page
                                      # ... S - truncate long lines
                                      # ... R - preserve color
                                      # ... X - don't initialize term (no clear)

man

To have colors in manpages, the simplest is to install the package most:

sudo apt-get install most

And then tell how you want to use it:

# SYSTEM-WIDE
sudo update-alternatives --config pager      # To select 'most' as default pager system-wide
# per USER - in ~/.bashrc
export PAGER=most                            # To select 'most' as default pager for that user
alias man="PAGER=most man"                   # To select 'most' only for manpages

The drawback is that most does not support the same keyboard shortcuts as less. If you still want colors in your man page, you can get the same effect with less thanks to some bash script-fu magic (see [7])

mkdir ~/.terminfo/
cd ~/.terminfo
wget http://nion.modprobe.de/mostlike.txt   # Get the new terminfo description
tic mostlike.txt                            # Compile it using terminfo entry-description compiler

Now just define the following alias in ~/.bashrc:

alias man="TERMINFO=~/.terminfo TERM=mostlike PAGER=LESS LESS=C man"     # LESS=C can be skipped or adapted

Unfortunately the supplied terminfo does not support scrolling the pages with the mousewheel as it used to. Another alternative is to give less some explicit termcap commands (see [8]):

export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;37m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'

view

view is vim front-end document reader. view can highlight syntax of files based on their filename, but also based on their content (unlike source-highlight). Use view - to view standard input:

view file.c                          # Detect .c format from filename
git diff HEAD^ | view -              # Git - Will detect diff format from content
svn diff | view -                    # Svn - idem

Pager

Manpages
  • By default uses pager -s
    • /usr/bin/pager is a symlink to /etc/alternatives/pager.
  • Overridden by env var $PAGER.
  • Overridden by env var $MANPAGER.
  • Overridden by cli option -P pager.

Config / Tuning

Vivid

Vivid is a generator for the LS_COLORS environment variable that controls the colorized output of ls, tree, fd, bfs, dust and many other tools.

Usage:

export LS_COLORS="$(vivid generate molokai)"

Exa

exa is a modern replacement for the file listing utility ls

exa --long --tree --level=3