Remove redundant folders in a filepath

Is there a way to remove redundant folders in a file path?

Given a file system like
├── folder1
│ ├── subfolder1
│ │ ├── /images1/redundant1/redundant2/file.txt
│ │ ├── /images2/redundant1/redundant2/file.txt
│ ├── subfolder2
├── folder2

How would I remove the redundant folders that don’t contain any immediate files within them? So keep the same files, just remove the redundant folders from the path?

Like this:
├── folder1
│ ├── subfolder1
│ │ ├── /images1/file.txt
│ │ ├── /images2/file.txt
│ ├── subfolder2
├── folder2

I would tackle this from the shell level, and think about a bash script to do this rather than Julia.
I would start by using the find utility, and my brain is saying recursion till you get to the leaves of the tree.
However I do not have the find fu aat this time in the morning.
find cn discover empty directories by
find -type d -empty
However you are looking for directories which contain one or more than one directory object

ps the tree utility will display your file tree structure nicely. Looks like you cannot do much with the output of tree

pps Think about what happens if you follow symbolic links. I would not follow links - you could end up stomping ll over a filesystem.
Also think about hidden files - make sure you find them

ppps I will be humiliated when someone points out this is Comp Sci 101 problem and you just recurse down the tree

1 Like