Cygwin Snippets and Resources

With Cygwin, sometimes you need all the help you can get. Here are some usefull snippets.

Bash

Find command for file, exclude Windows directories

This searches or .png files among the Cygwin files. The -path ‘/BlahBlah’ -prune prevents the recursion from going into specific directories, and the -o strings the options together. You need to knock out /cygdrive, /dev, and /proc to prevent the native Windows directories, registry, etc. from being searched. Without eliminating those, your search takes a long time.

[code language=”bash” collapse=”0″]
find / -path ‘/cygdrive’ -prune -o -path ‘/dev’-prune -o -path ‘/proc’ -prune -o -name ‘*.png’ -print
[/code]

Delete A File If Present, Delete Parent Directory If Now Empty

[code language=”bash” collapse=”0″]
#!/bin/sh
function RemoveFile()
{
if [ -f "$1" ]; then
chmod u+w "$1"
rm -f "$1"
fi
}

function RemoveFileAndEmptyParent()
{
RemoveFile "$1"
scdir=$(dirname "$1")
if [ -d "${scdir}" ]; then
if [ ! "$(ls -A "${scdir}")" ]; then
chmod u+w "${scdir}"
rm -r "${scdir}"
fi
fi
}

DeskTopDir=$(cygpath -A -D)

RemoveFile "${DeskTopDir}/My Shortcut.lnk"

SMDIR="$(cygpath -P -A)"/My\ Application

RemoveFileAndEmptyParent "${SMDIR}/My Shortcut.lnk"
[/code]

CD To Windows Directory In Clipboard

You need to install getclip for this.
cd "$(cygpath "$(getclip)")"

Now, if you want in alias for this in your ~/.bashrc file, do this:
alias cdw='cd "$(cygpath "$(getclip)")"'

Extract Contents From tar.bz2 File
tar -xjvf some-file.tar.bz2

Extract Contents From tar.z File
tar -zxvf some-file.tar.gz

Open File In Windows Clipboard In Vim

A handy alias in my .bashrc is used for this:
alias vimclip='vim "($cygpath "$(getclip)")"'

Recursively Grep, Excluding Directories
grep -r --exlcude-dir=".svn" MySearchTerm .

When Things Get Weird

Rebasing Cygwin DLLs

Nothing seems to work? Xterm in a bad mood and won’t load? Rebasing the DLL’s in Cygwin seems to do the trick often. So try this:

From a CMD prompt:

cd c:\cygwin
ash
./rebaseall -v
exit

This entry was posted in Cygwin, Snippets and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *