Shell – how to have find on a directory with a changing pattern

directory-structureshell

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?

Best Answer

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 {} \;

Related Question