Bash script auto-complete filename by file type/extension

Suppose I wrote a bash script with usage script.sh filename.type. That is, the script takes in filenames as arguments. Bash’s tab completion works with all files. However, I’ve seen commands that will complete the filename even faster because it will select the file with the “right” extension. For example, pdflatex firstpart[Tab] would automatically select firtpart.whatever.is.next.tex. I’d like this feature for the scripts I write as well. Thanks to this post and this, I found out it is implemented through the complete function in bash. For installed programs like pdflatex, the complete commands are added to /etc/bash_completion through the installer script (I think). For me, I can add them to my ~/.bashrc:

 <pre class="src src-sh"><span style="color: #ff4500;">##</span><span style="color: #ff4500;">complete -G "*.Rnw" pgfSweave</span>

complete -f -X ‘!.@(Rnw)’ pgfSweave Sweave Sweave.sh complete -f -X ‘!.@(eps|ps)’ eps2pdf.sh

Find files and find files containing certain text

This is a reminder to myself as I keep forgetting how to do these basic searches in Linux.

To find files with file name containing the text foo using the command find:

## find file with "foo" in file name
find ./ -name "*foo*" ## replace ./ with path; can use shell style wildcards
## ignore upper and lower cases
find ./ -iname "*foo*"
## print pathnames of all files
find ./ -print

To find files with bar in it’s content using the command grep:

grep "bar" -r ./
## print lines without the word "bar"
grep -v "bar" -r ./
## note, can also use regexp with -E