I have a file hierarchy like:
data
├── debug.log
├── messages
│ ├── msg001.txt
│ ├── msg002.txt
│ └── msg003.txt
└── pictures
├── msg002
│ └── pic001.jpg
└── msg003
├── pic001.jpg
└── pic002.jpg
I would like to find all the files, and all the directories below the top two levels (data, data/messages, and data/pictures). All the things that are not a part of the fixed structure of the hierarchy, if that makes sense.
Can i do this with a single find invocation?
I can find the files:
$ find data -type f | sort
data/debug.log
data/messages/msg001.txt
data/messages/msg002.txt
data/messages/msg003.txt
data/pictures/msg002/pic001.jpg
data/pictures/msg003/pic001.jpg
data/pictures/msg003/pic002.jpg
And i can find the directories:
$ find data -mindepth 2 -type d | sort
data/pictures/msg002
data/pictures/msg003
But i can't combine these, because -mindepth is an option, not a test:
$ find data -type f -o \( -mindepth 2 -type d \) | sort
find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
data/messages/msg001.txt
data/messages/msg002.txt
data/messages/msg003.txt
data/pictures/msg002
data/pictures/msg002/pic001.jpg
data/pictures/msg003
data/pictures/msg003/pic001.jpg
data/pictures/msg003/pic002.jpg
(note that data/debug.log is not found here)
Is there any way to consider the depth in the hierarchy as a real test?
The best kludge i can think of is using a regexp on the path to recognise the top two levels of directories:
$ find data -type f -o \( -type d -regextype posix-extended \! -regex 'data(/[^/]+)?' \) | sort