Here is my little one-liner, because I used it today and I find it fun:
for f in `find .` ; do mv $f `echo $f | tr '[A-Z]' '[a-z]'` ; done
Here is my little one-liner, because I used it today and I find it fun:
for f in `find .` ; do mv $f `echo $f | tr '[A-Z]' '[a-z]'` ; done
I felt I needed to write an article about netcat, so here is it !
Netcat is an incredibly usefull tool, that allows you to play with tcp connection easily from the shell.
Basically, as it name implies, it’s just cat over the network, but what its name doesn’t tell you is that it also can act as a socket listener.
So let’s play with pipes, here is one of my favourite use of netcat:
mkfifo proxypipe cat proxypipe | nc -l -p 80 | tee -a inflow | nc localhost 81 | tee -a outflow 1>proxypipe
This command will redirect traffic from localhost:80 to localhost:81, in the inflow file you while find the incoming http request, in the outfile, you will find the http response from the server.
Similarly, you can do this:
cat proxypipe | nc -l 80 | tee -a inflow | sed 's/^Host.*/Host: www.google.fr/' | nc www.google.fr 80 | tee -a outflow >proxypipe
This will allow your browser to point to google using http://localhost .
Anyway, this is my favourite but netcat has thounds of other uses, have a look at it !
It can be usefull for file transfers (gzip|nc) , performance measurement (dd|gzip), protocol debugging (replaying requests), security testing (nc does port scan) …
I’ve successfully insalled redmine pretty much easily but I needed to find out what packages to install with apt, which one with gem, which version …
Here is my magic receipe to install it all:
apt-get update apt-get install subversion mysql-server rubygems rake pwgen # next line generates a password for the database export PASSWORD=`pwgen -nc 8 1` gem install -v=2.1.2 rails echo "CREATE DATABASE redmine DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ; GRANT ALL PRIVILEGES ON redmine.* TO 'redmine'@'localhost' IDENTIFIED BY '$PASSWORD' WITH GRANT OPTION; FLUSH PRIVILEGES" | mysql cd /opt/ svn export http://redmine.rubyforge.org/svn/branches/0.8-stable redmine-0.8 cd redmine-0.8/ cat <<EOF >> config/database.yml production: adapter: mysql socket: /var/run/mysqld/mysqld.sock database: redmine host: localhost username: redmine password: $PASSWORD encoding: utf8 EOF rake db:migrate RAILS_ENV="production" rake redmine:load_default_data RAILS_ENV="production" apt-get remove pwgen subversion RAILS_ENV="production" ./script/server
And that’s it ! Redmine is running on port 3000.
I did this on an EC2 instance and it works like a charm (ami-7cfd1a15).
Maybe next article will discuss running redmine in mongrel or apache, and creating an init script for having redmine running on boot !
Today I encountered a problem: I needed to restore a single table from a database mysqldump.
Usually you cat the-mysqldump.sql |mysql the_database
so you’re only able to restore the full database. I didn’t find any mysqldump option to extract a single table from a full database dump, so I’ve come up with this (minimal) shell script:
#!/bin/sh extract_table(){ TABLE=$1 DUMPFILE=$2 grepstr="/*!40000 ALTER TABLE \`$TABLE\`" lines=`grep -n "$grepstr" $DUMPFILE |cut -d":" -f1` lines=`echo $lines|sed 's/\ /,/' ` echo "LOCK TABLES \`$TABLE\` WRITE;" sed -n "$lines p" $DUMPFILE echo "UNLOCK TABLES;" } extract_table $1 $2
Use it like this:
./this-script.sh table-to-extract dumfile-for-extract |mysql the_database
(use the |mysql
after you have checked the content).
Be carefull, this script is minimalistic:
If some people request it, I’ll write all these features, but as usual, I wanted to come up with a solution I could already use one hour a ago, and I’m spending time to write this script, let’s do it the faster I can ! 🙂
Let’s say you want to extract a part of a file, for example from line 12 to 20.
I’ve come up with two solutions:
head -n20 |tail -n8
sed -n '12,20p'
-n
option, so that the input is not printed to the output, than give sed an expression (within quotes), the expression is the first line, a coma, the last line, and the “p” instruction which means print.