Linux tricks

View source | View history | Atom feed for this file

Changing the Default Editor

To change the default editor on Debian, run:

$ sudo update-alternatives --config editor

On other systems, the $EDITOR variable can be set:

$ echo $EDITOR

$ which vim
/usr/bin/vim
$ export EDITOR=/usr/bin/vim
$ echo $EDITOR
/usr/bin/vim

Convert from HTML to Plain Text

To convert from HTML to plain text, just type:

$ cat filename.html | sed 's/<[^>]*>//g' | tr -s '\n'

Wget (Recursive Download)

Recursively download all text files to the current directory:

$ wget --no-directories --no-host-directories --recursive --no-parent --accept txt ⟨URL⟩

Or equivalently:

$ wget -nd -nH -r -np -A txt ⟨URL⟩

I’ve written a longer article on how to use wget at archiving websites on Unix-like systems.

Disable Bluetooth

To disable Bluetooth, use rfkill.

On Debian, the package is called rfkill. Once the package is installed, Bluetooth may be disabled by running:

$ sudo rfkill block bluetooth

This command may also be placed in /etc/rc.local (or an alternative startup script) so that the command executes each time at boot. For example:

$ which rfkill
/usr/sbin/rfkill
$ sudo vim /etc/rc.local
(add the following line before 'exit 0')
/usr/sbin/rfkill block bluetooth
$ sudo reboot # to see if it works

Running which rfkill is important because rfkill may be located elsewhere on your system. Using the absolute path is recommended to avoid confusion.

Source.

Hard Disk Drive Formatting (command line)

Partitioning through the command line is split into two steps:

  1. partitioning the drive (with fdisk), and
  2. creating the file system (with mkfs).
