Shell tricks
I put any tricks I pick up with shells here, as they are easy to forget.
Recursive grep
GNU grep has a silly -R switch. Don't use it, use find:
find . -type f -exec grep "mysearch" /dev/null {} \;
Too many files
$ rm *
pdksh: rm: Argument list too long
Can be easily fixed with:
for i in `ls`; do rm $i; done
Split string
Use a for loop and set the variable IFS to the separator.
#!/bin/sh
IFS=:
for dir in $PATH; do
if [ -x $dir/gcc ];then
GCC=$dir/gcc;
break;
fi
done
The above searches through the path for the location of the executable gcc.
Find/Replace multiple files with Perl
perl -pi -e "s/find/replace/g" file1 file2 ...
This one liner runs the given regex over the specified files, modifying the files inline. No backups are made for you.
Cool prompt
I picked this up on the internet, but sadly I can't remember where. Kudos to whoever put the effort into the colours. Put it in your .profile:
# cool prompt
if type -p printf > /dev/null 2>&1; then
red=$(printf '\e[31m')
colors="\[\e[0;36m\]\w\[\e[01m\]\[\e[0m\]"
export PS1="${NAME}:${colors}$([ $? -eq 0 ]||printf $red)\$\[\e[0m\] "
else
export PS1='\[\e[0;36m\]\w\[\e[01m\]\[\e[0m\]\$\[\e[0m\] \$'
fi