thefekete.net

$> :(){ :|:& };:

Bash script / function to append file names

In my previous post about appending file names, I made a one-off solution.

Here’s a bash function that you can throw in your .bashrc and use more easily:

#!/bin/bash

# Put this in your ~/.bashrc and use it like a script...
append_filename() {
    USAGE="append: Puts text between filename and extension"
    USAGE="$USAGE\nUsage: $0 APPENDAGE file1 file2 file3 ..."

    if [ "$#" == "0" ]; then
        echo -e "$USAGE"
        return 1  # exit kills shell this is called from
    fi

    # get appended string
    APPENDAGE=$1
    shift

    while (( "$#" )); do
        if [ -f "$1" ]; then  # don't rename directories
            mv "$1" "${1%.*}$APPENDAGE.${1##*.}"
        fi
        shift  # grab the next file in list
    done
}

Appending filenames, not extensions

I had a group of files that I needed to append a suffix to, but leave the extension intact. Basically, I needed to split the filename and extension, then put them back together with some filling in the middle…

This answer @ stack overflow shows how to split them, and then it’s pretty easy to put them back together:

#!/bin/bash
FILLER='-1'  # Whatever you want to append to the filename
for i in *
do
    mv $i "${i%.*}$FILLER.${i##*.}"
done

Splitting large files for a FAT32 removable drive or flash drive

If you got a file or group of files to put on a removable drive with a FAT32 file system – and any of them are over 4GB – then you need a way to make it smaller for transport. The easiest way is with split, cat and tar.

Read the rest of this entry »

Bash: Count Lines of Code in a Python Project

#!/bin/bash
find . -name '*.py' | xargs cat | sed '/^\s*#/d;/^\s*$/d' | wc -l

xargs converts find’s output to arguments, the sed RE drops comments and blank lines, and wc counts them lines. Thanks to these posts on stack overflow for some reference material.

Quickly check line character maximums from the command line

If you want to make sure you didn’t go over 79 or 80 characters in a file you can simply run:

#!/bin/bash
grep -Pn '.{80,}' file-to-check

This uses grep’s perl regex option (-P) so we can use the expression .{80,} to find any lines that have >= 80 characters. The -n option prints the line numbers in the output so you can find them easily.

SSH Key transfer one-liner

=== UPDATE ===
Thanks to Tomasz Muras from Open Source Tech Blog, I now know about ssh-copy-id, which basically renders this post useless, except for academic reasons… Nevertheless, it’s a good example of piping through ssh to programs on a remote machine, so I’ll leave it up.


If you’ve already got access to a server or workstation via ssh, but are tired of entering your password, or need more security… You can transfer the public key on the client straight into the authorized_keys2 file on the server with one step:

PUB_KEY='id_rsa.pub'
REMOTE_HOST='example.com'
REMOTE_PORT=22

cat ~/.ssh/$PUB_KEY | ssh $REMOTE_HOST -p$REMOTE_PORT \
"cat >> ~/.ssh/authorized_keys2"

PUB_KEY will most likely be id_rsa.pub or id_dsa.pub. REMOTE_HOST and REMOTE_PORT are the hostname and port number of the server you’re sending the key to (you can leave the port option out if its on the standard ssh port 22).

Change volume from command line

To change the system volume from bash (on ubuntu) do the following:

#!/bin/bash

# To raise the volume by 3%:
amixer -c 0 set Master 3+

# To lower the volume by 3%:
amixer -c 0 set Master 3-

Bash: Copy all files in an m3u playlist to folder

I’ve got a pretty good list of songs I like, and I wanted to copy them all to a USB stick for the car… So here’s the bash one-liner I wrote to take an m3u playlist and copy all the files to a new directory:

#!/bin/bash
cat playlist.m3u | grep -v '#' | \
while read i; do cp "${i}" /path/to/destination/ ; done

Read the rest of this entry »

Ubuntu 10.04 (Lucid Lynx) Setup

To fix the stupid window controls and get the text-mode location bar in nautilus:

#!/bin/sh
gconftool-2 --type string --set \
/apps/metacity/general/button_layout ":minimize,maximize,close" &&
gconftool-2 --type=Boolean --set \
/apps/nautilus/preferences/always_use_location_entry true

Note that I removed the ‘menu’ from the window controls. If you want it bone stock, use "menu:minimize,maximize,close" instead.

Aptitude key error fix for VirtualBox

Fix found at the Ubuntu Forums:

#!/bin/bash

wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
sudo apt-get clean
sudo cd /var/lib/apt
sudo mv lists lists.old
sudo mkdir -p lists/partial
sudo apt-get clean
sudo apt-get update