Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Recursive grep without grep -r

Bumped into this situation recently on a Solaris system. I wanted to do a recursive grep, using the -r flag. That flag is not standard, so I was stuck until I whipped up this little diddy.

After navigating to the directory that you want as the root of the search, enter
find . -type f -exec grep <pattern> {} \;

It will only show the entries that match that pattern. Don't forget that you can also add in addition filters to the find command to limit the search file types, if so needed. So, by way of example, if you wanted to find every time you used the word Vista in your home directory, open up the command line and enter:
[user@host ~]$ find . -type f -exec grep Vista {} \;

My search returned nothing ;-)

Deleting a line from a file matching criteria

I was messing around trying to figure out an easier way to remove entries from my /etc/portage/package.keywords file without having to drop into VI all the time and came up with this. You can put this into a bash script to make things alot easier on yourself if you are constantly altering your mask/use/keywords files in Gentoo.

sed -i '/x11-wm\/compiz/d' /etc/portage/package.keywords


The above example will remove any line that matches the regular expression x11-wm/compiz and edit the file in place "-i".

list files recursively

// list files recursively using ls

ls -R


while searching for files


ls -R | grep 'searchterm'

Cross-platform file encryption with TrueCrypt

# first download & install the free, open-source TrueCrypt program from http://www.truecrypt.org/downloads.php

ls -ld /Applications/TrueCrypt.app
ls -l /Applications/TrueCrypt.app/Contents/MacOS/TrueCrypt

open -a TrueCrypt

1. click "Create Volume"
2. TrueCrypt Volume Creation Wizard: select "Create a file container"
3. click Next
4. Volume Type: select "Standard TrueCrypt volume"
5. click Next
6. Volume Location: click "Select File ..."
7. navigate to ~/Desktop with the file browser, then click "New Folder"
8. enter "Name of new folder": TrueCrypt, then click Create
9. enter in "Save As": MyTrueCrypt, then click Save
10. Volume Location: the "Select File ..." field should contain /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt
11. click Next
12. Encryption Options:
    Encryption Algorithm: AES
    Hash Algorithm: RIPEMD-160
13. click Next
14. Volume Size: 3 MB
15. click Next
16. Volume Password: *********************  (more than 20 characters recommended)
17. click Next
18. Format Options: Filesystem Options: Filesystem type: select FAT
19. click Next
20. Volume Format: Move your mouse as randomly as possible within this window
21. click Format
22. "The TrueCrypt volume has been successfully created."
23. click OK
24. Volume Created
25. click Exit
26. click Close (to quit TrueCrypt)

open -a TrueCrypt

1. click "Select File ..."
2. navigate with the file browser to /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt
3. click Open
4. click Mount and enter your password: *********************
5. double-click: Slot 1 : Volume: /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt  Size: ...

touch '/Volumes/NO NAME/file.txt'    # or copy a text file to '/Volumes/NO NAME'
echo 'This is a test!' >> "$_"
cat '/Volumes/NO NAME/file.txt'
ls -l "$_"
# hdiutil unmount '/Volumes/NO NAME'

6. click Dismount
7. click Close


# References:
# Beginner's Tutorial, http://www.truecrypt.org/docs/?s=tutorial
# http://www.truecrypt.org/faq.php
# http://blogs.oreilly.com/digitalmedia/2008/03/truecrypt-51-open-source-file.html
# http://www.askstudent.com/security/a-step-by-step-guide-on-encrypting-files-using-truecrypt/
# http://techvj.blogspot.com/2007/03/secure-email-attachments-with-truecrypt.html


#------------------------------------------------


# download & install OSXCrypt, http://www.osxcrypt.org
man ocutil
ocutil
ocutil create -help

kextstat | grep OSXCrypt
ioreg -lw 0 | grep -i osxcrypt


# Example Usage

# Create a 200M volume, FAT formatted, using Seprent-Twofish-AES
ocutil create -verbose -fat -algorithm Serpent-Twofish-AES -size 200M MyCryptedVolume.img

# The same, but using also keyfiles
ocutil create -verbose -fat -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -algorithm Serpent-Twofish-AES -size 200M MyCryptedVolume.img

# The same, but only using the keyfiles
ocutil create -verbose -fat -nopass -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -algorithm Serpent-Twofish-AES -size 200M MyCryptedVolume.img

# Create an hidden volume of 100M, FAT formatted, using Serpent-AES, in a previously created volume
ocutil create -verbose -hidden -fat -algorithm Serpent-AES -size 100M MyCryptedVolume.img

# If the outer volume requires keyfiles you'll have something like ...
ocutil create -verbose -hidden -fat -algorithm Serpent-AES -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -size 100M MyCryptedVolume.img

# If you want to add keyfiles to the hidden volumes to an outer volume with keyfiles, your command will be something like ...
ocutil create -verbose -hidden -fat -algorithm Serpent-AES -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -size 100M -hidden_keyfiles MyHiddenKeyFile MyCryptedVolume.img

# The same as before but only with keyfiles for the hidden volume
ocutil create -verbose -hidden -fat -algorithm Serpent-AES -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 -size 100M -hidden_nopass -hidden_keyfiles MyHiddenKeyFile MyCryptedVolume.img

# Attaching a volume
ocutil attach -verbose MyCryptedVolume.img

# If the volume requires keyfiles
ocutil attach -verbose -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 MyCryptedVolume.img

# The same as before but only with keyfiles
ocutil attach -verbose -nopass -keyfiles MyKeyFile1,MyKeyFile2,MyKeyFile3 MyCryptedVolume.img

# Attaching an hidden volume
ocutil attach -hidden -verbose MyCryptedVolume.img

# Attaching a volume protecting its hidden volume
ocutil attach -protect_hidden -verbose MyCryptedVolume.img

# Detaching a volume
ocutil detach -verbose MyCryptedVolume.img
#ocutil detach -verbose device-name

# Generating one or more keyfiles
ocutil genkey -verbose output1.key [output2.key ...]

vim backreference on search and replace

// description of your code here

Use \(\) to denote an atom.
Use \1 to specify the first atom.

<this is your search and replacement terms>

:%s'\(<stuff_to_find>\)'\1<new_stuff>'gc

symbolically link entire directory contents

// description of your code here

ln -s /full/dir/path/to_files /full/dir/path/destination

Run script just before linux reboot (runlevel 6 / rc6.d)

update-rc.d is the program (script) which creates the necessary symbolic links to the /etc/rcX.d directories.

Here we are running the script found at /etc/init.d/updatedb_start, within runlevel 6, which is reboot.

Running this will create a symbolic link to the actual script file which lives within /etc/init.d (as it always should).

So, within /etc/rc6.d/ we'll see a symbolic link to our script.

The period at the end of the command is required. Tells update-rc.d that we're finished with the command (no further runlevels to edit).

We're using "stop" instead of "start" because we want to run the script during the killing off scripts part of the rc6.d scripts.
This causes the script to be run with a "stop" parameter, for example: updatedb_start stop
We don't actually use the stop parameter in this case, it's just ignored.

update-rc.d updatedb_start stop 18 6 .

log to syslog

how to log something to syslog

logger This is my message to syslog

activate root in ubuntu after install

After this you can run su.

sudo passwd root

running scheduled programs in linux

use crontab

Edit the current configuration for crontab with:
crontab -e


Normally we'd created bash script files located in /usr/local/bin and have them specified for execution within our crontab configuration.