$ man fdisk # read this before blindly following
$ sudo fdisk -l /dev/⟨xyz⟩ # this lists the current file systems on this drive; make sure you change ⟨xyz⟩ for your drive
$ sudo fdisk /dev/⟨xyz⟩
(type these into fdisk's prompt:)
m
d
n
defaults
w
$ man mkfs # read this too
$ sudo mkfs /dev/⟨xyzn⟩ # n is the partition number; most likely 1.
$ sudo fsck
$ sudo vim /etc/fstab
(add the following line to mount the disk at boot)
/dev/xyzn /mnt/point auto defaults 0 0

If you have it, gparted also works well.

Formatting Text

You can use a pager like less, but if you want a better output, you can try the following commands. (You can even pipe the output to less!)

Using fold

$ cat ⟨filename⟩ | fold --spaces --width=72 | sed 's/\(.\)/    \1/' # four spaces

The --spaces option makes the text split on spaces so words will not be cut off at the edge of the screen. --width=72 allows for 8 characters of margin space; we will use 4 on each side.

Original text using cat or less:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostr
ud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis au
te irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat 
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qu
i officia deserunt mollit anim id est laborum.

After using the above command (note that words are no longer cut off):

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do 
    eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad 
    minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip 
    ex ea commodo consequat. Duis aute irure dolor in reprehenderit in 
    voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur 
    sint occaecat cupidatat non proident, sunt in culpa qui officia 
    deserunt mollit anim id est laborum.

Using par

If you have par you can use it instead:

$ cat ⟨filename⟩ | par -j -w 72 | sed 's/\(.\)/    \1/' # four spaces again

The -j option justifies the text, and -w 72 sets the width to 72 characters.

After using par:

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt  ut labore et  dolore magna  aliqua. Ut enim  ad minim
    veniam, quis nostrud exercitation ullamco  laboris nisi ut aliquip ex ea
    commodo consequat. Duis  aute irure dolor in  reprehenderit in voluptate
    velit  esse  cillum dolore  eu  fugiat  nulla pariatur.  Excepteur  sint
    occaecat  cupidatat non  proident, sunt  in culpa  qui officia  deserunt
    mollit anim id est laborum.

How to Tell If You’re in a Git Directory

$ git branch >/dev/null 2>/dev/null || echo not git; git branch >/dev/null 2>/dev/null && echo git

Debian Networking

To start a network in Debian, run:

$ sudo dhclient eth0

Configure WiFi (command line)

For more information, please see the Arch Linux Wiki.

# /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

# Custom setting below
auto wlan0
iface wlan0 inet dhcp
wpa-ssid ⟨NETWORKNAME⟩
wpa-psk ⟨PASSWORD⟩

Scan for WiFi Networks

$ iwlist wlan0 scan

Sound Card Configuration

Here is how to change the default sound card on Debian 6 “Squeeze”:

$ echo 'options snd slots=snd-emu10k1,snd-via82xx' | sudo tee -a /etc/modprobe.d/alsa-base.conf

Or, more generically:

$ echo 'options snd slots=snd-⟨NEW_DEFAULT⟩,snd-⟨OLD_DEFAULT⟩' | sudo tee -a ⟨/path/to/configuration/file⟩

Try alsaconf if you don’t know which sound card you have.

Source

ww.sh

ww.sh is a small shell script that can be used to keep a log.

Here’s the script. The script depends on Vim and Bash:

#!/bin/bash
echo 'Title:'
read x
y="$(date "+%d %b %Y") @ $(date "+%H:%M:%S"): $x"
echo -e "\n$y\n" >> ~/⟨some text file⟩
vim + ~/⟨some text file⟩

Make sure you modify permissions to allow execution:

$ chmod u+x ww.sh

Run the script by typing:

$ ./ww.sh

You can also alias the scipt to allow quick access:

$ alias ww='⟨/path/to/ww.sh⟩'

OpenBSD Configuration Files

rc.local:

wsconsctl keyboard.map+="keysym Caps_Lock = Escape"

.xinitrc:

exec xmodmap ~/.disable_caps_lock_x &
exec openbox-session

.profile:

# $OpenBSD: dot.profile,v 1.4 2005/02/16 06:56:57 matthieu Exp $
#
# sh/ksh initialization

PATH=$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/games:.
export PATH HOME TERM
export PKG_PATH=ftp://ftp.openbsd.org/pub/OpenBSD/4.9/packages/i386/
export ENV=$HOME/.kshrc

.disable_caps_lock
remove Lock = Caps_Lock
keysym Caps_Lock = Escape

Change urxvt Font

$ urxvt -fn "xft:Terminus:pixelsize=15"

Configure Mutt

Moved to Mutt.

Other commandline notes

fbterm --font-names="Source Code Pro" --font-size=14
alias fb='fbterm --font-names="Source Code Pro" --font-size=14'

See here and here for getting Solarized to work with Gnome Terminal. also cf. this post

Updating Flash player (Debian):

sudo update-flashplugin-nonfree --install

Fixing permissions

Changing swappiness

On Debian/Ubuntu/Linux Mint:

# check swappiness value
$ cat /proc/sys/vm/swappiness 
60
# change to a lower value, which means RAM will more aggressively be
# used
$ sudo sysctl vm.swappiness=10
$ cat /proc/sys/vm/swappiness 
10

See also here for how to change it permanently by editing /etc/sysctl.conf to include the line vm.swappiness=10.

Check font aliases

$ fc-match "Charter"
DejaVuSans.ttf: "DejaVu Sans" "Book"
$ fc-match "Arial"
Arial.ttf: "Arial" "Normal"

Also try fc-list for the exact locations.

Audio glitches/stuttering/repeating

Upgrading on Ubuntu/Linux Mint caused odd audio glitches while playing music. As is often the case, the Arch Wiki was most helpful. Open /etc/pulse/default.pa and add the following line at the end:

load-module module-udev-detect tsched=0

The following might work:

$ pulseaudio -k
$ pulseaudio --start

On my machine, Pulseaudio failed to restart after being killed, but rebooting fixed one of the problems, namely the stuttering. Now I get strange popping sounds once in a while, especially when both MOC and Facebook try to produce a sound simultaneously. I added the line

options snd-hda-intel vid=8086 pid=8ca0 snoop=0

to /etc/modprobe.d/sound.conf (as in the Arch Wiki, which was originally empty), but that didn’t seem to fix it. Seeing this thread, I have now placed the same line in /etc/modprobe.d/alsa-base.conf (which was not empty).

Note though that while the stuttering was so annoying as to make the computer unusable (since I like listening to music while I work), the occasional popping is much less annoying, and I can probably live with it for now.

It might also possibly be some sort of power saving option kicking in. See here if nothing works.

Use less like tail -f

Instead of

tail -f filename

to continuously monitor the end of a file, it is possible to issue

less +F filename

One can then hit Ctrl-c to return to regular less, and hit F to return to the tail -f mode again.

tee /dev/tty

Piping output through tee /dev/tty allows you to print to stdout as well as pass it along to another process. I find it useful to pass output to xclip while also seeing what was copied. For instance

$ cat somefile | tee /dev/tty | xclip -sel clip

allows you to copy the contents of somefile into your clipboard while also seeing what is in the file.

sponge

sponge (from moreutils) allows you to pipe something to a file while also reading from it first. So for instance:

$ cat somefile | grep keyword | sponge somefile

Without sponge, you would first have to redirect the output of cat somefile | grep keyword to a temporary file, because simply doing

$ cat somefile | grep keyword > somefile

would blank somefile.

System free on excessive RAM usage

This is probably the single most annoying thing on Linux for me right now. Just some links for now:

Things that work for me:

Things that don’t work for me:

Things that I haven’t tried:

May 2022: For a while, I used thrash-protect, which worked sometimes but not usually? Eventually I just upgraded the RAM on my machine.

Backlinks