Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder structure like this:


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
up vote 3 down vote accepted

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

cd /domains/$domain/applications/j2ee-apps
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.

  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

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
@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

Your Answer


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.