Category: Unix/Linux

Find And Replace On All Files In And Below A Directory

9 October, 2008 | Unix/Linux | No comments

The following shell command uses the find function to find all files in or below the current directory that have the extension php. It then passes each file found onto a sed command which then replaces all <? with the longer &lt?php version.

find . -name '*.php' -exec sed -ie 's#<?#<?php#' {} \;

The -name argument in find will look at the base of the file name, that is, the file without any directory path. The -exec command is used to pass each file found onto another command, in this case sed is used.

Using !$ To Use Last Parameter

22 July, 2008 | Unix/Linux | No comments

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

21 July, 2008 | Unix/Linux | No comments

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.

  • The r stands for put keyboard in raw mode
  • The s for sync the disk
  • The e for terminate all processes
  • The i for kill all processes
  • The u for remount all filesystems read only
  • The b for reboot the system

Also , if your filesystem is Ext3 or ReiserFS and on reboot it wants you to do a filesystem check, don’t touch any key when it asks you to press "Y" and let it recover the journal automatically.

Also note that for this to work you need to have the SysRq key enabled in the Linux kernel, also called CONFIG_MAGIC_SYSRQ. You can check if it is enabled by typing:
ls /proc/sys/kernel/sysrq
If this prints out a line with that word then the key is enabled.

Search And Highlight With Grep

14 July, 2008 | Unix/Linux | No comments

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

27 June, 2008 | Unix/Linux | 1 comment

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.