Tag Archives: find

UNIX find writeable files – more creative

In a follow-up to my previous post, I got tired of typing things.  And so I came up with this script:

[findWriteableFiles.sh]

#!/bin/bash

# Put arguments into an array
#
args=$@

# If no arguments, set firt value to current dir.
#
if [ -z "${args[0]}" ]; then
  args[0]=.
fi

# Loop over supplied paths and find writeable files
#
for arg in $args; do

  find $arg -type f -perm /u=w  | grep -v "\.class" | grep -v tmp-bin | \
grep -v aTest | grep -v fixToStuff | grep -v build/ | grep -v gensrc/ | \
grep -v Reference | grep -v "~" | grep -v "classes" | grep -v "buildtree"

done

exit

My only detail to sort out was exactly how to refer to $@.  Apparently if you quote it (being exactly “$@”) that takes all the arguments as a single string.  Unquoted, it takes them separately and gets what I’m looking for.

As you can see, I also have a lot of cruft I don’t want to see in my results, so I use good ol’ “grep -v” to weed it out.  Yes, I know there is a way to use egrep to make that all one command, but for some reason it doesn’t always want to play nicely for me.  So in a classic “eff it, let the script deal with it” I just chain some pipes.  I think if I had thousands of files to sort through, this would suck.  But I don’t, so it doesn’t.

Advertisements

UNIX find writable files

Way back when, I posted about using find and grep.  I have learned a couple of things since then.

1) find your critiera | xargs grep your grep criteria … is nicer to your processor, memory, and quickly returning the results.

2) finding writable files.  This has been particularly useful when using a really backwards SCS that doesn’t integrate nicely, cleanly, or usable with an IDE.

My command:  find . -type f -perm /u=w

So simple. So elegant. So simply says “find all files in this directory, recursively, that are writable by the user/owner”. The one confusing thing I had about this was /u instead of /o. I figured “owner, group, world”, but it is actually “user, group, other(s)”.

Anyway, this has improved my programming life.  Yeah, I’m gonna say it… take that, Windows.


UNIX find grep

I am always hunting for this, and so I’m going to put it here so I can remember!

This command is great:  find . -exec grep -l hello {} \;

Essentially, it does this:

  1. find all files starting in the current directory (“.”)
  2. for each file found execute “grep -l hello”
  3. grep -l will print file names — with full relative path — that match the grep criteria

This is great, as a programmer, to try and figure out which file had a certain word (or phrase, or Regular Expression pattern) in it.

There are also a couple of other pages with a large set of references here and here.