0

How would I modify the following bash instruction to get only the directories that match exactly genre_ (basename) and another genre_*. As the next step, I appended | wc -l to the second find, but I get for each line the same very high number, which tells me it's a count across directories (why?)?

$ find . -name 'genre_' -exec dirname {} \; | while EFS= read -r thedir; do echo "$(find . -name 'genre_*')"; done
./The-Specials/genre_
./The-Specials/genre_rock
./ACDC/genre_Hard-Rock
./We-3/genre_
./We-3/genre_Jazz

In the output above, only ACDC should be filtered out.

0

1 Answer 1

0

I admit I don't understand what your code is supposed to do (read: the question is not clear in this matter). However I can answer this:

As the next step, I appended | wc -l to the second find, but I get for each line the same very high number, which tells me it's a count across directories (why?)

You read -r thedir but thedir is never used. The second find is the same command every time, starting in ., hence the same number.

Bonus: I don't see the point of echo "$(find …)". This code smells fishy. It probably should be just find ….

1
  • Sorry, it should have been $thedir in place of . inside the second find statement. This solved it: $ find . -name 'genre_' -exec dirname {} \; | while EFS= read -r thedir; do var="$(find $thedir -regex '.*genre_.*'| wc -l)"; if (( "$var" > 1 )); then rm "$thedir"/genre_; fi; done but I welcome any suggestion to make it more concise.
    – Erwann
    Commented Jun 18, 2018 at 23:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .