Linux Commands

Gracefully degrade less to cat for output not exceeding one page

echo "hi" | less -FX

From man less:

       -F or —quit-if-one-screen
              Causes less to automatically exit if the entire file can be displayed on the first screen.

       -X or —no-init
              Disables sending the termcap initialization and deinitialization strings to the terminal.  This is sometimes desirable if the deinitialization  string  does  something  unnecessary,  like
              clearing the screen.

Overcoming filename spaces with git ls-files

If your filenames and/or directory names contain any spaces, it can be a headache to pipe them around in bash. Enter null terminated filenames.

git ls-files -z | xargs -0 grep -li <word>

From git help ls-files:

-z
           \0 line termination on output.

From man xargs:

    • null
  • 0 Input items are terminated by a null character instead of by whitespace, and the

quotes and backslash are not special (every character is taken literally). Dis-

Recursively fixing a bad umask

I usually have a more restrictive umask than the default in my .bashrc:

# Tighten up umask
umask 077

…but every once in a while, I need to open up a directory (and all of its subdirectories and contents). Usually in this situation, I want the directories to have a different set of permissions (say, 755) than the contents (say, 644).

Convert a PDF to greyscale

gs -sOutputFile=grey.pdf -sDEVICE=pdfwrite \
  -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray \
  -dCompatibilityLevel=1.4 color.pdf < /dev/null

Source: http://vince-debian.blogspot.com/2008/05/pdf-conversion-to-grayscale.html

URI Encoding an image

I've known for some time that it's possible to embed an image into CSS (useful for greasemonkey scripts), using a format like so:

url(data:[mimetype];base64,[data])

...where [mimetype] is the mime type of your data (eg, image/png).

It's eluded me until now how exactly to get at the [data] bit of this equation. This is simply the base64 encoded string of the actual bytes of the file, which you can get like so:

cat foo.png | base64

This will spit a bunch of garbage to your screen, which you copy and paste into the [data] placeholder.

Changing partition labels

Change an ext2/ext3 partition label

View current label

sudo e2label /dev/niobe/home

Change label

sudo e2label /dev/niobe/home home

Change an NTFS partition label

sudo umount /dev/sdb1
sudo ntfslabel /dev/sdb1
sudo ntfslabel /dev/sdb1 my_external

Ubuntu caches the drive's label so to see the full affects of the change it is not enough just to umount and mount it again. You have to umount, remove, put back, mount again.

Source: https://help.ubuntu.com/community/RenameUSBDrive

Alternatives

Choosing an alternative

% sudo update-alternatives --config <name>

...where <name> is the name of a symlink in /etc/alternatives. Some of the ones I've used:

editor
x-www-browser
java

You are then prompted to select among the various alternatives available. If the desired alternative is not listed, it will have to be installed.

Installing a new editor

wtf is vim.tiny??

sudo update-alternatives --install /usr/bin/editor editor /usr/bin/vi 1

Syndicate content