Linux/Unix

Posts about using Linux and Unix

Using !$ To Use Last Parameter

Much like using Alt+. to print out last parameter you can also use !$ to use the last parameter from the previous command. Here is a simple example.

# cd ..
# cd !$

In this example we are moving up a directory and then doing the same action again, the !$ is a short cut to get hold of the ... This is more useful when doing things with longer parameters like directory or file names. For example here we are creating a directory and then moving into that same directory.

# mkdir /www/htdocs/directory/
# cd !$

Raising Skinny Elephants Is Utterly Boring

This might sound odd, but this is a mnemonic that helps you remember a sequence of letters that you can enter when your Linux system is locked. This is a last ditch attempt to get things up and running again and should only be used if all else fails and the only other thing that you can do it pull the plug.

If you have also tried pressing Ctrl+Alt+backspace and this does nothing then you can try using the key sequence Raising Skinny Elephants Is Utterly Boring.

Hold down the left Alt key and the SysRq key (found on the print screen button) and press each letter in turn. Make sure that you give a little time between keystrokes.

  • r
  • s
  • e
  • i
  • u
  • b

Here is a description of what you are doing.

Search And Highlight With Grep

Searching for things in many files is easy with the grep command, but in order to view the results with the expression results highlighted you need to use grep in conjunction with less.

grep expression *.txt | less +/expression

If you wanted to search all files called something.txt for the letter d you would use this line.

grep d *.txt | less +/d

Use Alt+. To Print Out Last Parameter

A handy trick when using a Unix/Linux system is to repeat the last parameter from the previous line. Lets say that you typed in the following line to move a file to another directory.

$ mv file.txt /usr/local/

To then move into that directory you can just type cd and Alt+. to copy in the last parameter used in the last line. This will put the following on the command line.

$ cd /usr/local/

You can press Alt+. multiple times to go back through your parameter history. Note that it only records the last parameter used for each line. So for the example above, if you pressed Alt+. twice you would get the last parameter of whatever command you executed before moving the file.

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 !!

The PHP User On Linux

Reading or writing a file using PHP is quite a common practice, but it can often fall cause programs to fall flat on their face if the proper user privileges are not in place. Although this is not a problem on Windows machines due to the lack of a proper security model, but on Linux machines you need to make sure your scripts can run with the correct permissions.

First you must determine what the name of the user and group is. On OS X the default is "www" for both user and group. If you are using Apache then the user and group information is kept in the http.conf file. Look for a couple of lines that look like this, this is the default settings for Apache 2.2.