Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a folder structure like this:

/domains/some-domain-1/applications/j2ee-apps
/domains/some-domain-2/applications/j2ee-apps
/domains/some-domain-3/applications/j2ee-apps

What is the best way to handle folders like this where the parent and child folders are the same? For example, if I wanted to cd to a cousin dir j2ee-apps of another domain folder from is there an easy way? What if I wanted to run a ls from the top level and get everything in the bottom folders (eg: j2ee-apps)?

Does anyone have any clever advice on how best to work with this?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You can set a variable to hold the variant part. Like so:

domain='some-domain-1'
cd /domains/$domain/applications/j2ee-apps
domain='some-domain-2'
cd /domains/$domain/applications/j2ee-apps

Since the cd command is the same, you can recall it on your shell with the arrow keys.

Depending on how frequently you do this, you might want to define a function in your .bashrc.

cdj2(){
  cd /domains/"$1"/applications/j2ee-apps
}

Then you can cdj2 some-domain-1.

Shell globbing (aka pathname expansion) can take care of the other part (see Stéphane Gimenez's answer). The find command would be useful if the directory structures weren't exactly the same, but you still want to see all the files matching a certain name.

find /domains -name 'j2ee-apps' -exec ls {} \;

share|improve this answer
    
What is the part on the end: '{} \'? –  javamonkey79 Oct 8 '11 at 1:27
    
I love the function definition suggestion, thanks! –  javamonkey79 Oct 8 '11 at 1:36
    
The funny bit at the end s the way the -exec option's arguments need to be formated. {} is replaced by the filename(s depending on other args and results). The argument has to be terminated with a ;, however, the shell interprets the ; and doesn't pass it to the command unless you escape it with a `\`. –  Shawn J. Goff Oct 8 '11 at 1:39
    
I can't seem to get the functions to work though :( Some tutorials say to use 'function name() { ... }' but this differs from your syntax. Neither seems to work. Is there something else special that one needs to do? –  javamonkey79 Oct 8 '11 at 1:46
    
When I put it in my .bash_profile using the 'function name(){...}' syntax it works. –  javamonkey79 Oct 8 '11 at 1:53
show 1 more comment

For ls, one can use the following to obtain the full list:

ls /domains/*/applications/j2ee-apps

To switch from one branch to the other I fear there is no easy way to do what you want.

share|improve this answer
    
Can you do something like this with find? –  javamonkey79 Oct 8 '11 at 1:26
1  
@javamonkey79: Yes you can use globs with find, eg. find $HOME/bin/*/c* -name '*a*' ... find also allows regex patterns: see -regex in man find –  Peter.O Oct 8 '11 at 12:31
add comment

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.