linux snippets

Bulk renaming of files

Tagged rename, regexp, bulk, filename, bash, perl, linux  Languages bash

Rename the files in a directory by replacing a space with an underscore. The rename program comes with most modern Linux distros.

rename 's/\ /_/g' *.*

Colorize grep

Tagged grep, bash, color, linux  Languages bash

Put this export in .bashrc to make grep colorize the text it found.

export GREP_OPTIONS='--color=auto'

Disable pc speaker beep in Linux

Tagged pc speaker, linux, quiet, disable, audio, beep  Languages bash

Not really a snippet, still a good fix for a big annoyance. Temporarily disable the speaker by removing the driver for it.

sudo rmmod pcspkr

To make the silence more permanent add this line into /etc/modprob.d/blacklist

blacklist pcspkr

Mount a network drive with proper privileges

Tagged smbfs, samba, mount, smbmount, permissions, filesystem, network drive, linux  Languages bash

This is not a snippet either, but I always forget how the local permissions for a Samba mount are given, which is the reason I put it here. Uid is the id of the user you want to mount the drive as, and gid is respectively the group id. Username is the username you want to connect with.

sudo mount -t smbfs -o uid=1000,gid=1000,username=marko //remote_server/work /mnt/net/stage/work

Screenshot in wmii

Tagged wmii, screenshot, linux, imagemagick  Languages bash

Method 1

A simple script to create a screenshot in wmii. It probably works in other window managers too. I call it 'scrot'. Put it in the path and run it as you'd run any program in wmii. The "import" program comes with imagemagick.

#!/bin/bash
import -window root /tmp/screenshot.png

Method 2

If you want timestamped screenshots then

apt-get install scrot
mkdir -p ~/screenshots

And create a script with the following content for making the screenshot.

#!/bin/bash
scrot '%Y-%m-%d-%H-%M-%S_$wx$h_scrot.png' -e 'mv $f ~/screenshots'

Find a text pattern in jar files

Tagged jar, find, search, recursive, linux, unzip  Languages bash

Helpful when you need to find a class or package in some jar file recursively below the current directory. Still needs a test to see if the file found was a file or directory. Works case insensitively. Uses the unzip command because of it's performance superiority in comparison to jar.

#!/bin/sh
for f in find . -type f -name '*\.jar'
do
        unzip -l $f | grep -i $1 && echo "was found in $f"
done

Flushing iptables

Tagged iptables, firewall, linux  Languages bash

Reset all rules of iptables. Handy to put in the root's crontab while configuring a remote firewall.

#!/bin/sh
# 
# rc.flush-iptables - Resets iptables to default values. 
# 
# Copyright (C) 2001  Oskar Andreasson <bluefluxATkoffeinDOTnet>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307   USA

#
# Configurations
#
IPTABLES="/sbin/iptables"

#
# reset the default policies in the filter table.
#
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT

#
# reset the default policies in the nat table.
#
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t nat -P OUTPUT ACCEPT

#
# reset the default policies in the mangle table.
#
$IPTABLES -t mangle -P PREROUTING ACCEPT
$IPTABLES -t mangle -P POSTROUTING ACCEPT
$IPTABLES -t mangle -P INPUT ACCEPT
$IPTABLES -t mangle -P OUTPUT ACCEPT
$IPTABLES -t mangle -P FORWARD ACCEPT

#
# flush all the rules in the filter and nat tables.
#
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
#
# erase all chains that's not default in filter and nat table.
#
$IPTABLES -X
$IPTABLES -t nat -X
$IPTABLES -t mangle -X

Find out the process id of a process you know how to single out with grep.

Tagged linux, cut, pid, process id  Languages bash

The cut program can be really versatile. Give it the character used to separate columns using the -d switch, and the column(s) you want to print using the -f switch. Here we give ' ' as a parameter to the -d switch because spaces indicate the columns here. A single column is given using -fn and multiple columns using -fn,n,n,n,n syntax.

ps axu |grep FitNesse | grep java |grep -v grep | cut -d' ' -f3

Remote X11 apps through SSH

Tagged ssh, x11, remote applications, linux  Languages bash

In order to forward X11 applications from a remote server into your local X session the server must support tunneling of X11 apps with this property in sshd_config

X11Forwarding yes

Then simply issue the -X parameter for the SSH client.

ssh -X remotehost

Now run an X11 application from the SSH terminal and it's UI will be tunneled to your local X session.

gvim my_script.rb