Bash Shell Scripting One Liners

Finding Code in PHP files

This will list all .php files that contain a certain string. Just replace {token} with whatever string you’re searching for. I use this when I have to automate figuring out which PHP files include other PHP files.

find ./ -name '*.php' | sed 's/^\(.*\)$/"\1"/g' | xargs grep -lF '{token}'


Handling file names with Spaces with find and xargs find can find files with spaces in their file names. This makes xargs go crazy unless you:

find ./ -type f -print0 | xargs -0 ...

The -print0 instructs find to end each search results with a NUL character. The -0 instructs xargs to use a NUL character as a separator. Neato


Example of how to find missing sequential files. I have a bunch of files named: 001.zip, 002.zip, 003.zip, etc. This helps me find if there are any missing files.

c=318; until [ $c -lt 1 ]; do a=`printf '%03d' $c`; \\
if [ ! -f "${a}.zip" ]; then echo "Missing ${a}.zip"; fi; let c-=1;


Determining what directory a shell script is in

First the code:

_DIR=`dirname "$(readlink -f $0)"`

This is useful if the shell script calls other scripts and is run within cron. Use the above command to change to a known directory and then run other scripts / commands.

{ 1 comment… read it below or add one }

1 Rajesh Duggal December 21, 2006 at 2:28 am

For searching inside files you can also try..

grep -Lr -include ‘*.php’ token

However, different versions of grep take different command line options.
If yours doesnt have a way to specify a filename filter
You can try..

find ./ -name ‘*.php’ -exec grep -L token {} \;

Rajesh Duggal

Reply

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>