Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH - Only printing the deepest directory in path

Tags:

linux

bash

tree

I need some help.....

In my .bashrc file I have a VERY useful function (It may be a bit rough and ready, and a bit hacky, but it works a treat!) that reads an input file, and uses the 'tree' function on each of the input lines to create a directory tree. this tree is then printed into an output file (along with the size of the folder).

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 time saver like no end, and outputs a snippet like the following:

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

etc etc....

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

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

I know something like python might do a better job, but my issue is I've never used python And I'm not sure the work systems would let me run python... they let us modify our own .bashrc so I'm not too worried!

Thanks in advance guys!!!!

Owen.

like image 836
Owen Morgan Avatar asked Dec 24 '14 00:12

Owen Morgan


People also ask

How do I print the current working directory in Bash?

It is typically printed as the full path to the directory (meaning that you can see the parent directory). To print the name of the current working directory, use the command pwd. As this is the first command that you have executed in Bash in this session, the result of the pwd is the full path to your home directory.

How to find the deepest level of a directory in Linux?

The locate command is your friend in this case: Stuff in enough /*/* until no results are displayed, then subtract one /* to get the deepest subdirectory level. The files in the deepest levels will also be display.

How to print only the directory names of a file?

If you want only the directory names as opposed to their full path, with GNU find, you can replace the -print with -printf '%f ' or assuming the file paths don't contain newline characters, pipe the output of the above command to awk -F / ' {print $NF}' or sed 's|.*/||' (also assuming the file paths contain only valid characters).

What is the full path to my home directory in Bash?

As this is the first command that you have executed in Bash in this session, the result of the pwd is the full path to your home directory. The home directory is the default directory that you will be in each time you start a new Bash session. Windows users: note that the Terminal uses forward slashes ( /) to indicate directories within a path.


1 Answers

You could use

find . -type d -links 2

Replace . with a directory if desired.

EDIT: Explanation:

find searches a directory for files that match a given filter. In this case, the directory is ., and the filter is -type d -links 2.

-type d filters for directories

-links 2 filters for those that have two (hard) links to their name. Effectively, this filters for all directories that have no subdirectories, because only those have two: The one in their parent directory and the . link in themselves. Those with subdirectories also have the .. links in their subdirectories.

like image 143
Wintermute Avatar answered Sep 24 '22 11:09

Wintermute