Linux Tips

From EggeWiki

compopt

Getting this error: <geshi lang="bash"> compopt -o filenames bash: compopt: command not found </geshi>

Fix: You are using bash 3.0, try using bash 4.0+

Remove _2 files

Mac creates _2 when ever you copy a file the second time. <geshi lang="bash"> find . -name "*_2.???" -type f | while read f; do g="$(echo "$f" | sed -e 's/_2//;')"; if awk '{ print $1; }') = $(md5sum "$g" | awk '{ print $1; }') ; then rm "$f"; fi; done </geshi>

Find duplicate files

<geshi lang="bash"> find . -type f -print0 | xargs -0 -n1 md5sum | sort -k 1,32 | guniq -w 32 -d --all-repeated=separate | sed -e 's/^[0-9a-f]*\ *//;' </geshi>

Recursive file search

Quite often I want to search for text in a source directory. The following bash function searches all files except those in the .svn directories.

<geshi lang="bash"> ffind () {

   find . -name ".svn" -prune -o -exec grep --color=auto "$@" {} +

} </geshi>

append your ssh key with one command

<geshi lang="bash"> cat ~/.ssh/id_rsa.pub | ssh example.com bash -c "cat >> ~/.ssh/authorized_keys" </geshi>

vim

To install my vim theme: <geshi lang="bash"> mkdir -p ~/.vim/color cd ~/.vim/color wget http://www.cs.cmu.edu/~maverick/VimColorSchemeTest/PapayaWhip.vim echo "colorscheme PapayaWhip" >> ~/.vimrc </geshi>

Remove CVS RCS Headers

Not everyone loves having RCS keywords in their files. Here's a one liner to remove them all from a code base. <geshi lang="bash"> find . -type f -name "*.java" | xargs grep -lE '\$(Header|Revision|Date|Id|Author):[^$]*\$' | \

xargs sed -rni  -e '/\$(Header|Revision|Date|Id|Author):[^$]*\$/!p'

</geshi>

Damn Small Linux on Dell Latitude D620

I installed DSL onto a USB drive, and tried to boot linux off the drive. The kernel would always panic, regardless of the cheat codes I tried. Next, I tried burning DSL to a CD-ROM. Booting the CD-ROM with the 'dsl acpi=off' switch got me to the desktop. I was able to mount the HD, but not see the USB drive. After trying all sorts of things to mount the USB drive, I decided to try turning off USB emulation in the BIOS. This worked and I was able to copy the files from the NTFS partition to my 1 GB FAT USB stick.

tr

I like to use 'tr' to create long constant names in Java. When CheckStyle complains that '2004' is a magic number, I like to replace it with something more interesting than 'YEAR_2004'. I haven't found a way to do this inside of IntelliJ but having a Bash shell open makes this easy.

$ echo "International Year to Commemorate the Struggle against Slavery and its Abolition" | tr '\ -' '__' | tr [:lower:] [:upper:]
INTERNATIONAL_YEAR_TO_COMMEMORATE_THE_STRUGGLE_AGAINST_SLAVERY_AND_ITS_ABOLITION

Script to colorize source code using vim

Create a .tar.gz archive

tar -pczf name_of_your_archive.tar.gz /path/to/directory

Unzip and extract an archive

gzip -drv ruby-1.8.3.tar.gz
tar -xf ruby-1.8.3.tar

Copy files from work to this website

rsync -avz -e ssh --rsync-path=/usr/bin/rsync /u/eggebr/tmp/ttf-bitstream-vera-1.10/ egge@theeggeadventure.com:/home/egge/fonts
rsync -avz -e ssh --rsync-path=/usr/bin/rsync /u/eggebr/.vimrc egge@theeggeadventure.com:/home/egge/

Backup [ccnorthjersey.org]

$ rsync -avz -e ssh ccnorthj@ccnorthjersey.org:/home/ccnorthj /cygdrive/c/Backup/ccnorthjersey.org

Backup theeggeadventure.com

$ rsync -avz -e ssh egge@theeggeadventure.com:/home/egge /cygdrive/c/Backup/theeggeadventure.com

[Rebuild a Debian Kernel]

Get a stack trace from a running Python process

This will print the stack, and then the process will exit. I'm not sure how to get the stack without killing the process.

 kill -SIGINT 28350

My bash prompt

Note - it's important to that the non-printing characters are enclosed in "\[" and "\[" so bash can do line wraps properly.

  export PS1="\[\e[32m\]\h\[\e[0m\]@\[\e[34m\]\W$\[\e[0m\] "

Use sed to remove ANSI colors

<geshi lang="bash">

  1. better way - thanks timmah

echo -e '\e[31mfoo\e[0m' | sed 's/\x1b\[[0-9]\{1,2\}\(;[0-9]\{1,2\}\)\{0,2\}m//g'

  1. old way, useing ctrl-v ctrl-[

echo -e '\e[31mfoo\e[0m' | sed 's/^[\[[0-9]\+m//g' </geshi>

Note: this works great on Linux, but not on OS X. Must be some difference betweent he GNU sed and BSD sed.

Redirecting sudo output

 Long answer: http://www.dagnall.net/blog/?p=41
Short answer:
  sudo -u someuser /bin/bash -c \"command > out.txt\"
  

Touch all open files in a directory

lsof +D $PWD | awk '{print $9}' | uniq | grep -v NAME | touch

Missing font problem

  • Problem: I get this warning when running xemacs on a Linux WS3 host:
Warning: Missing charsets in String to FontSet conversion
Warning: Unable to load any usable fontset

I don't have this issue on RH 8.0 hosts or SunOS.

  • Fix: The problem is the LANG environment variable.

It's probably set to en_US.UTF-8. Change it to en_US.iso88591, and the warning should go away. Or else find some fonts that are UTF-8 encoded, I suppose.

so you can set your .envrc or just do a

setenv LANG en_US.iso88591

Search & Replace text in a batch of files

for f in `ffind "*.java" | xargs grep -l "foo"`; do sed -e "s/foo/bar/" ${f} > tmp; mv -f tmp ${f} ; done

Verify all the jar's in the classpath exist

<geshi lang="bash">

  1. Warn if any jar's are missing

echo $CLASSPATH | tr ":" "\n" | xargs cksum > /dev/null </geshi>

Note: see the much better ruby script Classpathchecker.rb which does the same thing and also works on Windows.