Super User is a question and answer site for computer enthusiasts and power users. 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

Is there a Linux command that will return the file path of the bottom most dir.

I've tried the following commands: find /home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/ -type d

and

tree -dfi /home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/

Both these commands return:

/home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7
/home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/generic
/home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/admin
/home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/logandnetworkactivity
/home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/qvm
/home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/qvm/a
/home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/reports

As you can see in the case of:

/home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/qvm

    /home/Alison/workspace/DirLevel1/DirLevel2/DirLevel3/DirLevel4/DirLevel5/DirLevel6/DirLevel7/qvm/a

It is returning /qvm/ as well as /qvm/a where I just want the bottom dir.

share|improve this question

migrated from stackoverflow.com Sep 3 '12 at 5:40

This question came from our site for professional and enthusiast programmers.

1  
What if there are multiple paths at the same depth? Do you need both or just one? – thkala Sep 2 '12 at 16:38
up vote 3 down vote accepted

This should do the job:

find . -type d | \
  awk -F/ '
    NF <= old_NF {print prev}
    {old_NF=NF; prev=$0}
    END {print $0}
  '

Example:

find . -type d
.
./backup
./source
./source/siteA
./source/siteA/regionA
./source/siteA/regionA/files
./source/siteA/regionB
./source/siteA/regionB/files
./source/siteA/regionD
./source/siteA/regionD/files
./source/siteA/regionC
./source/siteA/regionC/files
./source/dir with space
./source/siteB
./source/siteB/regionA
./source/siteB/regionA/files
./source/siteB/regionB
./source/siteB/regionB/files

Now the command from above gives:

./backup
./source/siteA/regionA/files
./source/siteA/regionB/files
./source/siteA/regionD/files
./source/siteA/regionC/files
./source/dir with space
./source/siteB/regionA/files
./source/siteB/regionB/files

Short explanation:

The plain find . -type d output is already sorted according to the directory structure. What you now want is those lines (=paths) that are immediatly before a line that has the same or less depth. And the last line (always).

This is what the awk command does. It splits input lines on the path separator (-F/), stores the number of fields and the line ({old_NF=NF; prev=$0}) and then compares the old number of fields to the current one and prints the stored line if number of fields is less than or equal (NF <= old_NF {print prev}). And it prints the last line always (END {print $0}).

share|improve this answer
    
The level won't always be the same so would need to be more flexible to get to the bottom level no matter how deep the path goes. – Colin747 Sep 2 '12 at 16:11
    
@Colin747 please see my updated answer... – Theodros Zelleke Sep 2 '12 at 16:39
    
Worked perfectly, thanks. – Colin747 Sep 2 '12 at 16:42
    
But i t wont work appropriately on such a case (or am I misunderstanding the question?): ./source ./source/siteA ./source/siteA/regionA ./a/very/long/path/to/some/where/else ./a this would only print the 4th line. – Sahand Mozaffari Sep 2 '12 at 16:57
    
@SahandMozaffari before find prints ./a/very/long/path/to/some/where/else it definitely prints ./a, ./a/very, ... that's why it works ;-) – Theodros Zelleke Sep 2 '12 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .