Issue
This is the folder structure that I have.
Using the find command find . -type d
in root
folder gives me the following result
Result
./folder1
./folder1/folder2
./folder1/folder2/folder3
However, I want the result to be only ./folder1/folder2/folder3
. i.e only print the result if there's a file of type .txt
present inside.
Can someone help with this scenario? Hope it makes sense.
Solution
find . -type f -name '*.txt' |
sed 's=/[^/]*\.txt$==' |
sort -u
Find all .txt
files, remove file names with sed
to get the parent directories only, then sort -u
to remove duplicates.
This won’t work on file names/paths that contain a new line.
Answered By - dan