I have tried following command find . | egrep -v '.*/[A-Z]{3}-[0-9]{8}-.' to recursively search for files (not folders) that are not in the pattern. This also displays folders! What am I missing?
Asked
Active
Viewed 140 times
2
e4c4c1
- 31
- 5
-
Obviously `egrep` simply filters out all input lines which match that pattern anywhere in the line. You could fix that by adding `[^/]*$` to the end of the pattern, but using the search predicates of `find` directly, as suggested in Wiktor's answer, is a much better solution. – tripleee Mar 18 '22 at 11:17
1 Answers
3
You can use find directly with -not option:
find . -type f -regextype posix-egrep -not -regex '.*/[A-Z]{3}-[0-9]{8}-[^/]*$' -exec basename {} \;
With GNU find, you may use
find . -type f -regextype posix-egrep -not -regex '.*/[A-Z]{3}-[0-9]{8}-[^/]*$' -printf "%f\n"
Details:
-type f- return only file paths-regextype posix-egrepsets the regex flavor to POSIX ERE-notreverses the regex result.*/[A-Z]{3}-[0-9]{8}-[^/]*$- matches paths where file names start with three uppercase letters,-, eight digits,-and then can have any text other than/till the end of the string-exec basename {} \;/-printf "%f\n"only prints the file names without folders (see Have Find print just the filenames, not full paths)
Wiktor Stribiżew
- 607,720
- 39
- 448
- 563
-
Thank you for your hint! Do you have another hint that I see only data and no folders? – e4c4c1 Mar 18 '22 at 11:10
-
@e4c4c1 `-type f ` only outputs files... Or do you mean you only want to see the file names without the folders in the output? Then use `find . -type f -regextype posix-egrep -not -regex '.*/[A-Z]{3}-[0-9]{8}-[^/]*$' -exec basename {} \;` – Wiktor Stribiżew Mar 18 '22 at 11:13
-
-
1@WiktorStribiżew I was searching for `-type f` in combination with `-exec basename {} \;`, also your whole last command fits! Thank you! – e4c4c1 Mar 18 '22 at 11:35