BASH - print only the deepest directory in the path

I need help.....

In my .bashrc file, I have a VERY useful function (it can be a little rough and ready-made and a little hacked, but it works with pleasure!), Which reads the input file and uses the "tree" function on each of the input lines to create a directory tree . this tree is then printed to the output file (along with the folder size).

multitree() { while read cheese do pushd . > /dev/null pushd $cheese > /dev/null echo -e "$cheese \n\n" >> ~/Desktop/$2.txt tree -idf . >> ~/Desktop/$2.txt echo -e "\n\n\n" >> ~/Desktop/$2.txt du -sh --si >> ~/Desktop/$2.txt echo -e "\n\n\n\n\n\n\n" >> ~/Desktop/$2.txt popd > /dev/null done < $1 cat ~/done } 

This is a temporary splash screen, like the end, and displays the snippet as follows:

 ./foo ./foo/bar ./foo/bar/1 ./foo/bar/1/2 

etc. etc.

however, the first (and most tedious) thing I need to do is delete all entries, leaving only the deepest path to the folder (using the above example, it will be reduced only to. / foo / bar / 1/2)

Is there a way to process the file before / after the tree function only to print the deepest levels?

I know something like python can do better, but my problem is that I never used python. And I'm not sure that working systems will let me run python ... they let us change our own .bashrc I'm not too worried!

Thanks in advance guys !!!!

Owen.

+6
source share
2 answers

you can use

 find . -type d -links 2 

Replace if necessary . catalog.

EDIT: Explanation:

find searches the directory of files matching this filter. In this case, the directory . and the filter is -type d -links 2 .

-type d directory filters

-links 2 filters for those that have two (hard) links to their name. Effectively, these are filters for all directories that do not have subdirectories, because only those that have two: one in its parent directory and . . Those that have subdirectories also have links .. in their subdirectories.

+15
source

Here is a hint:

You just need to count the number of "/" characters in each line.

If the current line is less than the number of "/" characters in the previous line, the previous line will be the "deepest" in its part of the hierarchy.

This line and any subsequent line with even fewer "/" characters will NOT be the deepest directory in its part of the entire directory hierarchy. Once you get a line with the same number of characters "/" or more, you can "reset" and, again, keep track of the first line with fewer characters "/".

And finally, you need to handle the trivial case: only one line in your tree, the current directory does not have subdirectories, so it wins by default.

Another way to implement this is to consider the following statement:

If the directory name also exists as the exact prefix of another directory in the list, followed by the "/" character, then this is NOT the deepest directory in its part of the hierarchy.

+2
source

Source: https://habr.com/ru/post/980102/


All Articles