linux

Print Out A Random Futurama Quote

If you sent a curl request to the slashdot.org server you get back a random Futurama quote contained within the header information. The following curl command:

curl -Is slashdot.org

The commands supplied are I and s. I causes only the header of the file to be shown and s stops curl printing out anything. This returns the following headers:

HTTP/1.1 200 OK
Date: Wed, 25 Jun 2008 08:34:43 GMT
Server: Apache/1.3.41 (Unix) mod_perl/1.31-rc4
SLASH_LOG_DATA: shtml
X-Powered-By: Slash 2.005001
X-Bender: I'm an outdated piece of junk.
Cache-Control: private
Pragma: private
Connection: close
Content-Type: text/html; charset=iso-8859-1

Which contains a quote from Bender. To grab the correct line we then pass this through a regular expression to find a line that starts with an "X" and a dash, followed be either a B (for Bender) or an F (for Fry). The following line:

Bash Fork Bomb And The Cure

A fork bomb is a simple bit of shell code that, once run, will soon fill all available memory and fork space with itself. Here is the code, and remember, don't try this at home!

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

To explain what is going on we need to cut this code into sections. The first thing we do is refine a function called ":", which accepts no parameters.

$ :(){};

We then get this function to run itself recursively and also to run another version of itself in the background, this creates another fork of the program.

$ :|:&

Finally we start it all off with the first function call.

Delete File By inode Reference

If you want to delete a file that you can't type in the name of either because the name is long and complicated, or because it is difficult to type in without causing a syntax error then here is the solution.

You first need to find the inode reference of the file. This can be done by using the command ls -li. The start of each line has a number that is specific to that file. You could use the command ls -i , but the output is a little confusing.

To delete the file use the find command with the flag -inum, followed by a pipe into the rm (remove file) command like the following.

find . -inum 916618 | xargs rm

The xargs bit is used to pass a list of the files found from the find command to the rm command.

View Directory List After Entering

When navigating the file structure in Unix/Linux environments you will often find yourself typing cd to change the directory and then immediately typing ls to see the contents of the directory.

It is possible to run ls automatically every time you run cd by adding the following commands to your .bashrc file.

cd() {
 if [ -n "$1" ]; then
  builtin cd "$@" && ls
 else
  builtin cd ~ && ls
 fi
}}

 

Simple Trick To Run Last Command As Sudo

You can often forget what you are not running as a super user, so if you type in a command that you can't run with your current set of privileges it will tell give you a permission denied response.

An alternative is to use the !! command to run the last command in the .bash_history. Use this with the sudo command to run the last command as a super user.

> command
Permission denied
 
> sudo !!