How to print only the final path of subdirectories?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print only the final path of subdirectories?
# 1  
Old 11-11-2013
How to print only the final path of subdirectories?

Hello, stumped with the find command to show the final path of subdirectories.

Used the command
Code:
find /dir1 -type d

Output:
Code:
/dir1/dirA
/dir1/dirA/dirB
/dir1/dirA/dirB/dirC

Desired output is:
Code:
/dir1/dirA/dirB/dirC

Please help with awk statement or find syntax. Thanks in advance.
# 2  
Old 11-11-2013
Something like this?
Code:
find /dir1 -type d | awk -F'/' '{m=m<NF?NF:m;A[m]=$0}END{print A[m]}'

# 3  
Old 11-11-2013
Untested:
Code:
p=0
out=''
for x in $(find /dir -type d)
do
    c=$(echo $x | tr -dc '/' | wc -c)
    if [ $c -gt $p ]
    then
        p=$c
        out=$x
    fi
done

echo $out

# 4  
Old 11-11-2013
Can there be multiple subdirectories in a directory? If yes, then neither of the foregoing suggestions will work. If no, then there's a simpler alternative:
Code:
find . -depth -type d | head -n1

Regards,
Alister
# 5  
Old 11-11-2013
Show all final directories
Code:
find . -depth -type d |awk -F/ 'NF>=prev {print} {prev=NF}'


Last edited by MadeInGermany; 11-11-2013 at 04:59 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 11-11-2013
How about a recursive bash/ksh approach (which would also include paths that contain symbolic links to directories, which are also paths):
Code:
finalp(){
  typeset dir
  for dir in "$1"/*/
  do
    if [ -d "$dir" ]; then
      finalp "${dir%/}"
    else
      printf "%s\n" "${1%/}"
    fi
  done
}

finalp /dir1


Last edited by Scrutinizer; 11-12-2013 at 02:34 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-11-2013
Smoking solution MadeInGermany - Might have to report you for violating rule #15 :wink:
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

A way to print only part of directory path

Hi, So I struggled to find a solution to the following problem: I want to make sed print only part of multiple different paths. So lets say we have /path/path1/path2/logs/bla/blabla /path/path1/path2/path3/logs/yadda/yadda/yadda Can someone suggest a way to make sed or other... (5 Replies)
Discussion started by: dampio
5 Replies

2. Shell Programming and Scripting

Search subdirectories and find and print total files

Hi All, I have a folder name lets say path/to/folder/CUSTOMER and under this i have several folders and each of these subfolder have serveral subfolders and so on and at some point i will have a folder name called "FTP_FILES" . I need to search for these folders named "FTP_FILES and then... (10 Replies)
Discussion started by: Kevin Tivoli
10 Replies

3. Shell Programming and Scripting

Print path files in different directories

Hi guys :) First of all Happy New Year :) so i dont know if my doubt its already here posted by other person ... i need to print to one file the path of few files that are in different directories, like this: directory muscle ATP6.aa.muscle.fasta COX1.aa.muscle.fasta . . . ... (2 Replies)
Discussion started by: andreia
2 Replies

4. Shell Programming and Scripting

How to list all Subdirectories and files with its full path in a parent directory?

How to list all Subdirectories and files with its full path in a parent directory? (1 Reply)
Discussion started by: johnveslin
1 Replies

5. UNIX for Dummies Questions & Answers

profile PATH include subdirectories

I would like to modify my .profile PATH to include all subdirectories of the directory I specify. For example, right now I have PATH=$HOME/tier1 Tier1 has a tier2 directory in it. Right now I can execute files from tier1, but not tier2. I know I can add another path with $HOME/tier1/tier2,... (1 Reply)
Discussion started by: Smed
1 Replies

6. Shell Programming and Scripting

Print directory name along with their path

Can any one tell me that how can i print all directory with their path in a given parent directory. i.e. parent directory /home/usr/ Now this shoe directory may contain sevral directory /home/usr dir1/ dir1.1/ dir1.2/ dir2 dir2.1/ dir2.2/ ... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

7. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

8. UNIX for Dummies Questions & Answers

Tree directory structure of Unix to get final node path

Hi, I want to list all the last directories from mentioned base path. for eg: If i have a base path say /base/base1/ How can i get the path till last node in tree like directory structure of unix by applying any command. so that i will get following output. ... (7 Replies)
Discussion started by: Shiv@jad
7 Replies

9. Shell Programming and Scripting

split and print $PATH

Hello simple question : how can i split the $PATH by the ":" seperator with one liner ? mybe using awk is there any builtin function in awk that splits and prints the output ? thanks (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question