Update: No longer available and monthly t-shirts discontinued.

I'm super excited to announce monthly catonmat geek t-shirts in collaboration with Teespring! Every month on the first Monday I'll be launching a Teespring shirt campaign with an awesome geek tee.

Teespring is basically Kickstarter for t-shirts. If enough people commit to buying a shirt, everyone gets their shirt. Otherwise nobody gets a shirt (and nobody gets charged).

This month I'm presenting the limited-edition HTML5 tees. This shirt is also the official tee of the HTML5 Dev Conf that's taking place in SF on October 15 & 16.

This shirt is designed by my co-founder SubStack and it's available on American Apparel and Hanes tees.

Enjoy!

Love this shirt and want to get more geek shirts in the future? Subscribe to my blog and follow me on twitter for updates!

Hey guys! A few weeks ago I wrote an article called All About Bash Redirections. It explains all the possible bash redirections with illustrations. I thought it would be a great idea to make a cheat sheet that summarizes all these redirections. So I did.

Here is the bash redirections cheat sheet:


Download PDF | Download PNG | Download TXT | Download TEX

If you want to learn how each one of these redirections work, read my article All About Bash Redirections!

Found a mistake or want to contribute to this cheat sheet? Fork it on github!

Enjoy!

PS. I've created a dozen different cheat sheets. Take a look at my other cheat sheets about awk, ed, sed, perl, screen, more bash, gnu coreutils, util-linux, and many others.

A few days ago I made the GNU Coreutils Cheat Sheet. It had over 15,000 downloads in the first day and a lot of people suggested that I create more cheat sheets. So I did! I just created util-linux cheat sheet. Most of the utilities from util-linux are available on all Linux systems, so this cheat sheet provides a great overview of what you can do in Linux.

Here is the util-linux cheat sheet that lists all programs in the util-linux package:


Download PDF | Download PNG | Download XLSX

This cheat sheet is really useful if you're learning Linux and want to have a quick overview of all the available commands!

If you liked this Linux cheat sheet, take a look at other cheat sheets that I've created over the years. They include awk, ed, sed, perl, screen, bash, and many others.

A long, long time ago when I was mastering Linux I created three cheat sheets about GNU fileutils, GNU shellutils and GNU textutils. I'd keep them in front of me at all times, and I quickly memorized which commands did what.

A few days ago I remembered about them and decided to publish them on my blog but it turns out fileutils, shellutils and textutils were merged in GNU coreutils in 2003; so I thought why not create a new cheat sheet that lists all the utilities in coreutils along with their short descriptions.

Here is the GNU coreutils cheat sheet that lists all programs that are in coreutils:


Download PDF | Download PNG | Download XLSX

This cheat sheet is really useful if you're learning Linux and want to have a quick overview of all the available commands!

Bonus: Here are some of the commands that I used to create the cheat sheet:

$ curl 'http://www.gnu.org/software/coreutils/manual/coreutils.html' 2>/dev/null |
    grep 'h3 class' |
    grep 'class="command"' |
    sed 's/.*class="command">//' |
    sed 's|</span></samp>||' |
    sed 's|</h3>||' |
    grep ':' |
    sort

This one extracts the command descriptions from the coreutils manual.

And:

$ wget 'http://ftp.gnu.org/gnu/coreutils/coreutils-8.13.tar.gz'
$ gunzip < coreutils-8.13.tar.gz | tar -vx
$ cd coreutils-8.13/src
$ find . -name '*.c' | sort | xargs head -1

This one extracts the command descriptions from the program C source files.

Also take a look at other cheat sheets that I've created. They include awk, ed, sed, perl, screen, bash, and many others.

I just had this quick idea to write a tcp port scanner in bash. Bash supports the special /dev/tcp/host/port file that you can read/write. Writing to this special file makes bash open a tcp connection to host:port. If writing to the port succeeds, the port is open, else the port is closed.

So at first I wrote this quick script:

for port in {1..65535}; do
  echo >/dev/tcp/google.com/$port &&
    echo "port $port is open" ||
    echo "port $port is closed"
done

This loops over ports 1-65535 and tries to open google.com:$port. However this doesn't work that well because if the port is closed, it takes bash like 2 minutes to realize that.

To solve this I needed something like alarm(2) to interrupt bash. Bash doesn't have a built-in alarm function, so I had to write my own using Perl:

alarm() {
  perl -e '
    eval {
      $SIG{ALRM} = sub { die };
      alarm shift;
      system(@ARGV);
    };
    if ($@) { exit 1 }
  ' "$@";
}

This alarm function takes two args: seconds for the alarm call, and the code to execute. If the code doesn't execute in the given time, the function fails.

Once I had this, I could take my earlier code and just call it through alarm:

for port in {1..65535}; do
  alarm 1 "echo >/dev/tcp/google.com/$port" &&
    echo "port $port is open" ||
    echo "port $port is closed"
done

This is working! Now if bash freezes because of a closed port, the alarm 1 will kill the probe in 1 second, and the script will move to the next port.

I went ahead and turned this into a proper scan function:

scan() {
  if [[ -z $1 || -z $2 ]]; then
    echo "Usage: $0 <host> <port, ports, or port-range>"
    return
  fi

  local host=$1
  local ports=()
  case $2 in
    *-*)
      IFS=- read start end <<< "$2"
      for ((port=start; port <= end; port++)); do
        ports+=($port)
      done
      ;;
    *,*)
      IFS=, read -ra ports <<< "$2"
      ;;
    *)
      ports+=($2)
      ;;
  esac


  for port in "${ports[@]}"; do
    alarm 1 "echo >/dev/tcp/$host/$port" &&
      echo "port $port is open" ||
      echo "port $port is closed"
  done
}

You can run the scan function from your shell. It takes two arguments: the host to scan, and a list of ports to scan (such as 22,80,443), or a range of ports to scan (such as 1-1024), or an individual port to scan (such as 80).

Here is what happens when I run scan google.com 78-82:

$ scan google.com 78-82 
port 78 is closed
port 79 is closed
port 80 is open
port 81 is closed
port 82 is closed

Similarly you can write an udp port scanner. Just replace /dev/tcp/ with /dev/udp/.

Update

It turns out GNU's coreutils include timeout utility that runs a command with a time limit. Using timeout we can rewrite the tcp proxy without using Perl for SIGALRM:

$ timeout 1 bash -c "echo >/dev/tcp/$host/$port" &&
    echo "port $port is open" ||
    echo "port $port is closed"