Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This is what I have so far:

for f in 'svn ls repository_dir'; 
do 
svn checkout repository_dir/$f/trunk/dir1/dir2/dir3/dir4/needed_dir 
done

This works great for the projects (100's of them) that have the needed_dir in the correct place. But some projects ($f) have their directory structure a little different. So "needed_dir" might be in a different location.

In the do loop, how can I tell my bash script to:

"Find "needed_dir". If found, check it out."

Or

"Find "needed_file.txt". If found, check it out."

Thank you for any help

share|improve this question

2 Answers 2

This will probably be easiest with find.

To execute svn checkout or every directory named needed_dir in the directory tree of repository_dir/$f/trunk, use this command:

find repository_dir/$f/trunk/ -type d -name needed_dir -exec svn checkout {} \;

Find substitutes {} with the name of the found directory.

The switch -type d only finds directories, while -type f finds only files.

share|improve this answer
    
For some reason when I press [Enter] on the command in the terminal, it doesn't finish. I get the ">" in the terminal, as if it's waiting for more. My command is "for f in svn ls https://repository_dir/; do find repository_dir/$f/trunk -type d -name needed_dir -exec svn checkout {} \; done" Any ideas? –  user1776193 Mar 26 '13 at 4:49
    
@user1776193: You missed one ;. Should be: for f in `svn ls https://repository_dir/`; do find repository_dir/$f/trunk -type d -name needed_dir -exec svn checkout {} \;; done –  whjm Mar 26 '13 at 5:05
    
@clarkw when I add the extra ; at the end, it gives me the error: -bash: syntax error near unexpected token `done' –  user1776193 Mar 26 '13 at 9:19
    
@user1776193: As it said it's syntax error so try to fix it yourself. It's easy. –  whjm Mar 26 '13 at 11:33
    
I see no reason why the command as it is wouldn't work. Could you copy and paste the actual command you're using? –  Dennis Mar 26 '13 at 12:15

Sorry I don't have enough reputation to comment, so I have to create a new answer.

The find command is not going to work here because it can only search a directory on disk. (So it would work only if you first svn checkout the entirety of "repository_dir".)

See the following link on StackOverflow for a similar question. Something similar to the answer given there is more appropriate:

for f in 'svn ls -R repository_dir | grep "needed_dir"'; 
do 
svn checkout repository_dir/$f
done

The -R option to svn makes svn list everything at URL recursively. Grep searches for the directory you need.

For what it is worth, you can make this somewhat more terse by using the xargs command instead of a for loop.

svn list -R <URL> | grep "needed_dir" | xargs --max-args=1 -I'{}' svn co <URL>'{}'

Xargs runs "svn co (path-in-url)" for each white-space delimited word discovered by svn list and grep. (The weird -I'{}' is necessary otherwise xargs puts a space between the URL and the subdirectory name.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.