views:

1278

answers:

7

... How do you do it? My code/ directory at work is organized in folders and subfolders and subsubfolders, all of which (at least in theory) contain scripts or programs I want to run on a regular basis.

Its turning my otherwise picturesque .bashrc into an eyesore!

Thanks!

+1  A: 

something like

my_path=$(find $root -type d | tr '\n' ':')

or

my_path=$(find $root -type d -printf '%p:')
Vardhan Varma
Very. Nice. Any way to exclude hidden directories? I'm have a git repo, so trying it out I got many more directories than I was expecting :-/
No reason to have find print with '\n's -- better to tell it to colon-separate the files in the first place.
Charles Duffy
+2  A: 

In bash 4.0 you can just use the newly supported ** operator.

You have to enable it first on some with :

shopt -s globstar

You can then do

echo **

which recursively echos all files that are descendant of the current dir.

Beware it does tend to bail out on overly complicated dirs sometimes, so use the ** at the lowest recucurring point.

echo **/

Coincidentally, emits recursively all directory names, and only directory names. ( Excluding the current dir )

echo ./**/

Includes the current dir. ( Incidentally, it also skips hidden directories )

This should thuswise be suited for creating a path string:

echo ./**/ | sed 's/\s\s*/:/g'

And if you don't want relative paths,

echo $PWD/**/ | sed 's/\s\s*/:/g'

Ack

From your comment on one of the other posts it sounds like you're wanting behaviour much like 'Ack' provides. If you were intending to use a find + grep combination, this tool is generally much more efficient and easier to use for this task.

Example:

# search for 'mystring' in all c++ files recursively ( excluding SCM dirs and backup files ) 
ack  "mystring" --type=cpp 

# finds all text files not in an SCM dir ( recursively) and not a backup using type heuristics. 
ack -f --type=text
Kent Fredric
Was not aware of either of those, is there a way to make either output directories?
I like this solution for its brevity, however I think I might have a different version of sed than you, it replaces the first "s" is each directory with a colon. But kudos for making me upgrade to bash 4.0 :)
O_o. Thats not a very complicated regex :S. \s\s* is supposed to just replace spaces.
Kent Fredric
+3  A: 

At the end of your script, put the line:

PATH=${PATH}:$(find ~/code -type d | tr '\n' ':' | sed 's/:$//')

This will append every directory in your ~/code tree to the current path. I don't like the idea myself, preferring to have only a couple of directories holding my own executables and explicitly listing them, but to each their own.

If you want to exclude all directories which are hidden, you basically need to strip out every line that has the sequence "/." (to ensure that you don't check subdirectories under hidden directories as well):

PATH=${PATH}:$(find ~/code -type d | sed '/\/./d' | tr '\n' ':' | sed 's/:$//')

This will stop you from getting directories such as ~/code/level1/.hidden/level3/ (i.e., it stops searching within sub-trees as soon as it detects they're hidden). If you only want to keep the hidden directories out, but still allow non-hidden directories under them, use:

PATH=${PATH}:$(find ~/code -type d -name '[^.]*' | tr '\n' ':' | sed 's/:$//')

This would allow ~/code/level1/.hidden2/level3/ but disallow ~/code/level1/.hidden2/.hidden3/ since -name only checks the base name of the file, not the full path name.

paxdiablo
Nice, but same comment as for Vardhan... that is I need to exclude hidden directories. Also, what is the purpose of the piping the result of tr to sed?
hes trimming the last : so it doesn't produce a bogus path
Kent Fredric
you'd probably want to use find -print0 | tr "\0" ":" there instead Pax, then you'll have a filter that should work on *ALL* valid unix paths ( including ones with pesky newlines in them )
Kent Fredric
@Kent: Non-printable characters (including spaces) are evil and, if I ever find someone who uses them, I'll beat them to death with my entire collection of IBM mainframe COBOL manuals :-)
paxdiablo
find -type d -print0 | sed -e "y/\d0/:/;s/:$//;" # also another reduction possible.
Kent Fredric
if you want entertainment, put a beep character into a directory name. You'll know if somebody in the room CD's into it that way ;)
Kent Fredric
@xxlunatic, see update for stripping out hidden directories (and all their subdirectories).
paxdiablo
@Pax, why not just tell find to strip out hidden directories rather than doing it after-the-fact?
Charles Duffy
Because if you have a directory ~/code/.hidden/not_hidden, the find will give it to you with -name '[^\.]*' - I assumed xxlunatic wanted to stop searching a subtree as soon as it was deemed hidden.
paxdiablo
But updated with extra info anyway.
paxdiablo
@Pax, using -prune will stop find from recursing down those directories in the first place.
Charles Duffy
A: 

I have a single bin directory $HOME/bin and that gets an installed copy of any programs I build (or scripts, or symlinks to programs or scripts). It currently has almost 600 commands in it (ls | wc -l says 604, but there are a dozen or so sub-directories for various reasons).

When I'm testing a program, I execute it where I build it; once I've done testing for the time being, I acquire it with my acquire script, which copies the file and sets the permissions on it.

This leaves me with a nice tidy profile (I don't use .bashrc; I'd rather do the setup once when the login shell starts, and the sub-shells inherit the working environment without having to parse .bashrc again), but a rather ungainly bin directory. It avoids the cost of resetting PATH each time a shell starts, for example, and given how complex my path-setting code is, that is just as well!

Jonathan Leffler
+1  A: 

The following Does The Right Thing, including trimming hidden directories and their children and properly handling names with newlines or other whitespace:

export PATH="${PATH}$(find ~/code -name '.*' -prune -o -type d -printf ':%p')"

I use a similar trick for automatically setting CLASSPATHs.

Charles Duffy
Very cool, no invocations sed because find handles everything for you... except I have a version of find without printf :-/
A: 

Something like this:

_path="$(
  find <path> -name '.*' -prune -o -type d -print
  )" 

[[ $_path ]] && _path="${_path//$'\n'/:}" PATH="$PATH:${_path%:}"

If you have GNU find you may use -printf ':%p' directly.

radoulov
A: 

If you really need to go down this road, you could try minimizing that PATHs list some more: drop folders that contain no executables. Of course, at the cost of even more stats. ;-/

PATH=$PATH$(find ~/code -name '.*' -prune -o -type f -a -perm /u+x -printf ':%h\n' | sort | uniq | tr -d '\n')

I'd avoid doing this at each shell spawn. Some kind of caching should be used. For instance, add this line to your ~/.bashrc:

[ -s ~/.codepath ] && export PATH=$PATH$(<~/.codepath)

and run

find ~/code -name '.*' -prune -o -type f -a -perm /u+x -printf ':%h\n' |sort |uniq |tr -d '\n' > ~/.codepath

only when you know something really changed.

EDIT: here's a rewrite without your missing -printf

find ~/code -name '.*' -prune -o -type f -a -perm /u+x -print | sed 's@/[^/]\+$@:@' | sort | uniq | tr -d '\n' | sed 's/^/:/; s/:$//'
altblue