Let's say I have the following:
./
Dir1/
Sub1/
Sub1.jpg
Sub2/
Sub2.jpg
Sub2.png
Sub2.txt
Dir2/
Sub1/
Sub1.doc
Sub1.txt
Sub2/
Sub2.jpg
Sub2.png
SomeTxt.txt
Dir3/
Dir3.txt
Dir4/
Sub1/
Dir4.txt
Some.txt
Using bash (bashrc in my case), how can I get a variable that contains all of the directories containing a text file (.txt) with the same name as the containing directory? So from the example above I would want to have a variable containing the following:
./Dir1/Sub2/
./Dir1/Sub1/
./Dir3/
It shouldn't return ./Dir1/Sub1/
because it doesn't have a Sub1.txt
file.
It shoudn't return ./Dir2/Sub2/
because the txt file in it isn't Sub2.txt
.
It shouldn't return ./Dir4/
or ./Dir4/Sub1/
because Dir4.txt
is not directly in ./Dir4/
.
NOTE:
The names of the directories don't have to be these names in particular. What is important is that the txt file's name matches the containing directory's name.
Also, I know other languages might be easier but I need it in bash.
dirs = (path.parent for path in Path().rglob('*.txt') if path.parent.name == path.stem)
dir3.txt
anddir3
are not the same name.